python矩阵和向量乘法总结

向量之间的乘法

torch.dot

点乘,相同维度的两个向量对应元素相乘再相加

torch.mul

对应元素相乘,结果同a*b
例子如下:
在这里插入图片描述

矩阵与向量的乘法

要求:矩阵的列数=向量的维数
结果:矩阵乘法
函数:torch.mv(A,a)

a = torch.tensor([1,2,3])
A = torch.arange(12).reshape(4,3)
torch.mv(A,a)

结果:

tensor([ 8, 26, 44, 62])

矩阵与矩阵的乘法

torch.mm

要求:两个二维矩阵相乘

A = torch.arange(12).reshape(4,3)
B = torch.arange(15).reshape(3,5)
torch.mm(A,B)

结果:

tensor([[ 25,  28,  31,  34,  37],
        [ 70,  82,  94, 106, 118],
        [115, 136, 157, 178, 199],
        [160, 190, 220, 250, 280]])

torch.matmul

torhc.matmul()几乎可以应用与所有矩阵向量乘积的情况

两个一维向量相乘

相当于torch.dot(),计算两个向量的内积

a = torch.tensor([1,2,3])
b = torch.tensor([2,3,4])
torch.matmul(a,b)
>>> tensor(20)

二维矩阵和向量相乘

相当于torch.mv(),计算矩阵乘法

a = torch.tensor([1,2,3])
A = torch.arange(12).reshape(4,3)
torch.matmul(A,a)
>>>tensor([ 8, 26, 44, 62])

两个二维矩阵相乘

相当于torch.mm(),计算矩阵乘法

A = torch.arange(12).reshape(4,3)
B = torch.arange(15).reshape(3,5)
torch.matmul(A,B)
>>> tensor([[ 25,  28,  31,  34,  37],
        [ 70,  82,  94, 106, 118],
        [115, 136, 157, 178, 199],
        [160, 190, 220, 250, 280]])

向量和二维矩阵相乘

当向量(长度为n),矩阵(nxm)时,会自动在向量的最前面增加一个维度

arg1 = torch.tensor([-1,2])
arg2 = torch.tensor([
    [1,2],[3,4]
])
torch.matmul(arg1,arg2)
>>>tensor([5, 6])

结果同下运算:
在这里插入图片描述

多维矩阵相乘

如果两个参数均至少为一维,且其中一个参数的 ndim > 2,那么……(一番处理),然后进行批量矩阵乘法。

这条规则将所有涉及到三维张量及三维以上的张量(下文称为高维张量)的乘法分为三类:一维张量 × 高维张量、高维张量 × 一维张量、二维及二维以上的张量 × 二维及二维以上的张量。

如果第一个参数是一维张量,那么在此张量之前增加一个维度。

文档原文为:“ If the first argument is 1-dimensional, a 1 is prepended to its dimension for the purpose of the batched matrix multiply and removed after.”

如果第二个参数是一维张量,那么在此张量之后增加一个维度。

文档原文为:“If the second argument is 1-dimensional, a 1 is appended to its dimension for the purpose of the batched matrix multiple and removed after. ”

由于上述两个规则,所有涉及到一维张量和高维张量的乘法都被转变为二维及二维以上的张量 × 二维及二维以上的张量。

然后除掉最右边的两个维度,对剩下的维度进行广播。原文为:“The non-matrix dimensions are broadcasted.”

然后就可以进行批量矩阵乘法。

For example, if input is a (j × 1 × n × n) tensor and other is a (k × n × n) tensor, out will be a (j × k × n × n) tensor.

举例如下:

>>> arg1 = torch.tensor([1, 2, -1, 1])
>>> arg2 = torch.randint(low=-2, high=3, size=[3, 4, 1])
>>> torch.matmul(arg1, arg2)
tensor([[ 5],
        [-1],
        [-1]])
        
>>> arg2
tensor([[[ 2],
         [ 2],
         [-1],
         [-2]],

        [[-2],
         [ 2],
         [ 1],
         [-2]],

        [[ 0],
         [ 0],
         [-1],
         [-2]]])

根据第一条规则,先对 arg1 增加维度:

>>> arg3 = torch.unsqueeze(arg1, 0)
>>> arg3
tensor([[ 1,  2, -1,  1]])
>>> arg3.shape
torch.Size([1, 4])

由于 arg2.shape=torch.Size([3, 4, 1]) ,根据广播的规则,arg3 要被广播为 torch.Size([3, 1, 4]) ,也就是下面的 arg4。

>>> arg4 = torch.tensor([ [[ 1,  2, -1,  1]], [[ 1,  2, -1,  1]], [[ 1,  2, -1,  1]] ])
>>> arg4
tensor([[[ 1,  2, -1,  1]],

        [[ 1,  2, -1,  1]],

        [[ 1,  2, -1,  1]]])
>>> arg4.shape
torch.Size([3, 1, 4])

最后我们使用乘法函数 torch.bmm() 来进行批量矩阵乘法:

>>> torch.bmm(arg4, arg2)
tensor([[[ 5]],

        [[-1]],

        [[-1]]])

由于在第一条规则中对一维张量增加了维度,因此矩阵计算结束后要移除这个维度。移除之后和前面使用 torch.matmul() 的结果相同!

参考:PyTorch 中 torch.matmul() 函数的文档详解

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值