濮阳杆衣贸易有限公司

主頁 > 知識庫 > pytorch教程之Tensor的值及操作使用學習

pytorch教程之Tensor的值及操作使用學習

熱門標簽:電話機器人適用業(yè)務 不錯的400電話辦理 鄭州智能外呼系統(tǒng)運營商 獲客智能電銷機器人 南昌辦理400電話怎么安裝 湛江電銷防封卡 佛山防封外呼系統(tǒng)收費 徐州天音防封電銷卡 哈爾濱外呼系統(tǒng)代理商

參考網址

1、Tensors

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

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]])

建立隨機初始化矩陣

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ù)據類型是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]])

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

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

out

tensor([ 5.5000,  3.0000])

在原有tnesor的基礎上形成新的tensor,會繼承原有tensor的shapee和dtype等屬性,當然我么也可以修改這些屬性

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是一個元組,支持所有元組(tuple)的操作

2、對Tensor的操作

實現(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)

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

print(y)

所有原地替換

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

使用標準的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轉化為numpy的數(shù)字,使用item

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

Torch Tensor 和numpy的相互轉換

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

out

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

并且改變tensor的值會同時改變numpy的值

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

out

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

將numpy array轉化為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轉化,除了char形的tensor

CUDA Tensors

Tensors 可以被移動到其他設備使用.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學習筆記的詳細內容,更多關于pytorch教程的資料請關注腳本之家其它相關文章!

您可能感興趣的文章:
  • 使用Pytorch搭建模型的步驟
  • 如何使用Pytorch搭建模型
  • pytorch構建網絡模型的4種方法
  • Pytorch教程內置模型源碼實現(xiàn)

標簽:蕪湖 吉安 蘭州 呂梁 懷化 安康 廣西 紹興

巨人網絡通訊聲明:本文標題《pytorch教程之Tensor的值及操作使用學習》,本文關鍵詞  pytorch,教程,之,Tensor,的,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《pytorch教程之Tensor的值及操作使用學習》相關的同類信息!
  • 本頁收集關于pytorch教程之Tensor的值及操作使用學習的相關信息資訊供網民參考!
  • 推薦文章
    安达市| 正蓝旗| 昭平县| 海丰县| 曲麻莱县| 正安县| 奉化市| 焉耆| 循化| 喀什市| 天水市| 景洪市| 多伦县| 积石山| 海丰县| 青田县| 十堰市| 南溪县| 阿合奇县| 志丹县| 岳普湖县| 保德县| 澄江县| 苗栗市| 阳山县| 高台县| 南宁市| 清苑县| 云阳县| 福贡县| 全州县| 额尔古纳市| 佛坪县| 福贡县| 望谟县| 水城县| 大埔区| 桐梓县| 新蔡县| 中山市| 乐都县|