[pytorch] 通过一个例子分析torch.matmul矩阵与向量相乘的维度

pytorch文档中关于torch.matmul()的维度说明如下:

 

  • If both tensors are 1-dimensional, the dot product (scalar) is returned.
  • If both arguments are 2-dimensional, the matrix-matrix product is returned.
  • If the first argument is 1-dimensional and the second argument is 2-dimensional, a 1 is prepended to its dimension for the purpose of the matrix multiply. After the matrix multiply, the prepended dimension is removed.
  • If the first argument is 2-dimensional and the second argument is 1-dimensional, the matrix-vector product is returned.
  • If both arguments are at least 1-dimensional and at least one argument is N-dimensional (where N > 2), then a batched matrix multiply is returned. 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 (i.e. batch) dimensions are broadcasted (and thus must be broadcastable).

最后一项可能光看文档难以理解,下面举个例子:

>>a = torch.arange(0,18).view(2,3,3)
>>b = torch.ones(3,dtype=torch.long)
>>c = torch.matmul(a,b)
>>d = torch.matmul(b,a)
>>a.size()
torch.Size([2, 3, 3])
>>b.size()
torch.Size([3])
>>c.size()
torch.Size([2, 3])
>>d.size()
torch.Size([2, 3])
>>a[0]
tensor([[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]])
>>a[1]
tensor([[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]])
>>c
tensor([[ 3, 12, 21],
        [30, 39, 48]])
>>d
tensor([[ 9, 12, 15],
        [36, 39, 42]])

通过分析上面的结果我们可以发现,c = torch.matmul(a,b)等价于将a[0]和a[1]这两个(3,3)维的矩阵与b这个3维向量相乘:a[0]×b和a[1]×b,而d = torch.matmul(b,a)只是相乘顺序相反:b×a[0]和b×a[1].

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值