网络编程
位置:首页>> 网络编程>> Python编程>> pytorch--之halfTensor的使用详解

pytorch--之halfTensor的使用详解

作者:zxyhhjs2017  发布时间:2021-08-18 14:44:08 

标签:pytorch,halfTensor

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

来源:https://blog.csdn.net/zxyhhjs2017/article/details/88546423

0
投稿

猜你喜欢

  • 这几天一直困惑我的问题,我觉得pycharm应该也是有的啊,偶然间找到了这个。把那个框里的选中,就可以了。ps:我的是 PyCharm 20
  • 本文实例讲述了Python切片工具pillow用法。分享给大家供大家参考,具体如下:切片:使用切片将源图像分成许多的功能区域因为要对图片进行
  • 前言近几年,制造业作为国民经济主体,是国家创造力、竞争力和综合国力的重要体现。作为制造强国建设的主攻方向,可以说,智能制造发展水平关乎我国未
  • 前言golang实现定时任务很简单,只须要简单几步代码即可以完成,最近在做了几个定时任务,想研究一下它内部是怎么实现的,所以将源码过了一遍,
  • 1998年,W3C发布HTML 4.0 Specification,里面清清楚楚的写了每个标签的用法和语义。搜索引擎的算法参考了W3C的语义
  • jQuery的选择器可谓异常强大,没有什么DOM里的任何数据能逃出它的掌心,这点是我非常喜欢的,以前获取NODE要用getElementBy
  • 引言https://github.com/go-chassis/go-chassis是一个微服务开发框架,而微服务开发框架带来的其中一个课题
  • 本文实例讲述了Python网络编程之TCP套接字简单用法。分享给大家供大家参考,具体如下:上学期学的计算机网络,因为之前还未学习python
  • 如下所示:import pandas as pddata = pd.read_excel('123.xls','Sh
  • 序言哈喽兄弟们,今天来实现一个Python采集视频、弹幕、评论与一体的小软件。平常咱们都是直接代码运行,不过今天我们做成软件,这样的话,咱们
  • 我参与了IE7的开发过程,看到了在IE浏览器中形形色色使用MSXML的方法。显然有一些东西困扰着开发者:MSXML“混乱”的版本以及如何创建
  • 先给大家展示下效果图,大家感觉不错,请参考实现代码:实现原理:点击按钮,往需要动画的div中添加或移除拥有动画效果的class。由于微信小程
  • 很多年以前,面对上古时代遗留的 HTML 发出的腐臭,我捂住鼻子唉声叹气。刚练熟 web 标准的我,恨不得寝其尸食其肉,把一切推翻重来。但经
  • asp定时生成静态HTML的代码,对于缓解服务器压力有很大帮主,需要的朋友可以参考下。<% '判断是否要生成新的HT
  • 一、前言相关知识来自《python算法设计与分析》。初级排序算法是指几种较为基础且容易理解的排序算法。初级排序算法包括插入排序、选择排序和冒
  • 上次帮朋友写过的一个简单切换效果,超级简单,但也比较适用.因为用到了CSS Sprite技术,DEMO中附带了IE6兼容png的JS.核心J
  • 手把手教你实现MYSQL的备份还原示例代码用我比较熟悉的PHP,当然你看完并理解了其中的思路,相信你也可以快速地用你熟悉的语言自己写出来。一
  • 本文实例讲述了Python字符串的全排列算法。分享给大家供大家参考,具体如下:题目描述输入一个字符串,按字典序打印出该字符串中字符的所有排列
  • 我们小组讨论的话题是tab(标签)在使用时的禁忌。在讨论的开始,大家很快产生了六个感兴趣的话题:如何处理海量的tab?在浏览器中关掉tab之
  • 地址:https://youzan.github.io/vant/#/zh-CN/intro一.引入Vant组件库1.首先运行 npm in
手机版 网络编程 asp之家 www.aspxhome.com