pytorch--之halfTensor的使用詳解
證明出錯在dataloader里面
在pytorch當中,float16和half是一樣的數(shù)據(jù)結(jié)構(gòu),都是屬于half操作,
然后dataloader不能返回half值,所以在dataloader里面,要把float16改成float32即可返回
補充:Pytorch中Tensor常用操作歸納
對常用的一些Tensor的常用操作進行簡單歸納,方便日后查詢。后續(xù)有用到再補充。
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))#從標準正態(tài)分布(均值為0,方差為1)上隨機采用,高斯噪聲點,而rand相當于在0,1間隨機采樣 #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('\nnumpy',np_data) print('\ntorch',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)#默認從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)#指定位置增加一維
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python 使用 PyQt5 開發(fā)的關(guān)機小工具分享
這篇文章主要介紹了Python 使用 PyQt5 開發(fā)的關(guān)機小工具分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07教你漂亮打印Pandas DataFrames和Series
在今天的文章中,我們將探討如何配置所需的pandas選項,這些選項將使我們能夠“漂亮地打印” pandas DataFrames,需要的朋友可以參考下2021-05-05Python3.6 中的pyinstaller安裝和使用教程
這篇文章主要介紹了Python3.6 中的pyinstaller安裝和使用的教程,本文給大家介紹的非常詳細,對大家的工作或?qū)W習具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Python如何存儲和讀取ASCII碼形式的byte數(shù)據(jù)
這篇文章主要介紹了Python如何存儲和讀取ASCII碼形式的byte數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05