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

你可能感兴趣的:([pytorch] 通过一个例子分析torch.matmul矩阵与向量相乘的维度)