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

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

 
  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

你可能感兴趣的:(pytorch,深度学习,python)