深度学习(2)特征值、标量向量矩阵运算、线性代数

深度学习(2)特征值、标量向量矩阵运算、线性代数 参考github作者我只是用来学习。1. 特征值、特征向量求解① 下图为对矩阵求特征值、求特征向量的相关操作。2. 标量运算3. 向量运算① 下图中α是一个标量 是把向量b拉长。4. 矩阵运算① 矩阵长度叫范数下图为矩阵范数满足的公式。5. 特征向量、特征值① 红色、绿色向量都被一个矩阵作用后红色的大小和方向都被改变了绿色的仅被改变了大小没有改变方向。绿色向量为矩阵的特征向量。6. 线性代数6.1 标量① 标量由只有一个元素的张量表示。import torch x torch.tensor([3.0]) y torch.tensor([2.0]) print(x y) print(x * y) print(x / y) print(x ** y)tensor([5.]) tensor([6.]) tensor([1.5000]) tensor([9.])6.2 向量6.2.1 创建向量① 可以将向量视为标量值组成的列表。import torch x torch.arange(4) print(x)tensor([0, 1, 2, 3])6.2.2 访问向量元素① 通过张量的索引来访问任一元素。import torch x torch.arange(4) print(x[3]) # 索引从0开始tensor(3)6.2.3 访问向量长度① 访问张量的长度。import torch x torch.arange(4) print(len(x))46.2.4 访问向量维度① 只有一个轴的张量形状只有一个元素。import torch x torch.arange(4) print(x.shape)torch.Size([4])6.3 矩阵6.3.1 创建矩阵① 通过指定两个分量m和n来创建一个形状为m×n的矩阵。import torch A torch.arange(20).reshape(5,4) print(A)tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]])6.3.2 矩阵转置① 矩阵的转置。import torch A torch.arange(20).reshape(5,4) print(A) print(A.T) # 矩阵的转置tensor([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19]]) tensor([[ 0, 4, 8, 12, 16], [ 1, 5, 9, 13, 17], [ 2, 6, 10, 14, 18], [ 3, 7, 11, 15, 19]])6.3.3 对称矩阵① 对称矩阵symmetric matrixA 等于其转置import torch B torch.tensor([[1,2,3],[2,0,4],[3,4,5]]) print(B) print(B.T) print(B B.T)tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]]) tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]]) tensor([[True, True, True], [True, True, True], [True, True, True]])6.3.4 多维矩阵① 就像向量是标量的推广矩阵是向量的推广一样可以构建更多轴的数据结构。在1个三维张量中有两个二维矩阵每个而二维矩阵有3个向量每个向量有4个标量层 行 列import torch X torch.arange(24).reshape(2,3,4) print(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]]])6.3.5 矩阵克隆① 给定具有相同形状的任何两个张量任何按元素二元运算的结果都将是相同形状的张量。import torch A torch.arange(20,dtypetorch.float32).reshape(5,4) B A.clone() # 通过分配新内存将A的一个副本分配给B print(A) print(AB)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.]])6.3.6 矩阵相乘对应元素相乘① 两个句子的按元素乘法称为哈达玛积Hadamard product数学符号⊙import torch A torch.arange(20,dtypetorch.float32).reshape(5,4) B A.clone() # 通过分配新内存将A的一个副本分配给B print(A) print(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., 1., 4., 9.], [ 16., 25., 36., 49.], [ 64., 81., 100., 121.], [144., 169., 196., 225.], [256., 289., 324., 361.]])6.3.7 矩阵加标量import torch a 2 X torch.arange(24).reshape(2,3,4) print(a X) print((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])6.3.8 向量求和① 计算所有元素的和。import torch X torch.arange(4,dtypetorch.float32) print(X) print(X.sum())tensor([0., 1., 2., 3.]) tensor(6.)6.3.9 矩阵求和② 表示任意形状张量的元素和。import torch A torch.arange(20*2).reshape(2,5,4) print(A.shape) print(A.sum())torch.Size([2, 5, 4]) tensor(780)6.3.10 矩阵某轴求和维度丢失① 指定张量沿哪一个轴来通过求和降低维度。import torch A torch.arange(20*2).reshape(2,5,4) print(A) A_sum_axis0 A.sum(axis0) # (2,5,4) 对第一个维度进行求和剩下两个维度留下来了 print(A_sum_axis0) print(A_sum_axis0.shape)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]]]) 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])import torch A torch.arange(20*2).reshape(2,5,4) print(A) A_sum_axis1 A.sum(axis1) # (2,5,4) 对第二个维度进行求和剩下两个维度留下来了 print(A_sum_axis1) print(A_sum_axis1.shape)axis0求和把“楼层”这个维度压扁把两层楼叠在一起加计算过程 第0层 [a00, a01, a02, a03] 第1层 [b00, b01, b02, b03] [a00b00, a01b01, a02b02, a03b03] 对每一行每一列都这样加axis1求和把每层里的“行”这个维度压扁计算过程第0层 第0行 [0,1,2,3] 第1行 [4,5,6,7] 第2行 [8,9,10,11] 第3行 [12,13,14,15] 第4行 [16,17,18,19] 加起来 [0481216, 1591317, 26101418, 37111519]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]]]) tensor([[ 40, 45, 50, 55], [140, 145, 150, 155]]) torch.Size([2, 4])import torch A torch.arange(20*2).reshape(2,5,4) print(A) A_sum_axis1 A.sum([0,1]) # (2,5,4) 对第一、二个维度进行求和剩下一个维度留下来了 print(A_sum_axis1) print(A_sum_axis1.shape)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]]]) tensor([180, 190, 200, 210]) torch.Size([4])6.3.11 矩阵平均值① 一个与求和相关的量是平均值mean或average。代码含义结果形状例子结果A.mean()全局均值()标量3.5A.mean(axis0)对第0维求平均(5,4)按列平均A.mean(axis1)对第1维求平均(2,4)按行平均A.mean([0,1])对第0、1维求平均(4,)保留第2维A.mean() 等价于 A.sum() / A.numel()import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) print(A.mean()) print(A.numel()) print(A.sum()/A.numel())tensor(9.5000) 20 tensor(9.5000)import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) print(A.mean(axis0)) print(A.sum(axis0)) print(A.shape[0]) print(A.sum(axis0)/A.shape[0])tensor([ 8., 9., 10., 11.]) tensor([40., 45., 50., 55.]) 5 tensor([ 8., 9., 10., 11.])6.3.12 矩阵某轴求和维度不丢失① 计算总和或均值时保持轴数不变。import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) sum_A A.sum(axis1,keepdimsTrue) # keepdimsTrue不丢掉维度否则三维矩阵按一个维度求和就会变为二维矩阵二维矩阵若按一个维度求和就会变为一维向量 print(sum_A) print(sum_A.shape) # 维度没有丢失方便使用广播tensor([[ 6.], [22.], [38.], [54.], [70.]]) torch.Size([5, 1])6.3.13 矩阵广播① 通过广播将 A 除以 sum_A。import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) sum_A A.sum(axis1,keepdimsTrue) # keepdimsTrue不丢掉维度否则三维矩阵按一个维度求和就会变为二维矩阵二维矩阵若按一个维度求和就会变为一维向量 print(A/sum_A)tensor([[0.0000, 0.1667, 0.3333, 0.5000], [0.1818, 0.2273, 0.2727, 0.3182], [0.2105, 0.2368, 0.2632, 0.2895], [0.2222, 0.2407, 0.2593, 0.2778], [0.2286, 0.2429, 0.2571, 0.2714]])6.3.14 矩阵某轴累加① 某个轴计算A元素的累加总和。import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) print(A.cumsum(axis0))这里是按行累加的即向下累加tensor([[ 0., 1., 2., 3.], [ 4., 6., 8., 10.], [12., 15., 18., 21.], [24., 28., 32., 36.], [40., 45., 50., 55.]])6.3.15 向量点积① 点积是相同位置的按元素成绩的和。import torch x torch.arange(4,dtypetorch.float32) y torch.ones(4, dtypetorch.float32) print(x) print(y) print(torch.dot(x,y))tensor([0., 1., 2., 3.]) tensor([1., 1., 1., 1.]) tensor(6.)② 可以通过执行按元素乘法然后进行求和来表示两个向量的点积。import torch x torch.arange(4,dtypetorch.float32) y torch.ones(4, dtypetorch.float32) print(torch.sum(x*y))tensor(6.)6.3.16 矩阵向量积① A是一个m×n的矩阵x是一个n×1的矩阵矩阵向量积是一个长度为m的列向量其第i个元素是点积。import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) x torch.arange(4,dtypetorch.float32) print(A.shape) print(x.shape) print(torch.mv(A,x))torch.Size([5, 4]) torch.Size([4]) tensor([ 14., 38., 62., 86., 110.])6.3.17 矩阵相乘线性代数相乘① 可以将矩阵-矩阵乘法AB看作是简单地执行m次矩阵-向量积并将结果拼接在一起形成一个n×m矩阵。import torch A torch.arange(20, dtypetorch.float32).reshape(5, 4) B torch.ones(4,3) print(A) print(B) print(torch.mm(A,B))tensor([[ 0., 1., 2., 3.], [ 4., 5., 6., 7.], [ 8., 9., 10., 11.], [12., 13., 14., 15.], [16., 17., 18., 19.]]) tensor([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]) tensor([[ 6., 6., 6.], [22., 22., 22.], [38., 38., 38.], [54., 54., 54.], [70., 70., 70.]])6.3.18 矩阵L2范数① L2 范数是向量元素平方和的平方根import torch u torch.tensor([3.0,-4.0]) print(torch.norm(u))tensor(5.)torch.norm() 要求输入必须是 浮点数或复数类型6.3.19 矩阵L1范数① 范数它表示为向量元素的绝对值之和import torch u torch.tensor([3.0,-4.0]) print(torch.abs(u).sum())tensor(7.)6.3.20 矩阵F范数① 矩阵的弗罗贝尼乌斯范数Frobenius norm是矩阵元素的平方和的平方根import torch print(torch.norm(torch.ones((4,9)))) # 把矩阵拉成一个向量然后再求和tensor(6.)