pytorch關(guān)于Tensor的數(shù)據(jù)類型說明
關(guān)于Tensor的數(shù)據(jù)類型說明
1. 32位浮點型: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默認的數(shù)據(jù)類型是32位float類型。
2. 64位浮點型: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() 在不改變原tensor數(shù)據(jù)的情況下修改tensor的形狀,前后要求元素總數(shù)一致,且前后tensor共享內(nèi)存

如果想要直接改變Tensor的尺寸,可以使用resize_()的原地操作函數(shù)。
在resize_()函數(shù)中,如果超過了原Tensor的大小則重新分配內(nèi)存,多出部分置0,如果小于原Tensor大小則剩余的部分仍然會隱藏保留。

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


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

有時需要采用復制元素的形式來擴展Tensor的維度,這時expand就派上用場了。
expand()函數(shù)將size為1的維度復制擴展為指定大小,也可以使用expand_as()函數(shù)指定為示例Tensor的維度。

注意:在進行Tensor操作時,有些操作如transpose()、permute()等可能會把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(),選擇沿著指定維度進行排序,返回排序后的Tensor及對應(yīng)的索引位置。
max()與min()函數(shù)則是沿著指定維度選擇最大與最小元素,返回該元素及對應(yīng)的索引位置。


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


以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PyTorch中l(wèi)oading fbgemm.dll異常的解決辦法
PyTorch是一個深度學習框架,當我們在本地調(diào)試大模型時,可能會選用并安裝它,目前已更新至2.4版本,本文給大家介紹了PyTorch中l(wèi)oading fbgemm.dll異常的解決辦法,文中通過代碼和圖文介紹的非常詳細,需要的朋友可以參考下2024-08-08
一行代碼解決動態(tài)執(zhí)行Python函數(shù)方法實例
這篇文章主要為大家介紹了如何用一行代碼解決動態(tài)執(zhí)行Python函數(shù)方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
matplotlib部件之矩形選區(qū)(RectangleSelector)的實現(xiàn)
這篇文章主要介紹了matplotlib部件之矩形選區(qū)(RectangleSelector)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02
pytorch教程之網(wǎng)絡(luò)的構(gòu)建流程筆記
這篇文章主要介紹了pytorch教程中網(wǎng)絡(luò)的構(gòu)建流程,文中附含了詳細的示例代碼流程,有需要的朋友可以借鑒參考下,希望可以有所幫助2021-09-09

