校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁(yè) > 知識(shí)庫(kù) > pytorch--之halfTensor的使用詳解

pytorch--之halfTensor的使用詳解

熱門標(biāo)簽:河北防封卡電銷卡 天津電話機(jī)器人公司 開(kāi)封自動(dòng)外呼系統(tǒng)怎么收費(fèi) 應(yīng)電話機(jī)器人打電話違法嗎 電銷機(jī)器人的風(fēng)險(xiǎn) 地圖標(biāo)注線上如何操作 400電話辦理哪種 手機(jī)網(wǎng)頁(yè)嵌入地圖標(biāo)注位置 開(kāi)封語(yǔ)音外呼系統(tǒng)代理商

證明出錯(cuò)在dataloader里面

在pytorch當(dāng)中,float16和half是一樣的數(shù)據(jù)結(jié)構(gòu),都是屬于half操作,

然后dataloader不能返回half值,所以在dataloader里面,要把float16改成float32即可返回

補(bǔ)充:Pytorch中Tensor常用操作歸納

對(duì)常用的一些Tensor的常用操作進(jìn)行簡(jiǎn)單歸納,方便日后查詢。后續(xù)有用到再補(bǔ)充。

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)型
#后者聲明打開(kāi)梯度
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('\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最左邊開(kāi)始,這里結(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)#指定位置增加一維

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • pytorch常見(jiàn)的Tensor類型詳解
  • pytorch中tensor張量數(shù)據(jù)類型的轉(zhuǎn)化方式
  • Pytorch基本變量類型FloatTensor與Variable用法

標(biāo)簽:山東 宿遷 六盤水 蘭州 常州 成都 江蘇 駐馬店

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch--之halfTensor的使用詳解》,本文關(guān)鍵詞  pytorch--,之,halfTensor,的,使用,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《pytorch--之halfTensor的使用詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于pytorch--之halfTensor的使用詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 温州市| 都匀市| 娄烦县| 于田县| 河北省| 濮阳市| 娱乐| 嘉峪关市| 湘乡市| 专栏| 卓资县| 镇坪县| 阜平县| 璧山县| 隆尧县| 大洼县| 卢湾区| 东台市| 齐齐哈尔市| 柳江县| 和平县| 泌阳县| 孝感市| 颍上县| 肥西县| 威信县| 惠水县| 项城市| 溧阳市| 清苑县| 五常市| 日土县| 安远县| 博白县| 灵石县| 集安市| 南靖县| 南漳县| 兴业县| 大理市| 巴彦县|