Pytorch-tensor的創建,索引,切片


1.基本概念

標量:就是一個數,是0維的,只有大小,沒有方向

向量:是1*n的一列數,是1維的,有大小,也有方向

張量:是n*n的一堆數,是2維的,n個向量合並而成

2.a.size(),a.shape(),a.numel(),a.dim()的區別

a.size():輸出a的某一維度中元素的個數,若未指定維度,則計算所有元素的個數

a.shape():輸出a數組各維度的長度信息,返回是元組類型。

a.numel():輸出a占用內存的數量

a.dim():輸出a的維數

 

3.tensor的基本函數:

import torch
import numpy as np

if __name__ == '__main__':
    # 隨機正太分布
    a=torch.randn(2,3)
    print("a:",a)
    print("a.size():",a.size())
    print("a.size(0):",a.size(0))
    print("a.size(1):",a.size(1))
    print("a.shape[0]:",a.shape[0])
    print("a.shape[1]:",a.shape[1])
    print("a.shape:",a.shape)
    # 將a.shape轉換成list
    print("list(a.shape):",list(a.shape))
    # 輸出a占用內存的數量=2*3
    print("a.numel():",a.numel())
    # 輸出a的維數
    print("a.dim():",a.dim())
    print()


    # 0~1隨機均勻分布
    b=torch.rand(2,3,4)
    print("b:",b)
    print("b.size():",b.size())
    print("b.size(0):",b.size(0))
    print("b.size(1):",b.size(1))
    print("b.shape[0]:",b.shape[0])
    print("b.shape[1]:",b.shape[1])
    print("b.shape:",b.shape)
    print("list(b.shape):",list(b.shape))
    # 輸出b占用內存的數量=2*3*4
    print("b.numel():",b.numel())
    print("b.dim():",b.dim())
    print()

 
運行結果:

a: tensor([[-0.2106, -2.1292, -0.8221],
        [-1.5805,  0.2592, -1.1203]])
a.size(): torch.Size([2, 3])
a.size(0): 2
a.size(1): 3
a.shape[0]: 2
a.shape[1]: 3
a.shape: torch.Size([2, 3])
list(a.shape): [2, 3]
a.numel(): 6
a.dim(): 2

b: tensor([[[0.8126, 0.8908, 0.3507, 0.1554],
         [0.8679, 0.5295, 0.5461, 0.5021],
         [0.2570, 0.2250, 0.6310, 0.0662]],

        [[0.1139, 0.9552, 0.5847, 0.5421],
         [0.3589, 0.0090, 0.0324, 0.6984],
         [0.9562, 0.4533, 0.4296, 0.4052]]])
b.size(): torch.Size([2, 3, 4])
b.size(0): 2
b.size(1): 3
b.shape[0]: 2
b.shape[1]: 3
b.shape: torch.Size([2, 3, 4])
list(b.shape): [2, 3, 4]
b.numel(): 24
b.dim(): 3

 
tensor的創建:

# 將一個numpy的變量轉變成torch類型的
    # c是一個一行兩列的,[2.2 , 3.3] 矩陣
    c=np.array([2.2,3.3])
    print(torch.from_numpy(c))

    # d是一個三行四列的值為1的矩陣
    d=np.ones([3,4])
    # 導入后類型轉變成了torch.float64()
    print(torch.from_numpy(d))
    print()

    # 小寫的tensor()接受的是現有的數據,一行兩列
    print("torch.tensor([2,3]):\n",torch.tensor([2,3]))
    print()
    # 大寫的FloatTensor()接受的是數據的維度,兩行三列,(也可以接受現有的數據)
    print("torch.FloatTensor(2,3):\n",torch.FloatTensor(2,3))
    print()

    # 不推薦使用上邊的方法,因為上邊初始化,會生成非常大或者非常小的值
    # 而是推薦使用隨機生成指定數值區間的初始化
    # rand(2,3):隨機生成值在0~1區間的,兩行三列的的張量
    print("torch.rand(2,3):\n",torch.rand(2,3))
    print()

    # rand_like() 參數是一個張量(tensor),相當於把e的shape讀出來,之后再送給rand函數
    e=torch.rand(3,4)
    print("torch.rand_like(e): //e是一個三行四列的張量\n",torch.rand_like(e))
    print()

    # randint() 參數依次是,randint(最小值,最大值,shape),取不到最大值
    print("torch.randint(1,10,[3,4]):\n",torch.randint(1,10,[3,4]))

 
運行結果:

tensor([2.2000, 3.3000], dtype=torch.float64)
tensor([[1., 1., 1., 1.],
        [1., 1., 1., 1.],
        [1., 1., 1., 1.]], dtype=torch.float64)

torch.tensor([2,3]):
 tensor([2, 3])

torch.FloatTensor(2,3):
 tensor([[0., 0., 0.],
        [0., 0., 0.]])

torch.rand(2,3):
 tensor([[0.4876, 0.7776, 0.3553],
        [0.3311, 0.9068, 0.0672]])

torch.rand_like(e): //e是一個三行四列的張量
 tensor([[0.0792, 0.8138, 0.6931, 0.8604],
        [0.2047, 0.9061, 0.8075, 0.1821],
        [0.0216, 0.2109, 0.4703, 0.7405]])

torch.randint(1,10,[3,4]):
 tensor([[8, 3, 2, 7],
        [6, 3, 8, 6],
        [1, 4, 1, 5]])

 

4.tensor的創建與切片

import torch

if __name__ == '__main__':

    # 生成一個兩行三列的矩陣,並把所有值賦值為3.92
    c=torch.full((2, 3), 3.92)
    print('torch.full((2, 3), 3.92):\n',c,'\n')

    # 步長為2,按序生成0~10之間的數字
    d=torch.arange(0,10,step=2)
    print('d=torch.arange(0,10,step=2):\n',d,'\n')

    # 均勻生成某段數據
    e=torch.linspace(0,10,steps=10)
    print('torch.linspace(0,10,steps=10):\n',e,'\n')
    e1=torch.linspace(0,10,steps=11)
    print('e1=torch.linspace(0,10,steps=11):\n',e1,'\n')

    # 值全為1矩陣
    f=torch.ones(3,3)
    print('torch.ones(3,3):\n',f,'\n')
    # 值全為零矩陣
    f1=torch.zeros(3,3)
    print('torch.zeros(3,3):\n',f1,'\n')
    # 單位矩陣
    f2=torch.eye(3,3)
    print('torch.eye(3,3):\n',f2,'\n')

    g=torch.rand(4,3,28,28)
    print('shape的基本使用:')
    print(g[0].shape)
    print(g[0,0].shape)
    print(g[0,0,2,4])

    print('\ntensor的切片使用:')
    # 取前兩張圖片
    print(g[:2].shape)
    # 取第二張圖片向后及第一個通道向后
    print(g[2:,1:].shape)
    # 行:隔七個采一個樣,列:隔14個采一個樣,(start:stop:step)
    print(g[0,0,0:28:7,::14])


    h=torch.randn(3,4)
    print('\n',h)
    # 矩陣中值大於0.5的 賦值為ture
    mask=h.__ge__(0.5)
    print(mask)
    print(torch.masked_select(h,mask))

 
運行結果:

torch.full((2, 3), 3.92):
 tensor([[3.9200, 3.9200, 3.9200],
        [3.9200, 3.9200, 3.9200]]) 
        
d=torch.arange(0,10,step=2):
 tensor([0, 2, 4, 6, 8]) 
 
torch.linspace(0,10,steps=10):
 tensor([ 0.0000,  1.1111,  2.2222,  3.3333,  4.4444,  5.5556,  6.6667,  7.7778,
         8.8889, 10.0000]) 
         
e1=torch.linspace(0,10,steps=11):
 tensor([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]) 
 
torch.ones(3,3):
 tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]) 
        
torch.zeros(3,3):
 tensor([[0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]]) 
        
torch.eye(3,3):
 tensor([[1., 0., 0.],
        [0., 1., 0.],
        [0., 0., 1.]]) 
        
shape的基本使用:
torch.Size([3, 28, 28])
torch.Size([28, 28])
tensor(0.7568)

tensor的切片使用:
torch.Size([2, 3, 28, 28])
torch.Size([2, 2, 28, 28])
tensor([[0.4571, 0.3198],
        [0.6540, 0.3359],
        [0.2601, 0.8069],
        [0.9713, 0.6876]])
 tensor([[-2.4096,  1.1243, -1.0314, -1.4685],
        [-2.5054,  0.7131, -0.0376, -0.2110],
        [ 1.8922,  1.8989,  0.0459, -1.6457]])
tensor([[False,  True, False, False],
        [False,  True, False, False],
        [ True,  True, False, False]])
tensor([1.1243, 0.7131, 1.8922, 1.8989])



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM