App下載

Pytorch的Tensor常用操作有哪些?Tensor常用操作歸納!

猿友 2021-07-15 09:24:14 瀏覽數(shù) (2659)
反饋

對(duì)于機(jī)器學(xué)習(xí)而言,張量(tensor)是其實(shí)現(xiàn)的一個(gè)基礎(chǔ)。所以著名的機(jī)器學(xué)習(xí)庫TensorFlow便從中進(jìn)行取名。作為一個(gè)機(jī)器學(xué)習(xí)實(shí)現(xiàn)的基礎(chǔ),tensor的實(shí)現(xiàn)和應(yīng)用對(duì)機(jī)器學(xué)習(xí)的重要性不言而喻。在pytorch中實(shí)現(xiàn)了Tensor的功能,現(xiàn)在小編就對(duì)Tensor常用操作有哪些進(jìn)行一個(gè)簡單的歸納,希望能讓小伙伴在學(xué)習(xí)中有所感悟。

Tensor學(xué)習(xí)導(dǎo)圖

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])
#等價(jià)于
y = torch.FloatTensor([1,2,3])#32位浮點(diǎn)型
#后者聲明打開梯度
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ī)采用,高斯噪聲點(diǎn),而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、矩陣計(jì)算

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、維度變化

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

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

小結(jié)

以上就是Tensor常用操作有哪些的全部內(nèi)容。是小編的個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持W3Cschool。


0 人點(diǎn)贊