pytorch常用數(shù)據(jù)類型所占字節(jié)數(shù)對照表一覽
PyTorch上的常用數(shù)據(jù)類型如下
| Data type | dtype | CPU tensor | GPU tensor | Size/bytes |
|---|---|---|---|---|
| 32-bit floating | torch.float32 or torch.float | torch.FloatTensor | torch.cuda.FloatTensor | 4 |
| 64-bit floating | torch.float64 or torch.double | torch.DoubleTensor | torch.cuda.DoubleTensor | 8 |
| 16-bit floating | torch.float16or torch.half | torch.HalfTensor | torch.cuda.HalfTensor | - |
| 8-bit integer (unsigned) | torch.uint8 | torch.ByteTensor | torch.cuda.ByteTensor | 1 |
| 8-bit integer (signed) | torch.int8 | torch.CharTensor | torch.cuda.CharTensor | - |
| 16-bit integer (signed) | torch.int16or torch.short | torch.ShortTensor | torch.cuda.ShortTensor | 2 |
| 32-bit integer (signed) | torch.int32 or torch.int | torch.IntTensor | torch.cuda.IntTensor | 4 |
| 64-bit integer (signed) | torch.int64 or torch.long | torch.LongTensor | torch.cuda.LongTensor | 8 |
以上PyTorch中的數(shù)據(jù)類型和numpy中的相對應(yīng),占用字節(jié)大小也是一樣的
補(bǔ)充:pytorch tensor比較大小 數(shù)據(jù)類型要注意
如下
a = torch.tensor([[0, 0], [0, 0]]) print(a>=0.5)
輸出
tensor([[1, 1],
[1, 1]], dtype=torch.uint8)
結(jié)果明顯不對, 分析原因是因?yàn)? a是long類型, 而0.5是float. 0.5會被轉(zhuǎn)化為 long, 變?yōu)?. 因此結(jié)果會出錯, 做出如下修改就可以得到正確答案
正確用法:
a = torch.tensor([[0, 0], [0, 0]]).float() print(a>=0.5)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python面向?qū)ο笾^承和組合用法實(shí)例分析
這篇文章主要介紹了Python面向?qū)ο笾^承和組合用法,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中組合與繼承的相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
Python?nonlocal關(guān)鍵字?與?global?關(guān)鍵字解析
這篇文章主要介紹了Python?nonlocal關(guān)鍵字?與?global?關(guān)鍵字解析,nonlocal關(guān)鍵字用來在函數(shù)或其他作用域中使用外層變量,global關(guān)鍵字用來在函數(shù)或其他局部作用域中使用全局變量,更多香瓜內(nèi)容需要的小伙伴可以參考一下2022-03-03
Python3 pickle對象串行化代碼實(shí)例解析
這篇文章主要介紹了Python3 pickle對象串行化代碼實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
Python虛擬環(huán)境庫virtualenvwrapper安裝及使用
這篇文章主要介紹了Python虛擬環(huán)境庫virtualenvwrapper安裝及使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python開發(fā)虛擬環(huán)境使用virtualenvwrapper的搭建步驟教程圖解
virtualenvwrapper是用來管理virtualenv的擴(kuò)展包,用著很方便。這篇文章主要介紹了Python開發(fā)虛擬環(huán)境使用virtualenvwrapper的搭建步驟 ,需要的朋友可以參考下2018-09-09

