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

PyTorch上的常用数据类型如下

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中的数据类型和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)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。