pytorch--之halfTensor的使用详解

 更新时间:2021年05月24日 14:36:11   作者:zxyhhjs2017  
这篇文章主要介绍了pytorch--之halfTensor的使用详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

证明出错在dataloader里面

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

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

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

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

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)#指定位置增加一维

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

相关文章

  • django框架F&Q 聚合与分组操作示例

    django框架F&Q 聚合与分组操作示例

    这篇文章主要介绍了django框架F&Q 聚合与分组操作,结合实例形式详细分析了Django框架查询条件取对象中某列值、构建搜索条件以及聚合查询等相关操作技巧,需要的朋友可以参考下
    2019-12-12
  • PyQt5多线程防卡死和多窗口用法的实现

    PyQt5多线程防卡死和多窗口用法的实现

    这篇文章主要介绍了PyQt5多线程防卡死和多窗口用法的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • 关于Python中*args和**kwargs的深入理解

    关于Python中*args和**kwargs的深入理解

    这篇文章主要给大家介绍了关于Python中*args和**kwargs的相关资料,*args和**kwargs代表的是变量, 变量前面的 *(星号)才是必须的,也可以写成*v和**vs;写成*args和**kwargs只是一个常用的书写方式,需要的朋友可以参考下
    2021-08-08
  • pycharm实现增加运行时内存

    pycharm实现增加运行时内存

    这篇文章主要介绍了pycharm实现增加运行时内存方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python实现绘制双柱状图并显示数值功能示例

    Python实现绘制双柱状图并显示数值功能示例

    这篇文章主要介绍了Python实现绘制双柱状图并显示数值功能,涉及Python数值运算及基于matplotlib的图形绘制相关操作技巧,需要的朋友可以参考下
    2018-06-06
  • Django搭建项目实战与避坑细节详解

    Django搭建项目实战与避坑细节详解

    这篇文章主要给大家介绍了关于Django搭建项目实战与避坑细节的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Python 生成 -1~1 之间的随机数矩阵方法

    Python 生成 -1~1 之间的随机数矩阵方法

    今天小编就为大家分享一篇Python 生成 -1~1 之间的随机数矩阵方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 解决pycharm 远程调试 上传 helpers 卡住的问题

    解决pycharm 远程调试 上传 helpers 卡住的问题

    今天小编就为大家分享一篇解决pycharm 远程调试 上传 helpers 卡住的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-06-06
  • CentOS中安装python3.8.2的详细教程

    CentOS中安装python3.8.2的详细教程

    这篇文章主要介绍了CentOS中安装python3.8.2的详细教程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-03-03
  • Python中for循环详解

    Python中for循环详解

    这篇文章主要介绍了Python中for循环,有需要的朋友可以参考一下
    2014-01-01

最新评论