關(guān)于numpy和torch.tensor的張量的操作
1. 張量的拼接
(1) numpy.concatenate
np.concatenate((a1,a2,a3,…), axis=0)
張量的拼接要用np.concatenate這個方法的,其中 a1,a2,a3,…是拼接的子張量,axis是維數(shù),axis=0表示按照第一維進行拼接。
例如將兩個二維的張量按照第一維拼接成一個二維的張量:
import numpy as np a=np.array([[1,2,3]]) b=np.array([[4,5,6]]) c=np.concatenate((a,b),axis=0) print(c) d=np.concatenate((c,a),axis=0) print(d) e=np.concatenate((c,c),axis=1) print(e)
結(jié)果
array([[1, 2, 3],
[4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
對于axis選擇的更簡單直接的理解是我們可以從將被拼接的兩個矩陣的形狀上來看,比如
a.shape=(3,1,2), b.shape=(6,1,2),則我們對其進行拼接的話目的是讓拼接之后的shape=(9,1,2),那么我們就選擇axis=0,即代表對第0維的進行相加。
代碼如下:
import numpy as np a = np.zeros((3, 1, 2)) b = np.zeros((6, 1, 2)) c = np.concatenate((a, b), axis=0) print(c.shape)
結(jié)果為:
(9, 1, 2)
(2) torch.cat
這里的拼接和上面介紹的numpy的拼接功能是一樣的
C = torch.cat( (A,B),0 ) ?#按維數(shù)0拼接(豎著拼) C = torch.cat( (A,B),1 ) ?#按維數(shù)1拼接(橫著拼)
例:
import torch A=torch.ones(2,3) ?#2x3的張量(矩陣) ?? B=2*torch.ones(4,3) ?#4x3的張量(矩陣) ? ? C=torch.cat((A,B),0) ?#按維數(shù)0(行)拼接 print(C) ? ? ? ? ? ? ? ? ? ? ?
結(jié)果:
tensor([[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]])
接著上面
D=2*torch.ones(2,4) #2x4的張量(矩陣) C=torch.cat((A,D),1)#按維數(shù)1(列)拼接 print(C)
結(jié)果:
tensor([[ 1., 1., 1., 2., 2., 2., 2.],
[ 1., 1., 1., 2., 2., 2., 2.]])
2. 張量的重構(gòu)
(1) np.reshape
>>> import numpy as np >>> a = np.array([[1,2,3],[4,5,6]]) >>> a array([[1, 2, 3], ? ? ? ?[4, 5, 6]]) >>> b = np.reshape(a, (2,3,1)) >>> b array([[[1], ? ? ? ? [2], ? ? ? ? [3]], ? ? ? ?[[4], ? ? ? ? [5], ? ? ? ? [6]]]) >>> b.shape (2, 3, 1)
(2) array.shape
>>> import numpy as np >>> a = np.array([1,2,3,4,5,6,7,8]) >>> a.shape = (2, 4) >>> a array([[1, 2, 3, 4], ? ? ? ?[5, 6, 7, 8]])
(3) torch.view
在pytorch中view函數(shù)的作用為重構(gòu)張量的維度,相當(dāng)于numpy中resize()的功能,但是用法可能不太一樣。
1.torch.view(參數(shù)a,參數(shù)b,…)
例如:
import torch tt1=torch.tensor([-0.3623, -0.6115, ?0.7283, ?0.4699, ?2.3261, ?0.1599]) result=tt1.view(3,2) print(result)
結(jié)果
tensor([[-0.3623, -0.6115],
[ 0.7283, 0.4699],
[ 2.3261, 0.1599]])
在上面例子中參數(shù)a=3和參數(shù)b=2決定了將一維的tt1重構(gòu)成3x2維的張量。
2.有的時候會出現(xiàn)torch.view(-1)或者torch.view(參數(shù)a,-1)這種情況。
例:
import torch tt2=torch.tensor([[-0.3623, -0.6115], ? ? ? ? ?[ 0.7283, ?0.4699], ? ? ? ? ?[ 2.3261, ?0.1599]]) result=tt2.view(-1) print(result)
結(jié)果:
tensor([-0.3623, -0.6115, 0.7283, 0.4699, 2.3261, 0.1599])
由上面的案例可以看到,如果是torch.view(-1),則原張量會變成一維的結(jié)構(gòu)。
例:
import torch tt3=torch.tensor([[-0.3623, -0.6115], ? ? ? ? ?[ 0.7283, ?0.4699], ? ? ? ? ?[ 2.3261, ?0.1599]]) >>> result=tt3.view(2,-1)
結(jié)果:
tensor([[-0.3623, -0.6115, 0.7283],
[ 0.4699, 2.3261, 0.1599]])
由上面的案例可以看到,如果是torch.view(參數(shù)a,-1),則表示在參數(shù)b未知,參數(shù)a已知的情況下自動補齊列向量長度,在這個例子中a=2,tt3總共由6個元素,則b=6/2=3。
例:
import torch inputs = torch.randn(1,3) print(inputs) print(inputs.view(1, 1, -1))
結(jié)果:
tensor([[-0.5525, 0.6355, -0.3968]])
tensor([[[-0.5525, 0.6355, -0.3968]]])
將二維變?yōu)槿S,a=1,b=1,c=3/(1*1)
3. 張量的形狀
(1) torch.size
import torch inputs = torch.randn(1,3) print(inputs.size())
結(jié)果:
torch.Size([1, 3])
4. 張量的擴展
(1) torch.tensor擴展方法
用unsqueeze方法將原張量進行維度擴張,unsqueeze后面括號里的數(shù)字代表在哪個維度擴張
import torch a = torch.tensor([[1, 2, 3], [4, 5, 6]]) b = torch.tensor([[7, 8, 9], [4, 5, 6]]) print(a) print(b) a = a.unsqueeze(0) b = b.unsqueeze(0) print(a) print(b) c = torch.cat((a, b), 0) print(c) print(c.shape)
結(jié)果為
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[7, 8, 9],
[4, 5, 6]])
tensor([[[1, 2, 3],
[4, 5, 6]]])
tensor([[[7, 8, 9],
[4, 5, 6]]])
tensor([[[1, 2, 3],
[4, 5, 6]],[[7, 8, 9],
[4, 5, 6]]])
torch.Size([2, 2, 3])
用squeeze方法將原張量進行維度縮減,squeeze后面括號里的數(shù)字代表在哪個維度縮減
import torch a = torch.tensor([[1, 2, 3], [4, 5, 6]]) b = torch.tensor([[7, 8, 9], [4, 5, 6]]) print(a) print(b) a = a.unsqueeze(0) b = b.unsqueeze(0) print(a) print(b) a = a.squeeze(0) b = b.squeeze(0) print(a) print(b)
結(jié)果為
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[7, 8, 9],
[4, 5, 6]])
tensor([[[1, 2, 3],
[4, 5, 6]]])
tensor([[[7, 8, 9],
[4, 5, 6]]])
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[7, 8, 9],
[4, 5, 6]])
(2) np.array擴展方法
np.expand_dims:用于擴展數(shù)組的形狀
原始數(shù)組:
import numpy as np ? In [12]: a = np.array([[[1,2,3],[4,5,6]]]) a.shape Out[12]: (1, 2, 3)
np.expand_dims(a, axis=0)表示在0位置添加數(shù)據(jù),轉(zhuǎn)換結(jié)果如下:
In [13]:
b = np.expand_dims(a, axis=0)
b
Out[13]:
array([[[[1, 2, 3],
[4, 5, 6]]]])
In [14]:
b.shape
Out[14]:
(1, 1, 2, 3)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python?UnicodedecodeError編碼問題解決方法匯總
本文主要介紹了Python?UnicodedecodeError編碼問題解決方法匯總,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08python 中pyqt5 樹節(jié)點點擊實現(xiàn)多窗口切換問題
這篇文章主要介紹了python 中pyqt5 樹節(jié)點點擊實現(xiàn)多窗口切換問題,文中給大家介紹了python pyqt5 點擊按鈕來打開另一個窗口的方法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒借鑒價值,需要的朋友可以參考下2019-07-07解決python問題 Traceback (most recent call&n
這篇文章主要介紹了解決python問題 Traceback (most recent call last),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12