pytorch--之halfTensor的使用详解


Posted in Python onMay 24, 2021

pytorch--之halfTensor的使用详解

证明出错在dataloader里面

在pytorch当中,float16和half是一样的数据结构,都是属于half操作,

然后dataloader不能返回half值,所以在dataloader里面,要把float16改成float32即可返回

补充:Pytorch中Tensor常用操作归纳

对常用的一些Tensor的常用操作进行简单归纳,方便日后查询。后续有用到再补充。

pytorch--之halfTensor的使用详解

1、创建Tensor

import torch
#经典方式
device = torch.device("cuda:0")
x = torch.tensor([1,2],dtype = torch.float32,device = device,requires_grad=True)
w = sum(2 * x)
w.backward()
print(x.device)
print(x.dtype)
print(x.grad)
#Tensor
y = torch.Tensor([1,2,3])
#等价于
y = torch.FloatTensor([1,2,3])#32位浮点型
#后者声明打开梯度
y.requires_grad = True
#还有其他类型,常用的
torch.LongTensor(2,3)
torch.shortTensor(2,3)
torch.IntTensor(2,3)
w = sum(2 * y)
w.backward()
print(y.grad)
print(y.dtype)

输出:

cuda:0
torch.float32
tensor([2., 2.], device='cuda:0')
tensor([2., 2., 2.])
torch.float32

和numpy类似的创建方法

x = torch.linspace(1,10,10,dtype = torch.float32,requires_grad = True)
y = torch.ones(10)
z = torch.zeros((2,4))
w = torch.randn((2,3))#从标准正态分布(均值为0,方差为1)上随机采用,高斯噪声点,而rand相当于在0,1间随机采样
#torch.normal()????
print(x)
print(y)
print(z)
print(w)

输出

tensor([ 1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.], requires_grad=True)
tensor([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.]])
tensor([[-0.6505,  1.3897,  2.2265],
        [-1.7815, -1.8194, -0.4143]])

从numpy转换

np_data = np.arange(2,13,2).reshape((2,3))
torch_data = torch.from_numpy(np_data)#numpy转tensor
print('\nnumpy',np_data)
print('\ntorch',torch_data)

输出

numpy [[ 2  4  6]
 [ 8 10 12]]

torch tensor([[ 2,  4,  6],
        [ 8, 10, 12]], dtype=torch.int32)

2、组合

import torch
x = torch.arange(0,10,1).reshape(2,-1)#size=(2,5)
y = torch.ones(10).reshape(2,-1)#size=(2,5)
print(x)
print(y)
w = torch.cat((x,y),dim = 0)#默认从size最左边开始,这里结果为:(2+2,5)
z = torch.cat((x,y),dim = 1)#(2,5+5)
print(w,w.size())
print(z,z.size())
#还有种stack()

输出:

tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
tensor([[0., 1., 2., 3., 4.],
        [5., 6., 7., 8., 9.],
        [1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]]) torch.Size([4, 5])
tensor([[0., 1., 2., 3., 4., 1., 1., 1., 1., 1.],
        [5., 6., 7., 8., 9., 1., 1., 1., 1., 1.]]) torch.Size([2, 10])

3、数据类型转换

法一

x = torch.rand((2,2),dtype = torch.float32)
print(x.dtype)
x = x.double()
print(x.dtype)
x = x.int()
print(x)

输出:

torch.float32
torch.float64
tensor([[0, 0],
        [0, 0]], dtype=torch.int32)

法二

x = torch.LongTensor((2,2))
print(x.dtype)
x = x.type(torch.float32)
print(x.dtype)

输出:

torch.int64
torch.float32

4、矩阵计算

x = torch.arange(0,4,1).reshape(2,-1)
print(x)
print(x * x )#直接相乘
print(torch.mm(x,x))#矩阵乘法
print(x + 1)#广播
print(x.numpy())#转换成numpy

输出:

tensor([[0, 1],
        [2, 3]])
tensor([[0, 1],
        [4, 9]])
tensor([[ 2,  3],
        [ 6, 11]])
tensor([[1, 2],
        [3, 4]])
[[0 1]
 [2 3]]

5、维度变化

主要是对维度大小为1的升降维操作。

torch.squeeze(input)#去掉维度为1的维数
 torch.unsqueeze(input,dim)#指定位置增加一维

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

Python 相关文章推荐
Python_LDA实现方法详解
Oct 25 Python
Python在不同目录下导入模块的实现方法
Oct 27 Python
机器学习python实战之手写数字识别
Nov 01 Python
python安装twisted的问题解析
Aug 21 Python
Python使用字典的嵌套功能详解
Feb 27 Python
详解Python图像处理库Pillow常用使用方法
Sep 02 Python
python实现大学人员管理系统
Oct 25 Python
手动安装python3.6的操作过程详解
Jan 13 Python
python 用Matplotlib作图中有多个Y轴
Nov 28 Python
python excel多行合并的方法
Dec 09 Python
用Python写一个简易版弹球游戏
Apr 13 Python
Django模型层实现多表关系创建和多表操作
Jul 21 Python
pandas DataFrame.shift()函数的具体使用
May 24 #Python
教你怎么用python实现字符串转日期
May 24 #Python
pandas中DataFrame重置索引的几种方法
May 24 #Python
pandas取dataframe特定行列的实现方法
pytorch 如何使用amp进行混合精度训练
只需要这一行代码就能让python计算速度提高十倍
pytorch 如何使用float64训练
You might like
Zerg建筑一览
2020/03/14 星际争霸
php URL编码解码函数代码
2009/03/10 PHP
用php或asp创建网页桌面快捷方式的代码
2010/03/23 PHP
PHP FATAL ERROR: CALL TO UNDEFINED FUNCTION BCMUL()解决办法
2014/05/04 PHP
PHP共享内存用法实例分析
2016/02/12 PHP
基于jQuery实现表格数据的动态添加与统计的代码
2011/01/31 Javascript
jQuery操作Select选择的Text和Value(获取/设置/添加/删除)
2013/03/06 Javascript
自己编写的类似JS的trim方法
2013/10/09 Javascript
js 用CreateElement动态创建标签示例
2013/11/20 Javascript
使用AngularJS处理单选框和复选框的简单方法
2015/06/19 Javascript
学习JavaScript设计模式(单例模式)
2015/11/26 Javascript
手动初始化Angular的模块与控制器
2016/12/26 Javascript
简单的渐变轮播插件
2017/01/12 Javascript
JavaScript中 DOM操作方法小结
2017/04/25 Javascript
nodejs后台集成ueditor富文本编辑器的实例
2017/07/11 NodeJs
在 Angular6 中使用 HTTP 请求服务端数据的步骤详解
2018/08/06 Javascript
解决vue 引入子组件报错的问题
2018/09/06 Javascript
vue实现局部刷新的实现示例
2019/04/16 Javascript
Element DateTimePicker日期时间选择器的使用示例
2020/07/27 Javascript
python+mysql实现简单的web程序
2014/09/11 Python
Python中使用Flask、MongoDB搭建简易图片服务器
2015/02/04 Python
九步学会Python装饰器
2015/05/09 Python
Python2实现的LED大数字显示效果示例
2017/09/04 Python
python取代netcat过程分析
2018/02/10 Python
浅谈Python编程中3个常用的数据结构和算法
2019/04/30 Python
如何解决django-celery启动后迅速关闭
2019/10/16 Python
Windows10下Tensorflow2.0 安装及环境配置教程(图文)
2019/11/21 Python
python自动化办公操作PPT的实现
2021/02/05 Python
使用phonegap进行提示操作的具体方法
2017/03/30 HTML / CSS
The Hut德国站点:时装、家居用品、美容等
2016/09/23 全球购物
实习单位推荐信范文
2013/11/27 职场文书
财产公证书格式
2014/04/10 职场文书
超市创意活动方案
2014/08/15 职场文书
女人创业励志语录,句句蕴含能量,激发你的潜能
2019/08/20 职场文书
Spring Cache和EhCache实现缓存管理方式
2021/06/15 Java/Android
JavaScript 与 TypeScript之间的联系
2021/11/27 Javascript