1. Element-wise Multiplication
*
torch.Tensor.mul()
torch.mul()
2. Matrix Multiplication
torch.Tensor.matmul()
torch.matmul()
torch.Tensor.mm()
torch.mm()
3. Batch Matrix Multiplication
torch.bmm()
示例:
1. torch.mul(a, b)或a*b是矩阵a和b对应位相乘,a和b的维度必须相等,比如a的维度是(1, 2),b的维度是(1, 2),返回的仍是(1, 2)的矩阵
2. torch.mm(a, b)是矩阵a和b矩阵相乘,比如a的维度是(1, 2),b的维度是(2, 3),返回的就是(1, 3)的矩阵
import torch
a = torch.rand(2, 3)
b = torch.rand(2, 3)
c = torch.rand(3, 6)
print(torch.mul(a, b)) # 返回 2*3 的tensor
print(torch.mm(a, c)) # 返回 2*6 的tensor
如果您觉得我的文章对您有所帮助,欢迎扫码进行赞赏!