佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > pytorch教程之Tensor的值及操作使用學(xué)習(xí)

pytorch教程之Tensor的值及操作使用學(xué)習(xí)

熱門標(biāo)簽:網(wǎng)站文章發(fā)布 美圖手機(jī) 呼叫中心市場需求 智能手機(jī) 檢查注冊表項(xiàng) 服務(wù)器配置 銀行業(yè)務(wù) 鐵路電話系統(tǒng)

參考網(wǎng)址

1、Tensors

Tensors are similar to NumPy's ndaeeays,不同的是可以在GPU上使用和加速計(jì)算。
導(dǎo)入包

from __future__ import print_function
import torch

建立5*3的矩陣,未初始化

x = torch.empty(5,3)
print(x)

out

tensor([[ 1.4395e-36,  4.5848e-41,  1.4395e-36],
        [ 4.5848e-41,  1.4395e-36,  4.5848e-41],
        [ 1.4395e-36,  4.5848e-41,  2.8026e-45],
        [-1.9501e+00,  8.5165e+23,  0.0000e+00],
        [ 2.5223e-43,  0.0000e+00,  0.0000e+00]])

建立隨機(jī)初始化矩陣

x = torch.rand(5,3)
print(x)

out

tensor([[ 0.8074,  0.9175,  0.8109],
        [ 0.3313,  0.5902,  0.9179],
        [ 0.6562,  0.3283,  0.9798],
        [ 0.8218,  0.0817,  0.4454],
        [ 0.5934,  0.0040,  0.3411]])

建立零初始化矩陣,數(shù)據(jù)類型是Long

...
x = torch.zeros(5,3,dtype = torch.long) 
print(x)
...

out

tensor([[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]])

建立一個(gè)tensor數(shù)據(jù)來源于data

x = torch.tensor([5.5,3])
print(x)

out

tensor([ 5.5000,  3.0000])

在原有tnesor的基礎(chǔ)上形成新的tensor,會(huì)繼承原有tensor的shapee和dtype等屬性,當(dāng)然我么也可以修改這些屬性

x = x.new_ones(5,3,dtype = torch.double)
print(x)
x = torch.randn_like(x,dype = torch.float)
print(x)

out

tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]], dtype=torch.float64)
tensor([[-0.0730, -0.0716, -0.8259],
        [-1.7004,  0.8790, -0.0659],
        [-0.8969,  0.8736, -0.6035],
        [-0.1539, -2.9178, -0.7456],
        [-0.0245,  0.4075,  1.4904]])

獲取tensor的size

print(x.size())

out

torch.Size([5, 3])

torch.size是一個(gè)元組,支持所有元組(tuple)的操作

2、對Tensor的操作

實(shí)現(xiàn)加法的四種方式

方法一L

print(x+y)

方法二

print(torch.add(x,y))

方法三:輸出給額外的tensor

result = torch.empty(5,3)
torch.add(x,y ,out= result)
print (result)

方法四:原地替換-結(jié)果存放在y中

print(y)

所有原地替換

所有原地替換tensor的操作都有后綴,比如x.copy(y),會(huì)改變x

使用標(biāo)準(zhǔn)的numpy操作

print(x[:1]

out

tensor([-0.0716,  0.8790,  0.8736, -2.9178,  0.4075])

使用torch.view 改變tensor的形狀

x = torch.randn(4,4)
y = x.view(16)
z = x.view(-1,8)   # the size -1 is inferred from other dimensions
print (x.size(),y.xize(),z.size())

out

torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

tensor轉(zhuǎn)化為numpy的數(shù)字,使用item

x = torch.rnadn(1)
print(x)
print(x.item())

Torch Tensor 和numpy的相互轉(zhuǎn)換

a = torch.ones(5)
print (a) 

out

tensor([ 1.,  1.,  1.,  1.,  1.])

并且改變tensor的值會(huì)同時(shí)改變numpy的值

a.add_(1)
print(a)
print(b)

out

tensor([ 2.,  2.,  2.,  2.,  2.])
[ 2.  2.  2.  2.  2.]

將numpy array轉(zhuǎn)化為pytorch Tensor

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a,1,out = a )
print(a)
print(b)

out

[ 2.  2.  2.  2.  2.]
tensor([ 2.,  2.,  2.,  2.,  2.], dtype=torch.float64)

所有在cpu上的tensor都支持numpy轉(zhuǎn)化,除了char形的tensor

CUDA Tensors

Tensors 可以被移動(dòng)到其他設(shè)備使用.to的方法

...
if torch.cuda.is_avaulable(): 
device = torch.device(“cuda”) 
y = torch.ones_like(x,device = devcie) 
x= x.to(device) 
z = x+y 
print(z) 
print(z.to(“cpu”,torch.double)) 
...

out

tensor([-1.0620], device='cuda:0')
tensor([-1.0620], dtype=torch.float64)

以上就是pytorch教程之Tensor學(xué)習(xí)筆記的詳細(xì)內(nèi)容,更多關(guān)于pytorch教程的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 使用Pytorch搭建模型的步驟
  • 如何使用Pytorch搭建模型
  • pytorch構(gòu)建網(wǎng)絡(luò)模型的4種方法
  • Pytorch教程內(nèi)置模型源碼實(shí)現(xiàn)

標(biāo)簽:河南 滄州 長治 沈陽 上海 新疆 樂山 紅河

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《pytorch教程之Tensor的值及操作使用學(xué)習(xí)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    巫山县| 乐昌市| 金山区| 维西| 宁津县| 梁山县| 神农架林区| 绥阳县| 周口市| 嫩江县| 同心县| 祁东县| 巴林左旗| 卓资县| 五家渠市| 克什克腾旗| 莱芜市| 泊头市| 新田县| 大兴区| 阳东县| 赤峰市| 开化县| 石狮市| 彝良县| 子洲县| 武鸣县| 许昌县| 海盐县| 登封市| 衢州市| 乳山市| 方城县| 灵川县| 临泉县| 望江县| 伊宁市| 淮阳县| 泗洪县| 丘北县| 土默特右旗|