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.)
原文地址:https://www.cnblogs.com/MurasameLory-chenyulong/p/14906228.html