网络编程
位置:首页>> 网络编程>> Python编程>> pytorch常用数据类型所占字节数对照表一览

pytorch常用数据类型所占字节数对照表一览

作者:jingxian  发布时间:2021-07-25 15:26:29 

标签:pytorch,数据类型,占字节数

PyTorch上的常用数据类型如下

Data typedtypeCPU tensorGPU tensorSize/bytes
32-bit floatingtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor4
64-bit floatingtorch.float64 or torch.doubletorch.DoubleTensortorch.cuda.DoubleTensor8
16-bit floatingtorch.float16or torch.halftorch.HalfTensortorch.cuda.HalfTensor-
8-bit integer (unsigned)torch.uint8torch.ByteTensortorch.cuda.ByteTensor1
8-bit integer (signed)torch.int8torch.CharTensortorch.cuda.CharTensor-
16-bit integer (signed)torch.int16or torch.shorttorch.ShortTensortorch.cuda.ShortTensor2
32-bit integer (signed)torch.int32 or torch.inttorch.IntTensortorch.cuda.IntTensor4
64-bit integer (signed)torch.int64 or torch.longtorch.LongTensortorch.cuda.LongTensor8

以上PyTorch中的数据类型和numpy中的相对应,占用字节大小也是一样的

补充:pytorch tensor比较大小 数据类型要注意

如下


a = torch.tensor([[0, 0], [0, 0]])
print(a>=0.5)

输出

tensor([[1, 1],

[1, 1]], dtype=torch.uint8)

结果明显不对, 分析原因是因为, a是long类型, 而0.5是float. 0.5会被转化为 long, 变为0. 因此结果会出错, 做出如下修改就可以得到正确答案

正确用法:


a = torch.tensor([[0, 0], [0, 0]]).float()
print(a>=0.5)

来源:https://blog.csdn.net/yrwang_xd/article/details/106255054

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com