序號 | 數據類型及描述 |
---|---|
1. | bool_存儲為一個字節的布爾值(真或假) |
2. | int_默認整數,相當于 C 的long,通常為int32或int64 |
3. | intc相當于 C 的int,通常為int32或int64 |
4. | intp用于索引的整數,相當于 C 的size_t,通常為int32或int64 |
5. | int8字節(-128 ~ 127) |
6. | int1616 位整數(-32768 ~ 32767) |
7. | int3232 位整數(-2147483648 ~ 2147483647) |
8. | int6464 位整數(-9223372036854775808 ~ 9223372036854775807) |
9. | uint88 位無符號整數(0 ~ 255) |
10. | uint1616 位無符號整數(0 ~ 65535) |
11. | uint3232 位無符號整數(0 ~ 4294967295) |
12. | uint6464 位無符號整數(0 ~ 18446744073709551615) |
13. | float_float64的簡寫 |
14. | float16半精度浮點:符號位,5 位指數,10 位尾數 |
15. | float32單精度浮點:符號位,8 位指數,23 位尾數 |
16. | float64雙精度浮點:符號位,11 位指數,52 位尾數 |
17. | complex_complex128的簡寫 |
18. | complex64復數,由兩個 32 位浮點表示(實部和虛部) |
19. |
complex128復數,由兩個 64 位浮點表示(實部和虛部) |
直接使用類型名很可能會報錯,正確的使用方式是np.調用,eg, np.uint8
Torch定義了七種CPU張量類型和八種GPU張量類型,這里我們就只講解一下CPU中的,其實GPU中只是中間加一個cuda即可,如torch.cuda.FloatTensor:
torch.FloatTensor(2,3) 構建一個2*3 Float類型的張量
torch.DoubleTensor(2,3) 構建一個2*3 Double類型的張量
torch.ByteTensor(2,3) 構建一個2*3 Byte類型的張量
torch.CharTensor(2,3) 構建一個2*3 Char類型的張量
torch.ShortTensor(2,3) 構建一個2*3 Short類型的張量
torch.IntTensor(2,3) 構建一個2*3 Int類型的張量
torch.LongTensor(2,3) 構建一個2*3 Long類型的張量
同樣,直接使用類型名很可能會報錯,正確的使用方式是torch.調用,eg,torch.FloatTensor()
type函數可以由變量調用,或者把變量作為參數傳入。
返回的是該變量的類型,而非數據類型。
data = np.random.randint(0, 255, 300) print(type(data))
輸出
class 'numpy.ndarray'>
返回值為變量的數據類型
t_out = torch.Tensor(1,2,3) print(t_out.dtype)
輸出
torch.float32
t_out = torch.Tensor(1,2,3)
print(t_out.numpy().dtype)
輸出
float32
為了實施trochvision.transforms.ToPILImage()函數
于是我想從numpy的ndarray類型轉成PILImage類型
我做了以下嘗試
data = np.random.randint(0, 255, 300) n_out = data.reshape(10,10,3) print(n_out.dtype) img = transforms.ToPILImage()(n_out) img.show()
但是很遺憾,報錯了
raise TypeError('Input type {} is not supported'.format(npimg.dtype))
TypeError: Input type int32 is not supported
因為要將ndarray轉成PILImage要求ndarray是uint8類型的。
于是我認輸了。。。
使用了
n_out = np.linspace(0,255,300,dtype=np.uint8) n_out = n_out.reshape(10,10,3) print(n_out.dtype) img = torchvision.transforms.ToPILImage()(n_out) img.show()
得到了輸出
uint8
嗯,顯示了一張圖片
但是呢,就很憋屈,和想要的隨機數效果不一樣。
于是我用了astype函數
由變量調用,但是直接調用不會改變原變量的數據類型,是返回值是改變類型后的新變量,所以要賦值回去。
n_out = n_out.astype(np.uint8)
#初始化隨機數種子 np.random.seed(0) data = np.random.randint(0, 255, 300) print(data.dtype) n_out = data.reshape(10,10,3) #強制類型轉換 n_out = n_out.astype(np.uint8) print(n_out.dtype) img = transforms.ToPILImage()(n_out) img.show()
int32
uint8
pytorch中沒有astype函數,正確的轉換方法是
tensor = torch.Tensor(3, 5)
torch.long() 將tensor投射為long類型
newtensor = tensor.long()
torch.half()將tensor投射為半精度浮點類型
newtensor = tensor.half()
torch.int()將該tensor投射為int類型
newtensor = tensor.int()
torch.double()將該tensor投射為double類型
newtensor = tensor.double()
torch.float()將該tensor投射為float類型
newtensor = tensor.float()
torch.char()將該tensor投射為char類型
newtensor = tensor.char()
torch.byte()將該tensor投射為byte類型
newtensor = tensor.byte()
torch.short()將該tensor投射為short類型
newtensor = tensor.short()
同樣,和numpy中的astype函數一樣,是返回值才是改變類型后的結果,調用的變量類型不變
type(new_type=None, async=False)如果未提供new_type,則返回類型,否則將此對象轉換為指定的類型。 如果已經是正確的類型,則不會執行且返回原對象。
用法如下:
self = torch.LongTensor(3, 5) # 轉換為其他類型 print self.type(torch.FloatTensor)
如果張量已經是正確的類型,則不會執行操作。具體操作方法如下:
self = torch.Tensor(3, 5) tesnor = torch.IntTensor(2,3) print self.type_as(tesnor)
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。