最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

在跑模型的時(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() 改寫成 torch.as_tensor() 就可以避免報(bào)錯(cuò)了。

# 如下寫法報(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í)際情況,來決定是進(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, 沒警告
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ā)

    本篇文章主要介紹了python并發(fā)2之使用asyncio處理并發(fā),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • 基于Python打造一個(gè)高效開發(fā)輔助全能工具箱

    基于Python打造一個(gè)高效開發(fā)輔助全能工具箱

    在日常開發(fā)過程中,我們經(jīng)常需要進(jìn)行各種瑣碎但又必不可少的操作,本文介紹一款基于?Python?編寫的?全能工具箱,它涵蓋了開發(fā)過程中常用的功能,希望對(duì)大家有所幫助
    2025-03-03
  • pytorch 如何使用batch訓(xùn)練lstm網(wǎng)絡(luò)

    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)用過程詳解

    python函數(shù)定義和調(diào)用過程詳解

    這篇文章主要介紹了python函數(shù)定義和調(diào)用過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Django工程的分層結(jié)構(gòu)詳解

    Django工程的分層結(jié)構(gòu)詳解

    在本篇文章里小編給各位分享的是關(guān)于Django工程的分層結(jié)構(gòu)的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。
    2019-07-07
  • Python?海象運(yùn)算符(?:=)的三種用法

    Python?海象運(yùn)算符(?:=)的三種用法

    這篇文章主要介紹了Python?海象運(yùn)算符(:=)的三種用法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下
    2022-06-06
  • Python實(shí)現(xiàn)將MongoDB中的數(shù)據(jù)導(dǎo)入到MySQL

    Python實(shí)現(xiàn)將MongoDB中的數(shù)據(jù)導(dǎo)入到MySQL

    這篇文章主要為大家詳細(xì)介紹了如何通過Python封裝一個(gè)將?MongoDB?中的數(shù)據(jù)導(dǎo)入到?MySQL?中的?Python?工具類?MongoToMysql,感興趣的可以了解一下
    2023-05-05
  • Python代碼打開本地.mp4格式文件的方法

    Python代碼打開本地.mp4格式文件的方法

    今天小編就為大家分享一篇Python代碼打開本地.mp4格式文件的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • Python中的Super用法示例詳解

    Python中的Super用法示例詳解

    Python中可以直接通過調(diào)用父類名調(diào)用父類方法,在多重繼承中,使用super()是一個(gè)很好的習(xí)慣,下面這篇文章主要給大家介紹了關(guān)于Python中Super用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • Python+Turtle實(shí)現(xiàn)繪制勾股樹

    Python+Turtle實(shí)現(xiàn)繪制勾股樹

    畢達(dá)哥拉斯樹,也叫“勾股樹”,是由畢達(dá)哥拉斯根據(jù)勾股定理所畫出來的一個(gè)可以無限重復(fù)的樹形圖形。本文將利用Python中的Turtle庫(kù)實(shí)現(xiàn)勾股樹的繪制,感興趣的可以了解一下
    2023-01-01

最新評(píng)論

元阳县| 阿拉善左旗| 普宁市| 镇雄县| 铜梁县| 乌海市| 柳江县| 荣成市| 城固县| 明光市| 莎车县| 缙云县| 澄城县| 杭锦后旗| 黄陵县| 襄垣县| 合江县| 丰原市| 泰安市| 策勒县| 巴马| 盘山县| 屏南县| 九龙县| 丹巴县| 壤塘县| 阳山县| 云南省| 内江市| 卫辉市| 沭阳县| 南京市| 朝阳区| 晴隆县| 绥阳县| 隆安县| 乐平市| 横峰县| 富阳市| 辛集市| 彝良县|