欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyTorch中torch.tensor()和torch.to_tensor()的區(qū)別

 更新時間:2023年01月28日 10:38:05   作者:Enzo?想砸電腦  
在Pytorch中Tensor和tensor都用于生成新的張量,但二者并不相同,下面這篇文章主要給大家介紹了關(guān)于PyTorch中torch.tensor()和torch.to_tensor()區(qū)別的相關(guān)資料,需要的朋友可以參考下

前言

在跑模型的時候,遇到如下報錯

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() 改寫成 torch.as_tensor() 就可以避免報錯了。

# 如下寫法報錯
 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

作用:生成一個新的 tensor, 這個新生成的tensor 會根據(jù)原數(shù)據(jù)的實際情況,來決定是進(jìn)行淺拷貝,還是深拷貝。當(dāng)然,會優(yōu)先淺拷貝,淺拷貝會共享內(nèi)存,并共享 autograd 歷史記錄。

情況一:數(shù)據(jù)類型相同 且 device相同,會進(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)

深拷貝:會拷貝 數(shù)據(jù)類型 和 device,不會記錄 autograd 歷史 (also known as a “leaf tensor” 葉子tensor)

重點(diǎn)是:

  • 如果原數(shù)據(jù)的數(shù)據(jù)類型是:list, tuple, NumPy ndarray, scalar, and other types,不會 waring
  • 如果原數(shù)據(jù)的數(shù)據(jù)類型是:tensor,使用 torch.tensor(data) 就會報waring
# 原數(shù)據(jù)類型是:tensor 會發(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, 沒警告
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python生成任意范圍任意精度的隨機(jī)數(shù)方法

    Python生成任意范圍任意精度的隨機(jī)數(shù)方法

    下面小編就為大家分享一篇Python生成任意范圍任意精度的隨機(jī)數(shù)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Django?ORM?F對象和Q對象查詢

    Django?ORM?F對象和Q對象查詢

    Django提供了兩個非常有用的工具:F對象和Q對象,方便了在一些特殊場景下的查詢過程,這篇文章主要介紹了Django?ORM?F對象和Q對象查詢,需要的朋友可以參考下
    2022-10-10
  • Python實現(xiàn)網(wǎng)絡(luò)自動化eNSP

    Python實現(xiàn)網(wǎng)絡(luò)自動化eNSP

    這篇文章主要介紹了Python實現(xiàn)網(wǎng)絡(luò)自動化eNSP,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • python增加矩陣維度的實例講解

    python增加矩陣維度的實例講解

    下面小編就為大家分享一篇python增加矩陣維度的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python黑魔法Descriptor描述符的實例解析

    Python黑魔法Descriptor描述符的實例解析

    與迭代器和裝飾器等一樣,描述符也是Python編程中的一項高級技巧,這里我們就來講解Python黑魔法Descriptor描述符的實例解析:
    2016-06-06
  • 深入理解Python中變量賦值的問題

    深入理解Python中變量賦值的問題

    在 python 中賦值語句總是建立對象的引用值,而不是復(fù)制對象。因此,python 變量更像是指針,而不是數(shù)據(jù)存儲區(qū)域,這點(diǎn)和大多數(shù)語言類似吧,比如 C++、java 等。下面這篇文章主要介紹了Python中變量賦值的問題,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • Pyecharts 動態(tài)地圖 geo()和map()的安裝與用法詳解

    Pyecharts 動態(tài)地圖 geo()和map()的安裝與用法詳解

    這篇文章主要介紹了Pyecharts 動態(tài)地圖 geo()和map()的安裝與用法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • python?獲取圖片中文字的四種辦法

    python?獲取圖片中文字的四種辦法

    本文主要介紹了python?獲取圖片中文字的幾種辦法,主要使用光學(xué)字符識別(OCR)技術(shù),本文主要介紹了4種第三方庫,具有一定的參考價值,感興趣的可以了解一下
    2023-11-11
  • 一篇文章帶你了解Python中的裝飾器

    一篇文章帶你了解Python中的裝飾器

    Python中的裝飾器是你進(jìn)入Python大門的一道坎,不管你跨不跨過去它都在那里,下面這篇文章主要給大家介紹了關(guān)于Python中裝飾器的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • Python PyQt5-圖形界面的美化操作

    Python PyQt5-圖形界面的美化操作

    這篇文章主要介紹了Python PyQt5-圖形界面的美化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03

最新評論