1. 程式人生 > 其它 >Pytorch學習-線性代數實現

Pytorch學習-線性代數實現

線性代數

標量由只有一個元素的張量表示

import torch

x = torch.tensor([3.0])
y = torch.tensor([2.0])

x+y,x*y,x/y,x**y
(tensor([5.]), tensor([6.]), tensor([1.5000]), tensor([9.]))

將向量視為標量值組成的列表

x = torch.arange(4)
x
tensor([0, 1, 2, 3])

通過張量的索引來訪問任一元素

x[3]
tensor(3)

訪問張量長度

len(x)
4

只有一個軸的張量,形狀只有一個元素

x.shape
torch.Size([4])

通過指定兩個分量m和n來建立一個形狀為m×n的矩陣

A = torch.arange(20).reshape(5,4)
A
tensor([[ 0,  1,  2,  3],
        [ 4,  5,  6,  7],
        [ 8,  9, 10, 11],
        [12, 13, 14, 15],
        [16, 17, 18, 19]])

矩陣的轉置

A.T
tensor([[ 0,  4,  8, 12, 16],
        [ 1,  5,  9, 13, 17],
        [ 2,  6, 10, 14, 18],
        [ 3,  7, 11, 15, 19]])

對稱矩陣 A 等於其轉置: A = A^T

B = torch.tensor([[1,2,3],[2,0,4],[3,4,5]])
B
tensor([[1, 2, 3],
        [2, 0, 4],
        [3, 4, 5]])
B == B.T
tensor([[True, True, True],
        [True, True, True],
        [True, True, True]])

可以構建具有更多軸的資料結構

X = torch.arange(24).reshape(2,3,4)
X
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11]],

        [[12, 13, 14, 15],
         [16, 17, 18, 19],
         [20, 21, 22, 23]]])

給定具有相同形狀的任何兩個張量,任何按元素二元運算的結果都將是相同形狀的張量

A = torch.arange(20,dtype = torch.float32).reshape(5,4)
B = A.clone() # 通過分配新記憶體,將A的一個副本分配給B
A,A+B
(tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.],
         [12., 13., 14., 15.],
         [16., 17., 18., 19.]]),
 tensor([[ 0.,  2.,  4.,  6.],
         [ 8., 10., 12., 14.],
         [16., 18., 20., 22.],
         [24., 26., 28., 30.],
         [32., 34., 36., 38.]]))

兩矩陣的按元素乘法,哈達瑪積

A * B
tensor([[  0.,   1.,   4.,   9.],
        [ 16.,  25.,  36.,  49.],
        [ 64.,  81., 100., 121.],
        [144., 169., 196., 225.],
        [256., 289., 324., 361.]])
a = 2
X = torch.arange(24).reshape(2,3,4)
a+X,(a*X).shape
(tensor([[[ 2,  3,  4,  5],
          [ 6,  7,  8,  9],
          [10, 11, 12, 13]],
 
         [[14, 15, 16, 17],
          [18, 19, 20, 21],
          [22, 23, 24, 25]]]),
 torch.Size([2, 3, 4]))

計算元素和

x = torch.arange(4,dtype = torch.float32)
x,x.sum()
(tensor([0., 1., 2., 3.]), tensor(6.))

表示任意形狀張量的元素和

A = torch.arange(20*2).reshape(2,5,4)
A.shape,A.sum()
(torch.Size([2, 5, 4]), tensor(780))
A
tensor([[[ 0,  1,  2,  3],
         [ 4,  5,  6,  7],
         [ 8,  9, 10, 11],
         [12, 13, 14, 15],
         [16, 17, 18, 19]],

        [[20, 21, 22, 23],
         [24, 25, 26, 27],
         [28, 29, 30, 31],
         [32, 33, 34, 35],
         [36, 37, 38, 39]]])

指定求和彙總張量的軸

A_sum_axis0 = A.sum(axis = 0)
A_sum_axis0,A_sum_axis0.shape
(tensor([[20, 22, 24, 26],
         [28, 30, 32, 34],
         [36, 38, 40, 42],
         [44, 46, 48, 50],
         [52, 54, 56, 58]]),
 torch.Size([5, 4]))
A_sum_axis1 = A.sum(axis = 1)
A_sum_axis1,A_sum_axis1.shape
(tensor([[ 40,  45,  50,  55],
         [140, 145, 150, 155]]),
 torch.Size([2, 4]))
A.sum(axis = [0,1]),A.sum(axis = [0,1]).shape
(tensor([180, 190, 200, 210]), torch.Size([4]))

平均值

A = A.float()
A.mean(),A.sum()/A.numel()
(tensor(19.5000), tensor(19.5000))
A.mean(axis = 0),A.sum(axis = 0)/A.shape[0]
(tensor([[10., 11., 12., 13.],
         [14., 15., 16., 17.],
         [18., 19., 20., 21.],
         [22., 23., 24., 25.],
         [26., 27., 28., 29.]]),
 tensor([[10., 11., 12., 13.],
         [14., 15., 16., 17.],
         [18., 19., 20., 21.],
         [22., 23., 24., 25.],
         [26., 27., 28., 29.]]))

計算總和或均值時保持軸數不變

sum_A = A.sum(axis = 1,keepdims =True)
sum_A
tensor([[[ 40.,  45.,  50.,  55.]],

        [[140., 145., 150., 155.]]])

通過廣播將A 除以 sum_A

A/sum_A
tensor([[[0.0000, 0.0222, 0.0400, 0.0545],
         [0.1000, 0.1111, 0.1200, 0.1273],
         [0.2000, 0.2000, 0.2000, 0.2000],
         [0.3000, 0.2889, 0.2800, 0.2727],
         [0.4000, 0.3778, 0.3600, 0.3455]],

        [[0.1429, 0.1448, 0.1467, 0.1484],
         [0.1714, 0.1724, 0.1733, 0.1742],
         [0.2000, 0.2000, 0.2000, 0.2000],
         [0.2286, 0.2276, 0.2267, 0.2258],
         [0.2571, 0.2552, 0.2533, 0.2516]]])

某個軸計算 A 元素的累積總和

A.cumsum(axis = 0)
tensor([[[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.],
         [12., 13., 14., 15.],
         [16., 17., 18., 19.]],

        [[20., 22., 24., 26.],
         [28., 30., 32., 34.],
         [36., 38., 40., 42.],
         [44., 46., 48., 50.],
         [52., 54., 56., 58.]]])

點積是相同位置的按元素乘積的和

y = torch.ones(4,dtype = torch.float32)
x,y,torch.dot(x,y)
(tensor([0., 1., 2., 3.]), tensor([1., 1., 1., 1.]), tensor(6.))

矩陣向量積,此例子中使用了廣播機制

A = torch.arange(20).reshape(5,4)
A = A.float()
A,x
(tensor([[ 0.,  1.,  2.,  3.],
         [ 4.,  5.,  6.,  7.],
         [ 8.,  9., 10., 11.],
         [12., 13., 14., 15.],
         [16., 17., 18., 19.]]),
 tensor([0., 1., 2., 3.]))
A.shape,x.shape,torch.mv(A,x)
(torch.Size([5, 4]), torch.Size([4]), tensor([ 14.,  38.,  62.,  86., 110.]))

矩陣乘法

B = torch.ones(4,3)
torch.mm(A,B)
tensor([[ 6.,  6.,  6.],
        [22., 22., 22.],
        [38., 38., 38.],
        [54., 54., 54.],
        [70., 70., 70.]])

獲取向量L2範數:向量的長度

u = torch.tensor([3.0,-4.0])
torch.norm(u)
tensor(5.)

L1範數

torch.abs(u).sum()
tensor(7.)

矩陣的弗羅貝尼烏斯範數

torch.norm(torch.ones((4,9)))
tensor(6.)