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)

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

相关文章

功能概要:(目前已实现功能)公共展示部分:1.网站首页展示...
大体上把Python中的数据类型分为如下几类: Number(数字) ...
开发之前第一步,就是构造整个的项目结构。这就好比作一幅画...
源码编译方式安装Apache首先下载Apache源码压缩包,地址为ht...
前面说完了此项目的创建及数据模型设计的过程。如果未看过,...
python中常用的写爬虫的库有urllib2、requests,对于大多数比...