• 动手学习深度学习-《线性代数实现》


    线性代数在torch中的实现

    可以将向量视为标量值组成的列表;
    通过张量的索引来访问任一元素。

    x=torch.arange(4)
    print(x[3]) # 输出:tensor(3)
    
    • 1
    • 2

    访问张量的长度

    len(x)
    
    • 1

    查看张量的形状

    x.shape
    
    • 1

    通过指定两个分量 m m m n n n来创建一个形状为 m × n m×n m×n的矩阵

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

    矩阵的转置

    A.T
    
    • 1

    就像向量是标量的推广,矩阵是向量的推广一样,张量是更高维度的矩阵,我们可以构建具有更多轴的数据结构

    X = torch.arange(24).reshape(2, 3, 4)
    
    '''
    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]]])
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    clone与copy的区别:

    clone是深拷贝,拷贝后改变对象不会影响原来的对象;
    copy是浅拷贝,拷贝后改变对象,拷贝前/后的对象都会一起被改变。

    A = torch.arange(12).reshape(3, 4)
    B = A.clone()
    
    B = B+A
    
    print("A",A)
    
    print("B",B)
    
    '''
    A tensor([[ 0,  1,  2,  3],
            [ 4,  5,  6,  7],
            [ 8,  9, 10, 11]])
    B tensor([[ 0,  2,  4,  6],
            [ 8, 10, 12, 14],
            [16, 18, 20, 22]])
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    两个矩阵的按元素乘法称为哈达玛积(Hadamard product)(数学符号 ⊙ ⊙

    A = torch.arange(12).reshape(3, 4)
    B = A.clone()
    
    print(A*B)
    '''
    tensor([[  0,   1,   4,   9],
            [ 16,  25,  36,  49],
            [ 64,  81, 100, 121]])
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    计算元素的和

    对所有元素进行求和

    A = torch.arange(24).reshape(2,3,4)
    
    print(A.sum()) #输出:tensor(276)
    
    • 1
    • 2
    • 3

    指定张量沿哪一个轴来通过求和降低维度
    【简单的理解为,axis指向哪个维度,哪个维度消失(拍扁)

    A = torch.arange(24).reshape(2,3,4)
    
    print(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]]])
    shape:(2,3,4)
    '''
    
    print(A.sum(axis=2))
    '''
    tensor([[ 6, 22, 38],
            [54, 70, 86]])
    shape:(2,3)
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    一个与求和相关的量是平均值(mean或average)

    A = torch.arange(24,dtype=torch.float32).reshape(2,3,4)
    
    print(A.mean())# A.sum()/A.numel()也可以
    '''
    tensor(11.5000)
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    指定维度求平均

    A = torch.arange(24,dtype=torch.float32).reshape(2,3,4)
    
    print(A.mean(axis=0)) #等价于A.sum(axis=0) / A.shape[0]
    '''
    tensor([[ 6.,  7.,  8.,  9.],
            [10., 11., 12., 13.],
            [14., 15., 16., 17.]])
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    某个轴计算A元素的累积总和

    A = torch.arange(24,dtype=torch.float32).reshape(2,3,4)
    
    print(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.]]])
    '''
    print(A.cumsum(axis=1))
    '''
    tensor([[[ 0.,  1.,  2.,  3.],
             [ 4.,  6.,  8., 10.],
             [12., 15., 18., 21.]],
    
            [[12., 13., 14., 15.],
             [28., 30., 32., 34.],
             [48., 51., 54., 57.]]])
    从把axis=1的维度拍扁了的视角来进行累加
    
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    点积是相同位置的按元素乘积的和

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

    矩阵和向量的乘积:torch.mv()

    A = torch.arange(12).reshape(3,4)
    x = torch.arange(4)
    
    print(torch.mv(A,x))
    '''
    tensor([14, 38, 62])
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    矩阵-矩阵乘法:torch.mm()

    A = torch.arange(12).reshape(3,4)
    B = A.T
    
    print(torch.mm(A,B))
    '''
    tensor([[ 14,  38,  62],
            [ 38, 126, 214],
            [ 62, 214, 366]])
    '''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    L 2 L_2 L2范数是向量元素平方和的平方根: torch.norm()
    在这里插入图片描述

    u = torch.tensor([3.0, -4.0])
    torch.norm(u)# tensor(5.)
    
    • 1
    • 2

    L 1 L_1 L1范数,它表示为向量元素的绝对值之和:
    在这里插入图片描述

    torch.abs(u).sum()
    '''
    tensor(7.)
    '''
    
    • 1
    • 2
    • 3
    • 4

    矩阵的弗罗贝尼乌斯范数(Frobenius norm)是矩阵元素平方和的平方根:torch.norm()
    在这里插入图片描述

    A = torch.arange(12,dtype=torch.float32).reshape(3,4)
    
    print(torch.norm(A))# tensor(22.4944)
    
    • 1
    • 2
    • 3
  • 相关阅读:
    MybatisX插件xml映射文件中命名空间爆红
    通信算法之九十六:电力通信系统-HRF多载波通信系统-物理层收发信道开发
    详解VSCode中C++工程配置
    Viola-Jones检测器(VJ)---学习笔记
    2022-06-17 网工进阶(九)IS-IS-原理、NSAP、NET、区域划分、网络类型、开销值
    Go-Excelize API源码阅读(二十四)——SetColVisible、SetRowHeight
    单纯形投影算法
    SpringMVC项目RESTful案例
    内网穿透 nas/树莓派+ipv4服务器 (ipv6)
    轻量级简约仪表板Dasherr
  • 原文地址:https://blog.csdn.net/GuoShao_/article/details/127712694