一句话,bmm()
进行批量矩阵乘法,输入的张量只能是3维的。matmul()
强大的多借助广播机制,1维、2维、3维、4维的张量都行。
当bmm()
输入的张量不是3维的时候:RuntimeError: batch1 must be a 3D tensor
matmul()
1维,向量点积
>>> # vector x vector
>>> tensor1 = torch.randn(3)
>>> tensor2 = torch.randn(3)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([])
矩阵x向量
>>> # matrix x vector
>>> tensor1 = torch.randn(3, 4)
>>> tensor2 = torch.randn(4)
>>> torch.matmul(tensor1, tensor2).size() # 相当于这样形状3x4 4x1的矩阵相乘
torch.Size([3])
>>> # vector x matrix
>>> tensor1 = torch.randn(4)
>>> tensor2 = torch.randn(4, 3)
>>> torch.matmul(tensor1, tensor2).size() # 相当于这样形状1x4 4x3的矩阵相乘
torch.Size([3])
>>> # batched matrix x broadcasted vector
>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(4)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3])
>>> # batched matrix x batched matrix
>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(10, 4, 5)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3, 5])
>>> # batched matrix x broadcasted matrix
>>> tensor1 = torch.randn(10, 3, 4)
>>> tensor2 = torch.randn(4, 5)
>>> torch.matmul(tensor1, tensor2).size()
torch.Size([10, 3, 5])
维度更高的
t2 = torch.rand((1,16,2,3))
t3 = torch.rand((1,16,3,2))
print(torch.matmul(t2, t3).shape)
# torch.Size([1, 16, 2, 2])
t8 = torch.rand((1,16,15,2,3))
t9 = torch.rand((1,16,15,3,2))
print(torch.matmul(t8, t9).shape)
# torch.Size([1, 16, 15, 2, 2])