Pytorch-數據類型


1.張量數據類型

Pytorch常用的數據類型,其中FloatTensor、DoubleTensor、ByteTensor、IntTensor最常用。GPU和CPU的Tensor不相同。

 

 

  •  數據類型檢查使用isinstance()
import torch

a = torch.randn(2,3)
#torch.FloatTensor
a.type()
#true
isinstance(a,torch.FloatTensor)
  • 標量,torch.tensor(),t是小寫的
import torch

a = torch.tensor(2)
#0
print(len(a.shape))
#torch.Size([])
print(a.size())
  • 1維張量
import torch
import numpy as np

#torch.tensor里邊放的是數
a = torch.tensor([2.1])
print(a.shape)

#torch.FloatTensor里邊放的是維度
a = torch.FloatTensor(2)
#tensor([5.6052e-45, 0.0000e+00])
print(a)

a = np.ones(2)
#tensor([1., 1.], dtype=torch.float64)
b = torch.from_numpy(a)
print(isinstance(b,torch.DoubleTensor))
  • 多維張量
import torch
import numpy as np

# tensor([[[0.9539, 0.4338, 0.9842],
#          [0.2288, 0.0569, 0.9997]]])
a = torch.rand(1,2,3)
#3,返回維度
a.dim()
#6,返回元素數,1*2*3
a.numel()
  • 初始化張量
import torch

#(0,1)之間均值分布初始化
a = torch.rand(3,3)
# tensor([[0.7140, 0.3779, 0.7530],
#         [0.1225, 0.2168, 0.9868],
#         [0.6421, 0.0806, 0.1370]])
print(a)

#接收一個tensor,把a的shape讀出來,生成一個a的shape的均值分布
b = torch.rand_like(a)
# tensor([[0.6055, 0.3282, 0.4211],
#         [0.9757, 0.3171, 0.5054],
#         [0.3429, 0.1091, 0.9734]])
print(b)

#生成1-10之間的整數,生成形狀是[3,3]
c = torch.randint(1,10,[3,3])
# tensor([[6, 6, 9],
#         [8, 1, 1],
#         [9, 6, 8]])
print(c)
  • torch.full()
#生成一個2*3,元素都是7的tnensor
#生成標量使用[]
a = torch.full([2,3],7)
print(a)
  • torch.arange()
#生成[0,10)之間的等差數列
#tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
a = torch.arange(0,10)
#tensor([0, 2, 4, 6, 8])
b = torch.arange(0,10,2)
  • torch.linspace()
#[0,10]之間均勻取11個點
#tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.])
a = torch.linspace(0,10,steps=11)
print(a)
  • torch.ones()、torch.zeros()、torch.eye()
#單位陣
a = torch.ones(3,3)#0矩陣
b = torch.zeros(3,4)#對角陣
c = torch.eye(3)

2.索引

索引是從第0維開始的

import torch

a = torch.rand(4,3,28,28)

#torch.Size([3, 28, 28])
print(a[0])
#torch.Size([28, 28])
print(a[0,1])
  • :選取
import torch

a = torch.rand(4,3,28,28)
#選擇0維度0,1兩個元素,不包括2
print(a[:2])
#選擇0維度最后一個元素
#索引正編號[0,1,2,3],反編號[-4,-3,-2,-1]
#等價於a[3]
print(a[-1:])

總的形式可以表達為:start:end:step,::2表示元素隔1個進行采樣

  • 選擇特定的行,index_select()
import torch

a = torch.rand(4,3,28,28)

#第0個維度,選擇特定的第0個和第三個元素
b = a.index_select(0,torch.tensor([0,3]))
  • masked_select(),選擇特定位置元素
import torch

# tensor([[0.1956, 0.1843, 0.2313],
#         [0.1363, 0.4729, 0.7214],
#         [0.5356, 0.4904, 0.5742]])
a = torch.rand(3,3)
#值大於0.5的元素位置設為1
# tensor([[0, 0, 0],
#         [0, 0, 1],
#         [1, 0, 1]], dtype=torch.uint8)
mask = a.ge(0.5)
#tensor([0.7214, 0.5356, 0.5742])
c = torch.masked_select(a,mask)

2.維度變換

  • view()、reshape()
import torch

a = torch.rand(4,1,28,28)
#torch.Size([4, 784])
b = a.view(4,28*28)
#view完以后會丟失a的shape信息
#b.view(4,28,28,1),這樣變換語法上沒有錯誤,但是由於shape和a不匹配,變換的數據並不是原數據,造成數據污染
  • 增加維度,unsqueeze()
import torch

#torch.Size([2])
a = torch.tensor([15,22])
#正數表示在哪個維度之前添加一個維度,值<a.dim()+1
#torch.Size([2, 1])
# tensor([[15],
#         [22]])
b = a.unsqueeze(1)
#torch.Size([1, 2])
#tensor([[15, 22]])
b = a.unsqueeze(0)

#負數表示在哪個維度之后插入一個維度,值≥-a.dim()-1
#torch.Size([2, 1])
b=a.unsqueeze(-1)
#torch.Size([1, 2])
b=a.unsqueeze(-2)
# 正, 0,1, 2, 3
#    [4,3,28,28]
# 負,-4,-3,-2,-1
x = torch.rand(4,3,28,28)
#等價x.unsqueeze(2)
q = x.unsqueeze(-3)
  • squeeze(),維度減少
import torch

x = torch.rand(1,1,28,1)
#不填參數會把所有為1的維度減少
a = x.squeeze()
#把第3維維度減少
#torch.Size([1, 1, 28])
b = x.squeeze(3)
  • expand(),維度擴展
import torch

#維度拓展可以把所有是1的維度擴展到需要維度
x = torch.tensor([[1,2]])
# tensor([[1, 2],
#         [1, 2]])
x = x.expand(2,2)
y = torch.tensor([[3,4],[5,6]])
# tensor([[4, 6],
#         [6, 8]])
a = x +y
  • transpose(),交換行列

 

  •  permute(),把原來的行列交換
import torch

x = torch.rand(4,3,28,28)
#交換0維和1維
#torch.Size([3, 4, 28, 28])
b = x.permute(1,0,2,3)

permute和transpose會打亂數據在內存中的位置,如果數據在內存中不連續了,使用contiguous()把數據變成連續的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM