Python Pytorch深度學(xué)習(xí)之Tensors張量
環(huán)境:Anaconda自帶的編譯器——Spyder
最近才開(kāi)使用conda,發(fā)現(xiàn)conda 就是 yyds,愛(ài)啦~
一、Tensor(張量)
import torch #構(gòu)造一個(gè)5*3的空矩陣 x=torch.FloatTensor(5,3) print(x) # 構(gòu)造隨機(jī)初始化矩陣 x=torch.rand(5,3) print(x) # 構(gòu)造一個(gè)矩陣全為0,而且數(shù)據(jù)類型為long x=torch.zeros(5,3,dtype=torch.long) print(x) # 構(gòu)造一個(gè)張量,直接使用數(shù)據(jù) x=torch.tensor([5.5,3]) print(x) # 創(chuàng)建一個(gè)tensor基于已經(jīng)存在到的tensor x=x.new_ones(5,3,dtype=torch.double) print(x) x=torch.randn_like(x,dtype=torch.float) print(x) # 獲取它的維度信息 print(x.size())
運(yùn)行結(jié)果

二、操作
print("*************操作**************")
# 加法操作
y=torch.rand(5,3)
print(x+y)
print(torch.add(x, y))
# 定義變量,作為存儲(chǔ)結(jié)果
result=torch.empty(5,3)
torch.add(x,y,out=result)
print(result)
y.add_(x)
print(y)
運(yùn)行結(jié)果

注:任何使張量發(fā)生變化的操作都有一個(gè)前綴"":x.copy(y),這樣才會(huì)發(fā)生改變
print("*************改變大小_view**************")
x=torch.rand(4,4)
y=x.view(16)
z=x.view(-1,8)
print(x.size())
print(y.size())
print(z.size())
運(yùn)行結(jié)果

print("*************獲得value**************")
x=torch.randn(1)
print(x)
print(x.item())#獲得value值

總結(jié)
本篇文章就到這里了,希望能夠給你帶來(lái)幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信