halfTensor的使用是怎样的,常用操作有哪些
Admin 2022-07-09 群英技术资讯 701 次浏览
证明出错在dataloader里面
在pytorch当中,float16和half是一样的数据结构,都是属于half操作,
然后dataloader不能返回half值,所以在dataloader里面,要把float16改成float32即可返回
补充:Pytorch中Tensor常用操作归纳
对常用的一些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)
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])
法一
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
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]]
主要是对维度大小为1的升降维操作。
torch.squeeze(input)#去掉维度为1的维数 torch.unsqueeze(input,dim)#指定位置增加一维
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章主要介绍Python数据类型,很多新手在学习Python时,对类的继承不是很了解,下文给大家介绍了继承的用法,重写父类方法等等内容,具有一定参考学习价值,感兴趣的朋友可以看一看,希望大家阅读完这篇文章能有所收获。
python中none与null有啥区别?很多新手在python时,对于none与null容易混淆,因此,这篇文章就主要给大家讲讲none与null用法的不同,感兴趣的朋友就继续往下看吧,希望大家阅读完这篇文章能有所收获。
数据库是存储和管理数据的仓库,但数据库并不能直接存储数据,数据是存储在表中的,在存储数据的过程中一定会用到数据库服务器,所谓的数据库服务器就是指在计算机上安装一个数据库管理程序,如MySQL。数据库、表、数据库服务器之间的关系,如图所示。
这篇文章主要介绍了教你使用Python根据模板批量生成docx文档,文中有非常详细的代码示例,对正在学习python的小伙伴们有很好地帮助,需要的朋友可以参考下
python中常用的excel模块库有几种?怎样安装?学习python对excel模块有一定的了解还是很有必要的,下面我们就来看看常见的excel模块库有什么特点以及要如何安装。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008