pytorch--之halfTensor的使用詳解

證明出錯在dataloader里面
在pytorch當(dāng)中,float16和half是一樣的數(shù)據(jù)結(jié)構(gòu),都是屬于half操作,
然后dataloader不能返回half值,所以在dataloader里面,要把float16改成float32即可返回
補充:Pytorch中Tensor常用操作歸納
對常用的一些Tensor的常用操作進(jìn)行簡單歸納,方便日后查詢。后續(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))#從標(biāo)準(zhǔn)正態(tài)分布(均值為0,方差為1)上隨機采用,高斯噪聲點,而rand相當(dāng)于在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)#默認(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)#指定位置增加一維
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Python中*args和**kwargs的深入理解
這篇文章主要給大家介紹了關(guān)于Python中*args和**kwargs的相關(guān)資料,*args和**kwargs代表的是變量, 變量前面的 *(星號)才是必須的,也可以寫成*v和**vs;寫成*args和**kwargs只是一個常用的書寫方式,需要的朋友可以參考下2021-08-08
Python實現(xiàn)繪制雙柱狀圖并顯示數(shù)值功能示例
這篇文章主要介紹了Python實現(xiàn)繪制雙柱狀圖并顯示數(shù)值功能,涉及Python數(shù)值運算及基于matplotlib的圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2018-06-06
Django搭建項目實戰(zhàn)與避坑細(xì)節(jié)詳解
這篇文章主要給大家介紹了關(guān)于Django搭建項目實戰(zhàn)與避坑細(xì)節(jié)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python 生成 -1~1 之間的隨機數(shù)矩陣方法
今天小編就為大家分享一篇Python 生成 -1~1 之間的隨機數(shù)矩陣方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
解決pycharm 遠(yuǎn)程調(diào)試 上傳 helpers 卡住的問題
今天小編就為大家分享一篇解決pycharm 遠(yuǎn)程調(diào)試 上傳 helpers 卡住的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06

