numpy和torch.tensor的張量的操作

張量的拼接(numpy.concatenate)

np.concatenate((a1,a2,a3,…), axis=0)
張量的拼接要用np.concatenate這個方法的,其中 a1,a2,a3,…是拼接的子張量,axis是維數,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)

結果

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)

結果爲:

(9, 1, 2)

張量的拼接(torch.cat)

這裏的拼接和上面介紹的numpy的拼接功能是一樣的

C = torch.cat( (A,B),0 )  #按維數0拼接(豎着拼)
C = torch.cat( (A,B),1 )  #按維數1拼接(橫着拼)

例:

import torch
A=torch.ones(2,3)  #2x3的張量(矩陣)   
B=2*torch.ones(4,3)  #4x3的張量(矩陣)    
C=torch.cat((A,B),0)  #按維數0(行)拼接
print(C)                                

結果:

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)#按維數1(列)拼接
print(C)

結果:

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

張量的重構(torch.view)

在pytorch中view函數的作用爲重構張量的維度,相當於numpy中resize()的功能,但是用法可能不太一樣。
1.torch.view(參數a,參數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)

結果

tensor([[-0.3623, -0.6115],
        [ 0.7283,  0.4699],
        [ 2.3261,  0.1599]])

在上面例子中參數a=3和參數b=2決定了將一維的tt1重構成3x2維的張量。

2.有的時候會出現torch.view(-1)或者torch.view(參數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)

結果:

tensor([-0.3623, -0.6115,  0.7283,  0.4699,  2.3261,  0.1599])

由上面的案例可以看到,如果是torch.view(-1),則原張量會變成一維的結構。

例:

import torch
tt3=torch.tensor([[-0.3623, -0.6115],
         [ 0.7283,  0.4699],
         [ 2.3261,  0.1599]])
>>> result=tt3.view(2,-1)

結果:

tensor([[-0.3623, -0.6115,  0.7283],
        [ 0.4699,  2.3261,  0.1599]])

由上面的案例可以看到,如果是torch.view(參數a,-1),則表示在參數b未知,參數a已知的情況下自動補齊列向量長度,在這個例子中a=2,tt3總共由6個元素,則b=6/2=3。

例:

import torch
inputs = torch.randn(1,3)
print(inputs)
print(inputs.view(1, 1, -1))

結果:

tensor([[-0.5525,  0.6355, -0.3968]])
tensor([[[-0.5525,  0.6355, -0.3968]]])

將二維變爲三維,a=1,b=1,c=3/(1*1)

張量的形狀(torch.size)

import torch
inputs = torch.randn(1,3)
print(inputs.size())

結果:

torch.Size([1, 3])

張量的擴展

用unsqueeze方法將原張量進行維度擴張,unsqueeze後面括號裏的數字代表在哪個維度擴張

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)

結果爲

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後面括號裏的數字代表在哪個維度縮減

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)

結果爲

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]])
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章