溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Pytorch中的Tensor常用操作有哪些

發(fā)布時間:2022-02-24 09:38:51 來源:億速云 閱讀:172 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“Pytorch中的Tensor常用操作有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Pytorch中的Tensor常用操作有哪些”這篇文章吧。

1、創(chuàng)建Tensor

import torch
#經(jīng)典方式
device = torch.device("cuda:0")
x = torch.tensor([1,2],dtype = torch.float32,device = device,requires_grad=True)
w = sum(2 * x)
w.backward()
print(x.device)
print(x.dtype)
print(x.grad)
#Tensor
y = torch.Tensor([1,2,3])
#等價于
y = torch.FloatTensor([1,2,3])#32位浮點型
#后者聲明打開梯度
y.requires_grad = True
#還有其他類型,常用的
torch.LongTensor(2,3)
torch.shortTensor(2,3)
torch.IntTensor(2,3)
w = sum(2 * y)
w.backward()
print(y.grad)
print(y.dtype)

輸出:

cuda:0
torch.float32
tensor([2., 2.], device='cuda:0')
tensor([2., 2., 2.])
torch.float32

和numpy類似的創(chuàng)建方法

x = torch.linspace(1,10,10,dtype = torch.float32,requires_grad = True)
y = torch.ones(10)
z = torch.zeros((2,4))
w = torch.randn((2,3))#從標(biāo)準(zhǔn)正態(tài)分布(均值為0,方差為1)上隨機(jī)采用,高斯噪聲點,而rand相當(dāng)于在0,1間隨機(jī)采樣
#torch.normal()????
print(x)
print(y)
print(z)
print(w)

輸出

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.], requires_grad=True)
tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.]])
tensor([[-0.6505,  1.3897,  2.2265],
        [-1.7815, -1.8194, -0.4143]])

從numpy轉(zhuǎn)換

np_data = np.arange(2,13,2).reshape((2,3))
torch_data = torch.from_numpy(np_data)#numpy轉(zhuǎn)tensor
print('
numpy',np_data)
print('
torch',torch_data)

輸出

numpy [[ 2  4  6]
 [ 8 10 12]]

torch tensor([[ 2,  4,  6],
        [ 8, 10, 12]], dtype=torch.int32)

2、組合

import torch
x = torch.arange(0,10,1).reshape(2,-1)#size=(2,5)
y = torch.ones(10).reshape(2,-1)#size=(2,5)
print(x)
print(y)
w = torch.cat((x,y),dim = 0)#默認(rèn)從size最左邊開始,這里結(jié)果為:(2+2,5)
z = torch.cat((x,y),dim = 1)#(2,5+5)
print(w,w.size())
print(z,z.size())
#還有種stack()

輸出:

tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
tensor([[0., 1., 2., 3., 4.],
        [5., 6., 7., 8., 9.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]]) torch.Size([4, 5])
tensor([[0., 1., 2., 3., 4., 1., 1., 1., 1., 1.],
        [5., 6., 7., 8., 9., 1., 1., 1., 1., 1.]]) torch.Size([2, 10])

3、數(shù)據(jù)類型轉(zhuǎn)換

法一

x = torch.rand((2,2),dtype = torch.float32)
print(x.dtype)
x = x.double()
print(x.dtype)
x = x.int()
print(x)

輸出:

torch.float32
torch.float64
tensor([[0, 0],
        [0, 0]], dtype=torch.int32)

法二

x = torch.LongTensor((2,2))
print(x.dtype)
x = x.type(torch.float32)
print(x.dtype)

輸出:

torch.int64
torch.float32

4、矩陣計算

x = torch.arange(0,4,1).reshape(2,-1)
print(x)
print(x * x )#直接相乘
print(torch.mm(x,x))#矩陣乘法
print(x + 1)#廣播
print(x.numpy())#轉(zhuǎn)換成numpy

輸出:

tensor([[0, 1],
        [2, 3]])
tensor([[0, 1],
        [4, 9]])
tensor([[ 2,  3],
        [ 6, 11]])
tensor([[1, 2],
        [3, 4]])
[[0 1]
 [2 3]]

5、維度變化

主要是對維度大小為1的升降維操作。

 torch.squeeze(input)#去掉維度為1的維數(shù)
 torch.unsqueeze(input,dim)#指定位置增加一維

以上是“Pytorch中的Tensor常用操作有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI