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

10 篇文章 1 订阅
5 篇文章 0 订阅

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

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

 
  1. >>a = torch.arange(0,18).view(2,3,3)

  2. >>b = torch.ones(3,dtype=torch.long)

  3. >>c = torch.matmul(a,b)

  4. >>d = torch.matmul(b,a)

  5. >>a.size()

  6. torch.Size([2, 3, 3])

  7. >>b.size()

  8. torch.Size([3])

  9. >>c.size()

  10. torch.Size([2, 3])

  11. >>d.size()

  12. torch.Size([2, 3])

  13. >>a[0]

  14. tensor([[0, 1, 2],

  15. [3, 4, 5],

  16. [6, 7, 8]])

  17. >>a[1]

  18. tensor([[ 9, 10, 11],

  19. [12, 13, 14],

  20. [15, 16, 17]])

  21. >>c

  22. tensor([[ 3, 12, 21],

  23. [30, 39, 48]])

  24. >>d

  25. tensor([[ 9, 12, 15],

  26. [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].

来源:https://blog.csdn.net/CVAIDL/article/details/107752697

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值