PyTorch中torch.tensor()和torch.to_tensor()的区别
作者:Enzo?想砸电脑 发布时间:2022-11-18 11:59:42
前言
在跑模型的时候,遇到如下报错
UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
网上查了一下,发现将 torch.tensor()
改写成 torch.as_tensor()
就可以避免报错了。
# 如下写法报错
feature = torch.tensor(image, dtype=torch.float32)
# 改为
feature = torch.as_tensor(image, dtype=torch.float32)
然后就又仔细研究了下 torch.as_tensor()
和 torch.tensor()
的区别,在此记录。
1、torch.as_tensor()
new_data = torch.as_tensor(data, dtype=None,device=None)->Tensor
作用:生成一个新的 tensor, 这个新生成的tensor 会根据原数据的实际情况,来决定是进行浅拷贝,还是深拷贝。当然,会优先浅拷贝,浅拷贝会共享内存,并共享 autograd 历史记录。
情况一:数据类型相同 且 device相同,会进行浅拷贝,共享内存
import numpy
import torch
a = numpy.array([1, 2, 3])
t = torch.as_tensor(a)
t[0] = -1
print(a) # [-1 2 3]
print(a.dtype) # int64
print(t) # tensor([-1, 2, 3])
print(t.dtype) # torch.int64
import numpy
import torch
a = torch.tensor([1, 2, 3], device=torch.device('cuda'))
t = torch.as_tensor(a)
t[0] = -1
print(a) # tensor([-1, 2, 3], device='cuda:0')
print(t) # tensor([-1, 2, 3], device='cuda:0')
情况二: 数据类型相同,但是device不同,深拷贝,不再共享内存
import numpy
import torch
import numpy
a = numpy.array([1, 2, 3])
t = torch.as_tensor(a, device=torch.device('cuda'))
t[0] = -1
print(a) # [1 2 3]
print(a.dtype) # int64
print(t) # tensor([-1, 2, 3], device='cuda:0')
print(t.dtype) # torch.int64
情况三:device相同,但数据类型不同,深拷贝,不再共享内存
import numpy
import torch
a = numpy.array([1, 2, 3])
t = torch.as_tensor(a, dtype=torch.float32)
t[0] = -1
print(a) # [1 2 3]
print(a.dtype) # int64
print(t) # tensor([-1., 2., 3.])
print(t.dtype) # torch.float32
2、torch.tensor()
torch.tensor()
是深拷贝方式。
torch.tensor(data, dtype=None, device=None, requires_grad=False, pin_memory=False)
深拷贝:会拷贝 数据类型 和 device,不会记录 autograd 历史 (also known as a “leaf tensor” 叶子tensor)
重点是:
如果原数据的数据类型是:list, tuple, NumPy ndarray, scalar, and other types,不会 waring
如果原数据的数据类型是:tensor,使用 torch.tensor(data) 就会报waring
# 原数据类型是:tensor 会发出警告
import numpy
import torch
a = torch.tensor([1, 2, 3], device=torch.device('cuda'))
t = torch.tensor(a)
t[0] = -1
print(a)
print(t)
# 输出:
# tensor([1, 2, 3], device='cuda:0')
# tensor([-1, 2, 3], device='cuda:0')
# /opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:5: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
# 原数据类型是:list, tuple, NumPy ndarray, scalar, and other types, 没警告
import torch
import numpy
a = numpy.array([1, 2, 3])
t = torch.tensor(a)
b = [1,2,3]
t= torch.tensor(b)
c = (1,2,3)
t= torch.tensor(c)
结论就是:以后尽量用 torch.as_tensor()
吧
来源:https://blog.csdn.net/weixin_37804469/article/details/128767214
猜你喜欢
- 本文实例讲述了python将ip地址转换成整数的方法。分享给大家供大家参考。具体分析如下:有时候我们用数据库存储ip地址时可以将ip地址转换
- Python3 线程中常用的两个模块为:_threadthreading(推荐使用)使用Thread类创建import threadingf
- 本文实例为大家分享了python+logging+yaml实现日志分割的具体代码,供大家参考,具体内容如下1、建立log.yaml文件ver
- 引言解释器环境:python3.5.1我们都知道python网络编程的两大必学模块socket和socketserver,其中的socket
- 数据格式:(polygon.txt) 里面含有2个多边形,一行是一个点 0.085, 0.834, 0.024, 0.744, 0, 0.6
- 本文实例讲述了Python 网络编程之TCP客户端/服务端功能。分享给大家供大家参考,具体如下:demo.py(TCP客户端):import
- 代码执行结构为顺序结构、选择结构、循环结构。python判断选择结构【if】if 判断条件 #进行判断条件满足之后执行下方语句 执行语句el
- 之前写了一篇flask开发环境搭建,今天继续,进行一个实战小项目-blog系统。blog系统很简单,只有一个页面,然后麻雀虽小五脏俱全。这里
- 不多说,我们直接上源码:# -*- coding:UTF-8 -*-'''实现文件打包、上传与校验Created o
- 通过这个布局思路来做一个简单的后台管理系统也是OK的,大家可以参考一下啦!话不多说,还是先来梳理一下需要的第三方模块。PyQ5 的UI界面布
- 在一开始接触PHP接触MYSQL的时候就听不少人说:“MySQL就跑跑一天几十万IP的小站还可以,要是几百万IP就不行了
- 代码如下:---在仓储管理中经常会碰到的一个问题 一、关于LIFO与FIFO的简单说明 ---FIFO: First in, First o
- 本文实例讲述了python用10行代码实现对 * 的检测功能。分享给大家供大家参考。具体如下:原理:将图片转换为YCbCr模式,在图片中寻
- 本文实例为大家分享了Python Unittest自动化单元测试框架的具体代码,供大家参考,具体内容如下1、python 测试框架(本文只涉
- 上班族经常会遇到这样情况,着急下班结果将关机误点成重启,或者临近下班又通知开会,开完会已经迟了还要去给电脑关机。今天使用PyQt5做了个自动
- 本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础以及Python两个线程标准库的完整介绍及使用示例。注意:本文基于
- 像素误差看自己设计好上线的网站,偶尔会发觉像素行间出现了弹性空间,总在不经意间蹦出一定的差距。有些页面很难发现,比如活动类页面,这类页面多呈
- 简单实现了一个在函数执行出现异常时自动重试的装饰器,支持控制最多重试次数,每次重试间隔,每次重试间隔时间递增。最新的代码可以访问从githu
- 目录1. DeepSource2. Codacy3. SonarQube4. Veracode5. Checkmarx6. Coverity
- 质数(Prime number),又称素数,指在大于1的自然数中,除了1和该数自身外,无法被其他自然数整除的数(也可定义为只有1与该数本身两