在實(shí)際計算過程中,float類型使用最多,因此這里重點(diǎn)介紹numpy和torch數(shù)據(jù)float類型轉(zhuǎn)化遇到的問題,其他類型同理。
numpy使用astype轉(zhuǎn)化數(shù)據(jù)類型,float默認(rèn)轉(zhuǎn)化為64位,可以使用np.float32指定為32位
#numpy轉(zhuǎn)化float類型 a= np.array([1,2,3]) a = a.astype(np.float) print(a) print(a.dtype)
[1. 2. 3.]
float64
不要使用a.dtype指定數(shù)據(jù)類型,會使數(shù)據(jù)丟失
#numpy轉(zhuǎn)化float類型 b= np.array([1,2,3]) b.dtype= np.float32 print(b) print(b.dtype)
[1.e-45 3.e-45 4.e-45]
float32
不要用float代替np.float,否則可能出現(xiàn)意想不到的錯誤
不能從np.float64位轉(zhuǎn)化np.float32,會報錯
np.float64與np.float32相乘,結(jié)果為np.float64
在實(shí)際使用過程中,可以指定為np.float,也可以指定具體的位數(shù),如np.float,不過直接指定np.float更方便。
torch使用torch.float()轉(zhuǎn)化數(shù)據(jù)類型,float默認(rèn)轉(zhuǎn)化為32位,torch中沒有torch.float64()這個方法
# torch轉(zhuǎn)化float類型 b = torch.tensor([4,5,6]) b = b.float() b.dtype
torch.float32
np.float64使用torch.from_numpy轉(zhuǎn)化為torch后也是64位的
print(a.dtype) c = torch.from_numpy(a) c.dtype
float64
torch.float64
不要用float代替torch.float,否則可能出現(xiàn)意想不到的錯誤
torch.float32與torch.float64數(shù)據(jù)類型相乘會出錯,因此相乘的時候注意指定或轉(zhuǎn)化數(shù)據(jù)float具體類型
np和torch數(shù)據(jù)類型轉(zhuǎn)化大體原理一樣,只有相乘的時候,torch.float不一致不可相乘,np.float不一致可以相乘,并且轉(zhuǎn)化為np.float64
tensor轉(zhuǎn)化為numpy
import torch b = torch.tensor([4.0,6]) # b = b.float() print(b.dtype) c = b.numpy() print(c.dtype)
torch.int64
int64
numpy轉(zhuǎn)化為tensor
import torch import numpy as np b= np.array([1,2,3]) # b = b.astype(np.float) print(b.dtype) c = torch.from_numpy(b) print(c.dtype)
int32
torch.int32
可以看到,torch默認(rèn)int型是64位的,numpy默認(rèn)int型是32位的
補(bǔ)充:torch.from_numpy VS torch.Tensor
最近在造dataset的時候,突然發(fā)現(xiàn),在輸入圖像轉(zhuǎn)tensor的時候,我可以用torch.Tensor直接強(qiáng)制轉(zhuǎn)型將numpy類轉(zhuǎn)成tensor類,也可以用torch.from_numpy這個方法將numpy類轉(zhuǎn)換成tensor類,那么,torch.Tensor和torch.from_numpy這兩個到底有什么區(qū)別呢?既然torch.Tensor能搞定,那torch.from_numpy留著不就是冗余嗎?
有區(qū)別,使用torch.from_numpy更加安全,使用tensor.Tensor在非float類型下會與預(yù)期不符。
實(shí)際上,兩者的區(qū)別是大大的。打個不完全正確的比方說,torch.Tensor就如同c的int,torch.from_numpy就如同c++的static_cast,我們都知道,如果將int64強(qiáng)制轉(zhuǎn)int32,只要是高位轉(zhuǎn)低位,一定會出現(xiàn)高位被抹去的隱患的,不僅僅可能會丟失精度,甚至?xí)?fù)對調(diào)。
這里的torch.Tensor與torch.from_numpy也會存在同樣的問題。
看看torch.Tensor的文檔,里面清楚地說明了,
torch.Tensor is an alias for the default tensor type (torch.FloatTensor).
而torch.from_numpy的文檔則是說明,
The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa. The returned tensor is not resizable.
也即是說,
1、當(dāng)轉(zhuǎn)換的源是float類型,torch.Tensor與torch.from_numpy會共享一塊內(nèi)存!且轉(zhuǎn)換后的結(jié)果的類型是torch.float32
2、當(dāng)轉(zhuǎn)換的源不是float類型,torch.Tensor得到的是torch.float32,而torch.from_numpy則是與源類型一致!
import torch import numpy as nps1 = np.arange(10, dtype=np.float32) s2 = np.arange(10) # 默認(rèn)的dtype是int64# 例一 o11 = torch.Tensor(s1) o12 = torch.from_numpy(s1) o11.dtype # torch.float32 o12.dtype # torch.float32 # 修改值 o11[0] = 12 o12[0] # tensor(12.)# 例二 o21 = torch.Tensor(s2) o22 = torch.from_numpy(s2) o21.dtype # torch.float32 o22.dtype # torch.int64 # 修改值 o21[0] = 12 o22[0] # tensor(0)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
標(biāo)簽:銅川 潛江 黑龍江 株洲 常德 通遼 呂梁 阿里
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決numpy和torch數(shù)據(jù)類型轉(zhuǎn)化的問題》,本文關(guān)鍵詞 解決,numpy,和,torch,數(shù)據(jù),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。