pytorch 学习5-线性代数基础

标量:

  • 简单操作

  1. c=a+b
  2. c=a.b
  3. c=\sina  
  • 长度

  1. \left |a \right | = \begin{cases} a & \text{ if } a>0 \\ -a & \text{ if } otherwise \end{cases}
  2. \left |a+b \right | \leq \left |a \right | + \left |b \right |
  3. \left |a*b \right | \leq \left |a \right | * \left |b \right |

向量

  • 简单操作

  1. c=a+b       where c_{i} = a_{i} + b_{i}
  2. c=\alphab         where c_{i} = \alpha b_{i}
  3. c=\sin a     where c_{i} = \sin a_{i}
  • 长度

  1. \left \| a \right \|\l_{2} = \left [ \sum_{i=1}^{m} a_{i}^{2}\right ]^{\frac{1}{2}}
  2. \left \| a \right \|\l \geq 0 for all a
  3. \left \| a+b \right \|\l\leq \left \| a \right \| + \left \| b \right \|
  4. \left \| ab \right \|\l =\left | a \right | \left \| b \right \|
  5. 点乘   a^{T}b = \sum_{i}^{}a_{i}b_{i}        点积(dot product)  a^{T}b或<a,b>,相同位置的按元素乘积的和
    import torch
    
    x = torch.arange(4, dtype=torch.float32)
    y = torch.ones(4, dtype=torch.float32)
    z = torch.dot(x, y)
    print(x, y, z)
    

    输出:    两个向量的点积为 元素乘法然后求和。

    tensor([0., 1., 2., 3.]) 
    tensor([1., 1., 1., 1.]) 
    tensor(6.)

  6. 正交  a^{T}b = \sum_{i}^{}a_{i}b_{i}=0

矩阵

  • 简单操作

  1. C=A+B     where C_{ij} = A_{ij} + B_{ij}
  2. C=\alphaB         where C_{ij} = \alpha B_{ij}
  3. C=\sin A     where C_{ij} = \sin A_{ij}
  • 乘法

  1. 矩阵向量积(矩阵乘以向量)使用mv函数      其中,A的列维必须与b的维数长度一致。
    import torch
    
    x = torch.arange(4, dtype=torch.float32)
    A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
    print(A.shape, x.shape, torch.mv(A, x))

    输出: 

    torch.Size([5, 4]) torch.Size([4]) tensor([ 14.,  38.,  62.,  86., 110.])
  2. 矩阵乘以矩阵
  3. 范数   c=Ab hence \left \| c \right \| \leq \left \| A \right \|\left \| b \right \|          取决于如何衡量b和c的长度.。常见范数   :
  • 矩阵范数:最小的满足公式的值
  • Frobenius范数    \left \| A \right \|_{Frob} = \left [ \sum_{ij}^{}A_{ij}^{2}\right ]\tfrac{1}{2}

线性代数的实现:

标量由只有一个元素的张量表示

创建m*n的矩阵

# 通过指定m,n创建形状为m*n的矩阵
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]])

矩阵的转置:

# 矩阵的转置
print(A.T)

输出

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

对称矩阵(symmetric matrix)A=A^{T}

# 对称矩阵
B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])
print(B)
print("B==B.T", B == B.T)

输出:

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

哈达玛积(Hadamard product)(数学符号\odot)

# 哈达玛积
B = A.clone()   #通过分配新内存,将A拷贝给B
print("A*B=", A * B)
a = 2
X = torch.arange(24).reshape(2, 3, 4)
print("a+X", a + X)
print("(a*X)形状", (a + X).shape)

输出

A*B= 
tensor([[  0,   1,   4,   9],
        [ 16,  25,  36,  49],
        [ 64,  81, 100, 121],
        [144, 169, 196, 225],
        [256, 289, 324, 361]])
a+X 
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]]])
(a*X)形状 torch.Size([2, 3, 4])

矩阵向量积

# 通过指定m,n创建形状为m*n的矩阵
A = torch.arange(20).reshape(5, 4)
print(A)
x = torch.arange(4)
print(x)
# 矩阵向量积
print(A.shape, x.shape)
print(torch.mv(A, x))

输出  mv():  a_{ij}^{T}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])
torch.Size([5, 4]) torch.Size([4])
tensor([ 14,  38,  62,  86, 110])

矩阵间的乘法AB

# 通过指定m,n创建形状为m*n的矩阵
A = torch.arange(20, dtype=torch.float32).reshape(5, 4)
B = torch.ones(4, 3)
# 矩阵间的乘法
print(B, B.shape)
print(torch.mm(A, B))

输出

tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]]) torch.Size([4, 3])
tensor([[ 6.,  6.,  6.],
        [22., 22., 22.],
        [38., 38., 38.],
        [54., 54., 54.],
        [70., 70., 70.]])

矩阵的L_{2}范数

L_{2}范数是向量元素平方和的平方根

# L2范数   向量元素平方和的平方根
u = torch.tensor([3.0, -4.0])
print(torch.norm(u))

输出:

tensor(5.)

矩阵的L_{1}范数

L_{1}范数是向量元素的绝对值之和

# L1范数 向量元素的绝对值之和
print(torch.abs(u).sum())

输出

tensor(7.)

矩阵的佛罗贝尼乌斯范数(Frobenius norm)

矩阵元素的平方和的平方根

# 佛罗贝尼乌斯范数
print(torch.norm(torch.ones((4, 9))))

输出

tensor(6.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值