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


Posted in Python onMay 17, 2021

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)

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

Python 相关文章推荐
巧用Python装饰器 免去调用父类构造函数的麻烦
May 18 Python
python psutil库安装教程
Mar 19 Python
python判断设备是否联网的方法
Jun 29 Python
对python cv2批量灰度图片并保存的实例讲解
Nov 09 Python
Python定义函数功能与用法实例详解
Apr 08 Python
解决pycharm 远程调试 上传 helpers 卡住的问题
Jun 27 Python
python二元表达式用法
Dec 04 Python
OpenCV 表盘指针自动读数的示例代码
Apr 10 Python
解决Jupyter notebook更换主题工具栏被隐藏及添加目录生成插件问题
Apr 20 Python
python 模拟登录B站的示例代码
Dec 15 Python
python字典进行运算原理及实例分享
Aug 02 Python
Python NumPy灰度图像的压缩原理讲解
Aug 04 Python
python使用tkinter实现透明窗体上绘制随机出现的小球(实例代码)
Python编写可视化界面的全过程(Python+PyCharm+PyQt)
Pytorch 实现变量类型转换
Python进度条的使用
May 17 #Python
Python包管理工具pip的15 个使用小技巧
Python中json.dumps()函数的使用解析
May 17 #Python
Python中threading库实现线程锁与释放锁
You might like
创建配置文件 用PHP写出自己的BLOG系统 2
2010/04/12 PHP
php数据库操作model类(使用__call方法)
2016/11/16 PHP
javascript得到XML某节点的子节点个数的脚本
2008/10/11 Javascript
jquery弹出关闭遮罩层实例
2013/08/06 Javascript
从数组中随机取x条不重复数据的JS代码
2013/12/24 Javascript
javascript获取文档坐标和视口坐标
2015/05/26 Javascript
关于function类中定义变量this的简单说明
2016/05/28 Javascript
JavaScript中ES6字符串扩展方法
2016/08/26 Javascript
实例浅析js的this
2016/12/11 Javascript
Bootstrap3 模态框使用实例
2017/02/22 Javascript
基于DOM节点删除之empty和remove的区别(详解)
2017/09/11 Javascript
Vue+SpringBoot开发V部落博客管理平台
2017/12/27 Javascript
ES6基础之默认参数值
2019/02/21 Javascript
vue-router的钩子函数用法实例分析
2019/10/26 Javascript
在Vue中实现随hash改变响应菜单高亮
2020/03/09 Javascript
uniapp 仿微信的右边下拉选择弹出框的实现代码
2020/07/12 Javascript
JavaScript实现无限轮播效果
2020/11/19 Javascript
JavaScript实现筛选数组
2021/03/02 Javascript
python中bisect模块用法实例
2014/09/25 Python
jupyter notebook引用from pyecharts.charts import Bar运行报错
2020/04/23 Python
详解python中的装饰器
2018/07/10 Python
使用tensorflow实现线性回归
2018/09/08 Python
手把手教你如何安装Pycharm(详细图文教程)
2018/11/28 Python
python3.6使用SMTP协议发送邮件
2020/05/20 Python
Django实现文章详情页面跳转代码实例
2020/09/16 Python
python unichr函数知识点总结
2020/12/16 Python
Python 带星号(* 或 **)的函数参数详解
2021/02/23 Python
全球第二大家装零售商:Lowe’s
2018/01/13 全球购物
俄罗斯儿童和青少年服装、鞋子及配件的在线商店:Orby
2020/02/20 全球购物
声明struct x1 { . . . }; 和typedef struct { . . . }x2;有什么不同
2012/06/02 面试题
与UNIX有关的几个名词
2015/09/17 面试题
新郎新娘婚礼答谢词
2014/01/11 职场文书
小学生寒假家长评语
2014/04/16 职场文书
求职自我评价范文100字
2014/09/23 职场文书
2014年中学生检讨书大全
2014/10/09 职场文书
单位租房协议范本
2014/12/03 职场文书