日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pytorch關(guān)于Tensor的數(shù)據(jù)類型說明_python

作者:煙火笑風(fēng)塵 ? 更新時(shí)間: 2022-09-08 編程語言

關(guān)于Tensor的數(shù)據(jù)類型說明

1. 32位浮點(diǎn)型:torch.FloatTensor

a=torch.Tensor( [[2,3],[4,8],[7,9]], )
print "a:",a
print "a.size():",a.size()
print "a.dtype:",a.dtype
 
b=torch.FloatTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype

可以看出?torch.FloatTensor 是32位float類型,并且torch.Tensor默認(rèn)的數(shù)據(jù)類型是32位float類型。

2. 64位浮點(diǎn)型:torch.DoubleTensor

b=torch.DoubleTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype

?3. 16位整型:torch.ShortTensor

b=torch.ShortTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype

4.? 32位整型:torch.IntTensor

b=torch.IntTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype

5. 64位整型:torch.LongTensor

b=torch.LongTensor( [[2,3],[4,8],[7,9]] )
print "b:",b
print "b.shape:",b.shape
print "b.dtype:",b.dtype

6. 快速創(chuàng)建Tensor

(1)?torch.zeros()

a=torch.zeros( size=(4,5),dtype=torch.float32 )
print a
print a.shape
print a.dtype

(2)?torch.randn()

a=torch.randn( size=(4,5),dtype=torch.float32 )
print a
print a.shape
print a.dtype

7. Tensor索引方式,參考numpy

8. Tensor和numpy數(shù)組轉(zhuǎn)換:

(1) Tensor轉(zhuǎn)numpy,

a=torch.randn( size=(4,5),dtype=torch.float32 )
print a
print a.shape
print a.dtype
 
b= a.numpy()
print b
print b.shape
print b.dtype

(2) numpy轉(zhuǎn)Tensor,

a=np.random.randn(4,3)
print a
print a.shape
print a.dtype
 
b=torch.from_numpy( a )
print b
print b.shape
print b.dtype

9.更改Tensor的數(shù)據(jù)類型,

a=torch.FloatTensor( (3,2) )
print a
print a.shape
print a.dtype
 
a.int()
print a
print a.shape
print a.dtype

10. GPU加速,如果pytorch支持GPU加速,可以加Tensor放到GPU執(zhí)行,

if torch.cuda.is_available():
    a_cuda = a.cuda()

pytorch Tensor變形函數(shù)

view(), resize(), reshape() 在不改變?cè)璽ensor數(shù)據(jù)的情況下修改tensor的形狀,前后要求元素總數(shù)一致,且前后tensor共享內(nèi)存

如果想要直接改變Tensor的尺寸,可以使用resize_()的原地操作函數(shù)。

在resize_()函數(shù)中,如果超過了原Tensor的大小則重新分配內(nèi)存,多出部分置0,如果小于原Tensor大小則剩余的部分仍然會(huì)隱藏保留。

transpose()函數(shù)可以將指定的兩個(gè)維度的元素進(jìn)行轉(zhuǎn)置,而permute()函數(shù)則可以按照給定的維度進(jìn)行維度變換。

在實(shí)際的應(yīng)用中,經(jīng)常需要增加或減少Tensor的維度,尤其是維度為1的情況,這時(shí)候可以使用squeeze()與unsqueeze()函數(shù),前者用于去除size為1的維度,而后者則是將指定的維度的size變?yōu)?。

有時(shí)需要采用復(fù)制元素的形式來擴(kuò)展Tensor的維度,這時(shí)expand就派上用場(chǎng)了。

expand()函數(shù)將size為1的維度復(fù)制擴(kuò)展為指定大小,也可以使用expand_as()函數(shù)指定為示例Tensor的維度。

注意:在進(jìn)行Tensor操作時(shí),有些操作如transpose()、permute()等可能會(huì)把Tensor在內(nèi)存中變得不連續(xù),而有些操作如view()等是需要Tensor內(nèi)存連續(xù)的,這種情況下需要使用contiguous()操作先將內(nèi)存變?yōu)檫B續(xù)的。在PyTorch v0.4版本中增加了reshape()操作,可以看做是Tensor.contiguous().view()

Tensor的排序與取極值

排序函數(shù)sort(),選擇沿著指定維度進(jìn)行排序,返回排序后的Tensor及對(duì)應(yīng)的索引位置。

max()與min()函數(shù)則是沿著指定維度選擇最大與最小元素,返回該元素及對(duì)應(yīng)的索引位置。

Tensor與NumPy轉(zhuǎn)換

Tensor與NumPy可以高效地進(jìn)行轉(zhuǎn)換,并且轉(zhuǎn)換前后的變量共享內(nèi)存。在進(jìn)行PyTorch不支持的操作時(shí),甚至可以曲線救國(guó),將Tensor轉(zhuǎn)換為NumPy類型,操作后再轉(zhuǎn)為Tensor。

原文鏈接:https://blog.csdn.net/moshiyaofei/article/details/89703161

欄目分類
最近更新