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

线性代数在torch中的实现

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

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

访问张量的长度

len(x)

查看张量的形状

x.shape

通过指定两个分量 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]])
'''

矩阵的转置

A.T

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

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]]])
'''

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]])
'''

两个矩阵的按元素乘法称为哈达玛积(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]])
'''

计算元素的和

对所有元素进行求和

A = torch.arange(24).reshape(2,3,4)

print(A.sum()) #输出:tensor(276)

指定张量沿哪一个轴来通过求和降低维度
【简单的理解为,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)
'''

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

A = torch.arange(24,dtype=torch.float32).reshape(2,3,4)

print(A.mean())# A.sum()/A.numel()也可以
'''
tensor(11.5000)
'''

指定维度求平均

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.]])
'''

某个轴计算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的维度拍扁了的视角来进行累加

'''

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

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.))
'''

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

A = torch.arange(12).reshape(3,4)
x = torch.arange(4)

print(torch.mv(A,x))
'''
tensor([14, 38, 62])
'''

矩阵-矩阵乘法: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]])
'''

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

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

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

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

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

A = torch.arange(12,dtype=torch.float32).reshape(3,4)

print(torch.norm(A))# tensor(22.4944)
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值