欢迎光临
我们一直在努力

pytorch常用数据类型所占字节数是多少

本篇内容介绍了“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)

“pytorch常用数据类型所占字节数是多少”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注云搜网网站,小编将为大家输出更多高质量的实用文章!

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。