- element-wise multiplication/按元素位置相乘
a.mul(b)
a * b
import torch
a = torch.tensor((1,2,3,4))
b = torch.tensor((5,6,7,8))
a * b # same as: a.mul(b)
# output: tensor([ 5, 12, 21, 32])
- dot product/matrix multiplication/矩阵点乘
torch.mm(a,b)
torch.bmm(a,b) # b means batch
torch.matmul() # 支持broadcast,一般用后两维计算,前面认为是batch
a @ b
import torch
a = torch.tensor(((1,2,3,4),(4,3,2,1)))
b = torch.tensor(((5,6,7,8),(8,7,6,5)))
a @ b.T
# output: tensor([[70, 60],
# [60, 70]])