PyTorch中torch.tensor()和torch.to_tensor()的區(qū)別
前言
在跑模型的時(shí)候,遇到如下報(bào)錯(cuò)
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).
網(wǎng)上查了一下,發(fā)現(xiàn)將 torch.tensor() 改寫(xiě)成 torch.as_tensor() 就可以避免報(bào)錯(cuò)了。
# 如下寫(xiě)法報(bào)錯(cuò) feature = torch.tensor(image, dtype=torch.float32) # 改為 feature = torch.as_tensor(image, dtype=torch.float32)
然后就又仔細(xì)研究了下 torch.as_tensor() 和 torch.tensor() 的區(qū)別,在此記錄。
1、torch.as_tensor()
new_data = torch.as_tensor(data, dtype=None,device=None)->Tensor
作用:生成一個(gè)新的 tensor, 這個(gè)新生成的tensor 會(huì)根據(jù)原數(shù)據(jù)的實(shí)際情況,來(lái)決定是進(jìn)行淺拷貝,還是深拷貝。當(dāng)然,會(huì)優(yōu)先淺拷貝,淺拷貝會(huì)共享內(nèi)存,并共享 autograd 歷史記錄。
情況一:數(shù)據(jù)類型相同 且 device相同,會(huì)進(jìn)行淺拷貝,共享內(nèi)存
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')
情況二: 數(shù)據(jù)類型相同,但是device不同,深拷貝,不再共享內(nèi)存
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相同,但數(shù)據(jù)類型不同,深拷貝,不再共享內(nèi)存
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)
深拷貝:會(huì)拷貝 數(shù)據(jù)類型 和 device,不會(huì)記錄 autograd 歷史 (also known as a “leaf tensor” 葉子tensor)
重點(diǎn)是:
- 如果原數(shù)據(jù)的數(shù)據(jù)類型是:list, tuple, NumPy ndarray, scalar, and other types,不會(huì) waring
- 如果原數(shù)據(jù)的數(shù)據(jù)類型是:tensor,使用 torch.tensor(data) 就會(huì)報(bào)waring
# 原數(shù)據(jù)類型是:tensor 會(huì)發(fā)出警告
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).# 原數(shù)據(jù)類型是:list, tuple, NumPy ndarray, scalar, and other types, 沒(méi)警告 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)
結(jié)論就是:以后盡量用 torch.as_tensor() 吧
總結(jié)
到此這篇關(guān)于PyTorch中torch.tensor()和torch.to_tensor()區(qū)別的文章就介紹到這了,更多相關(guān)torch.tensor()和torch.to_tensor()區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python并發(fā)2之使用asyncio處理并發(fā)
本篇文章主要介紹了python并發(fā)2之使用asyncio處理并發(fā),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
基于Python打造一個(gè)高效開(kāi)發(fā)輔助全能工具箱
在日常開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要進(jìn)行各種瑣碎但又必不可少的操作,本文介紹一款基于?Python?編寫(xiě)的?全能工具箱,它涵蓋了開(kāi)發(fā)過(guò)程中常用的功能,希望對(duì)大家有所幫助2025-03-03
pytorch 如何使用batch訓(xùn)練lstm網(wǎng)絡(luò)
這篇文章主要介紹了pytorch 如何使用batch訓(xùn)練lstm網(wǎng)絡(luò)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
python函數(shù)定義和調(diào)用過(guò)程詳解
這篇文章主要介紹了python函數(shù)定義和調(diào)用過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Python實(shí)現(xiàn)將MongoDB中的數(shù)據(jù)導(dǎo)入到MySQL
這篇文章主要為大家詳細(xì)介紹了如何通過(guò)Python封裝一個(gè)將?MongoDB?中的數(shù)據(jù)導(dǎo)入到?MySQL?中的?Python?工具類?MongoToMysql,感興趣的可以了解一下2023-05-05
Python+Turtle實(shí)現(xiàn)繪制勾股樹(shù)
畢達(dá)哥拉斯樹(shù),也叫“勾股樹(shù)”,是由畢達(dá)哥拉斯根據(jù)勾股定理所畫(huà)出來(lái)的一個(gè)可以無(wú)限重復(fù)的樹(shù)形圖形。本文將利用Python中的Turtle庫(kù)實(shí)現(xiàn)勾股樹(shù)的繪制,感興趣的可以了解一下2023-01-01

