numpy和torch转化如何实现,代码是什么
Admin 2022-07-26 群英技术资讯 1249 次浏览
这篇文章给大家分享的是numpy和torch转化如何实现,代码是什么。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。在实际计算过程中,float类型使用最多,因此这里重点介绍numpy和torch数据float类型转化遇到的问题,其他类型同理。
numpy使用astype转化数据类型,float默认转化为64位,可以使用np.float32指定为32位
#numpy转化float类型 a= np.array([1,2,3]) a = a.astype(np.float) print(a) print(a.dtype)
[1. 2. 3.]
float64
不要使用a.dtype指定数据类型,会使数据丢失
#numpy转化float类型 b= np.array([1,2,3]) b.dtype= np.float32 print(b) print(b.dtype)
[1.e-45 3.e-45 4.e-45]
float32
不要用float代替np.float,否则可能出现意想不到的错误
不能从np.float64位转化np.float32,会报错
np.float64与np.float32相乘,结果为np.float64
在实际使用过程中,可以指定为np.float,也可以指定具体的位数,如np.float,不过直接指定np.float更方便。
torch使用torch.float()转化数据类型,float默认转化为32位,torch中没有torch.float64()这个方法
# torch转化float类型 b = torch.tensor([4,5,6]) b = b.float() b.dtype
torch.float32
np.float64使用torch.from_numpy转化为torch后也是64位的
print(a.dtype) c = torch.from_numpy(a) c.dtype
float64
torch.float64
不要用float代替torch.float,否则可能出现意想不到的错误
torch.float32与torch.float64数据类型相乘会出错,因此相乘的时候注意指定或转化数据float具体类型
np和torch数据类型转化大体原理一样,只有相乘的时候,torch.float不一致不可相乘,np.float不一致可以相乘,并且转化为np.float64
tensor转化为numpy
import torch b = torch.tensor([4.0,6]) # b = b.float() print(b.dtype) c = b.numpy() print(c.dtype)
torch.int64
int64
numpy转化为tensor
import torch import numpy as np b= np.array([1,2,3]) # b = b.astype(np.float) print(b.dtype) c = torch.from_numpy(b) print(c.dtype)
int32
torch.int32
可以看到,torch默认int型是64位的,numpy默认int型是32位的
补充:torch.from_numpy VS torch.Tensor
最近在造dataset的时候,突然发现,在输入图像转tensor的时候,我可以用torch.Tensor直接强制转型将numpy类转成tensor类,也可以用torch.from_numpy这个方法将numpy类转换成tensor类,那么,torch.Tensor和torch.from_numpy这两个到底有什么区别呢?既然torch.Tensor能搞定,那torch.from_numpy留着不就是冗余吗?
有区别,使用torch.from_numpy更加安全,使用tensor.Tensor在非float类型下会与预期不符。
实际上,两者的区别是大大的。打个不完全正确的比方说,torch.Tensor就如同c的int,torch.from_numpy就如同c++的static_cast,我们都知道,如果将int64强制转int32,只要是高位转低位,一定会出现高位被抹去的隐患的,不仅仅可能会丢失精度,甚至会正负对调。
这里的torch.Tensor与torch.from_numpy也会存在同样的问题。
看看torch.Tensor的文档,里面清楚地说明了,
torch.Tensor is an alias for the default tensor type (torch.FloatTensor).
而torch.from_numpy的文档则是说明,
The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa. The returned tensor is not resizable.
也即是说,
1、当转换的源是float类型,torch.Tensor与torch.from_numpy会共享一块内存!且转换后的结果的类型是torch.float32
2、当转换的源不是float类型,torch.Tensor得到的是torch.float32,而torch.from_numpy则是与源类型一致!
import torch import numpy as nps1 = np.arange(10, dtype=np.float32) s2 = np.arange(10) # 默认的dtype是int64# 例一 o11 = torch.Tensor(s1) o12 = torch.from_numpy(s1) o11.dtype # torch.float32 o12.dtype # torch.float32 # 修改值 o11[0] = 12 o12[0] # tensor(12.)# 例二 o21 = torch.Tensor(s2) o22 = torch.from_numpy(s2) o21.dtype # torch.float32 o22.dtype # torch.int64 # 修改值 o21[0] = 12 o22[0] # tensor(0)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
一些朋友想要知道怎样提取视频中的音频,其实我们能使用Python来实现。本文就给大家分享Python提取视频中的音频的操作流程,供大家参考,感兴趣的朋友可以了解看看。
这篇文章主要介绍了Python字典遍历的陷阱,我们都知道,Python中常常按照key、value的形式来遍历字典的items。若value是基本数据类型(int,float等),则是传的拷贝,是不能直接修改value的,下面来看看文章的详细内容吧
在matplotlib中,一般图例默认是在图表内部的,如果要放置到图例外面,需要对坐标进行指定,下面这篇文章主要给大家介绍了关于python中matplotlib调整图例位置的相关资料,需要的朋友可以参考下
内容介绍前言一、Xpath简介二、Xpath语法规则语法规则标签定位属性定位索引定位取文本内容三、语法规则练习总结前言网上已经有很多大佬发过Xpath,而且讲的都很好,我是因为刚开始学习网络爬虫,对这
matplotlib是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图,下面这篇文章主要给大家介绍了关于matplotlib之pyplot模块添加文本、注解(text和annotate)的相关资料,需要的朋友可以参考下
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008