筆記 | PyTorch張量Tensor的一些必備操作
點(diǎn)擊上方“小白學(xué)視覺”,選擇加"星標(biāo)"或“置頂”
重磅干貨,第一時(shí)間送達(dá)
張量是什么?
標(biāo)量:0維張量;比如一個(gè)單獨(dú)的數(shù)字1
向量:1維張量;比如一組排列好的數(shù):1,2,3,4,5,6
矩陣:2為張量;比如一個(gè)灰度圖矩陣
3維張量;比如一個(gè)彩色圖RGB
n維張量
張量是一個(gè)多維數(shù)組,它是標(biāo)量、向量、矩陣的高維擴(kuò)展
Variable是PyTorch0.4.0之前的內(nèi)容,現(xiàn)在并入Tensor了!
Variable是torch.autograd中的數(shù)據(jù)類型,后來并入了Tensor,主要目的是為了封裝求導(dǎo)
五個(gè)屬性:
data:被包裝的Tensor
grad:data的梯度
grad_fn:創(chuàng)建Tensor的Func,記錄比如操作加法還是乘法
requires_grad:是否需要求梯度
is_leaf:是否是葉子結(jié)點(diǎn)(張量)
后來Variable就并入了Tensor中
現(xiàn)在是8個(gè)屬性:
dtype:張量的數(shù)據(jù)類型,如torch.FloatTensor,torch.cuda.FloatTensor
shape:張量的形狀
device:張量所在的設(shè)備,GPU還是CPU
那么張量有哪些必備的創(chuàng)建方法值得學(xué)習(xí)?
這些代碼王博Kings都一個(gè)一個(gè)敲打注釋的,可以直接運(yùn)行
import numpy as np
import torch
通過torch.tensor創(chuàng)建張量
arr = np.ones((3,3))print("由np.ones生成的數(shù)據(jù):\n",arr)print("ndarray的數(shù)據(jù)類型:",arr.dtype,"\n")#t = torch.tensor(arr, device='cuda')t = torch.tensor(arr)print("torch.tensor創(chuàng)建好的Tensor:\n",t)
輸出結(jié)果:
由np.ones生成的數(shù)據(jù):[[1. 1. 1.][1. 1. 1.][1. 1. 1.]]ndarray的數(shù)據(jù)類型:float64torch.tensor創(chuàng)建好的Tensor:tensor([[1., 1., 1.],[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64)Process finished with exit code 0
通過torch.from_numpy創(chuàng)建張量
arr = np.array([[1,2,3],[4,5,6]])print("由np.array生成的數(shù)據(jù):\n",arr)t = torch.from_numpy(arr)print("torch.from_numpy創(chuàng)建好的Tensor:\n",t)# 由from_numpy創(chuàng)建的Tensor是共享內(nèi)存空間# 修改numpy中的一個(gè)數(shù)字,看看tensor有沒有變化print("\n=======================\n修改arr")arr[0,0]=9print("由np.array生成的數(shù)據(jù):\n",arr)t = torch.from_numpy(arr)print("torch.from_numpy創(chuàng)建好的Tensor:\n",t)# 由from_numpy創(chuàng)建的Tensor是共享內(nèi)存空間# 修改tensor中的一個(gè)數(shù)字,看看numpy有沒有變化print("\n=======================\n修改t")t[0,0]=-9print("由np.array生成的數(shù)據(jù):\n",arr)t = torch.from_numpy(arr)print("torch.from_numpy創(chuàng)建好的Tensor:\n",t)
運(yùn)行結(jié)果
由np.array生成的數(shù)據(jù):[[1 2 3][4 5 6]]torch.from_numpy創(chuàng)建好的Tensor:tensor([[1, 2, 3],[4, 5, 6]], dtype=torch.int32)=======================修改arr由np.array生成的數(shù)據(jù):[[9 2 3][4 5 6]]torch.from_numpy創(chuàng)建好的Tensor:tensor([[9, 2, 3],[4, 5, 6]], dtype=torch.int32)=======================修改t由np.array生成的數(shù)據(jù):[[-9 2 3][ 4 5 6]]torch.from_numpy創(chuàng)建好的Tensor:tensor([[-9, 2, 3],[ 4, 5, 6]], dtype=torch.int32)Process finished with exit code 0
通過torch.zeros創(chuàng)建張量
out_t = torch.tensor(([1]))t = torch.zeros((3,3), out=out_t)print("t的值:\n",t,"\n out_t的值\n",out_t)print("看一下t和out_t的id位置號(hào)\n",id(t),"\n",id(out_t))
輸出結(jié)果
t的值:tensor([[0, 0, 0],[0, 0, 0],[0, 0, 0]])out_t的值tensor([[0, 0, 0],[0, 0, 0],[0, 0, 0]])看一下t和out_t的id位置號(hào)18195287469521819528746952Process finished with exit code 0
通過torch.full創(chuàng)建全相同的張量
t = torch.full((3,3),10)print("torch.full((3,3),10)的輸出結(jié)果\n",t)
輸出結(jié)果
torch.full((3,3),10)的輸出結(jié)果tensor([[10., 10., 10.],[10., 10., 10.],[10., 10., 10.]])
通過torch.arange創(chuàng)建等差數(shù)列張量
t = torch.arange(2,10,2)print("torch.arange(2,10,2)的輸出結(jié)果\n",t)
輸出結(jié)果
torch.arange(2,10,2)的輸出結(jié)果tensor([2, 4, 6, 8])
通過torch.linspace創(chuàng)建均分?jǐn)?shù)列 張量
步長(zhǎng)=(Start - end)/(Steps - 1)
t = torch.linspace(2,10,5)print("torch.linspace(2,10,5)的輸出結(jié)果\n",t)t = torch.linspace(2,10,6)print("torch.linspace(2,10,6)的輸出結(jié)果\n",t)
輸出結(jié)果
torch.linspace(2,10,5)的輸出結(jié)果tensor([ 2., 4., 6., 8., 10.])torch.linspace(2,10,6)的輸出結(jié)果tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
通過torch.eye創(chuàng)建單位對(duì)角矩陣
t = torch.eye(3)print("torch.eye(3)的輸出結(jié)果\n",t)
輸出結(jié)果
torch.eye(3)的輸出結(jié)果tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])
通過torch.normal()創(chuàng)建概率分布的張量
# mean:張量 std: 張量print("-----------mean:張量 std: 張量---------")mean = torch.arange(1, 5, dtype=torch.float)std = torch.arange(1, 5, dtype=torch.float)print("mean是:",mean,"\n std是:",std)t_normal = torch.normal(mean, std)print("t_normal是\n",t_normal)# mean:標(biāo)量 std: 標(biāo)量print("-----------mean:標(biāo)量 std: 標(biāo)量---------")t_normal = torch.normal(0., 1., size=(5,))print("torch.normal(0., 1., size=5,))輸出\n",t_normal)# mean:張量 std: 標(biāo)量print("-----------mean:張量 std: 標(biāo)量---------")mean = torch.arange(1, 5, dtype=torch.float)std = 1print("mean是:",mean,"\n std是:",std)t_normal = torch.normal(mean, std)print("t_normal是\n",t_normal)
輸出結(jié)果:
-----------mean:張量 std: 張量---------mean是:tensor([1., 2., 3., 4.])std是:tensor([1., 2., 3., 4.])t_normal是tensor([ 1.0527, 2.8746, 5.1393, -2.5108])-----------mean:標(biāo)量 std: 標(biāo)量---------torch.normal(0., 1., size=5,))輸出tensor([-0.7771, -0.7684, 0.8846, 0.5542, 1.3451])-----------mean:張量 std: 標(biāo)量---------mean是:tensor([1., 2., 3., 4.])std是:1t_normal是tensor([0.5560, 1.9777, 2.9634, 2.7911])Process finished with exit code 0
好消息,小白學(xué)視覺團(tuán)隊(duì)的知識(shí)星球開通啦,為了感謝大家的支持與厚愛,團(tuán)隊(duì)決定將價(jià)值149元的知識(shí)星球現(xiàn)時(shí)免費(fèi)加入。各位小伙伴們要抓住機(jī)會(huì)哦!

交流群
歡迎加入公眾號(hào)讀者群一起和同行交流,目前有SLAM、三維視覺、傳感器、自動(dòng)駕駛、計(jì)算攝影、檢測(cè)、分割、識(shí)別、醫(yī)學(xué)影像、GAN、算法競(jìng)賽等微信群(以后會(huì)逐漸細(xì)分),請(qǐng)掃描下面微信號(hào)加群,備注:”昵稱+學(xué)校/公司+研究方向“,例如:”張三 + 上海交大 + 視覺SLAM“。請(qǐng)按照格式備注,否則不予通過。添加成功后會(huì)根據(jù)研究方向邀請(qǐng)進(jìn)入相關(guān)微信群。請(qǐng)勿在群內(nèi)發(fā)送廣告,否則會(huì)請(qǐng)出群,謝謝理解~

