预备知识:矩阵的各种乘积
torch.mul: 两个输入按元素相乘,内积
分两种情况,默认其中一个是tensor
torch.mm: 特指二维矩阵乘法,外积
If input is (n×m)
tensor, mat2 is a (m×p)
tensor, out will be a (n×p)
tensor.
bonus: torch.bmm就是加了一维batch_size, 也是一样的,只能说3维的两个向量,而且必须符合boardcasted规则
torch.matmul: 两个tensor的矩阵乘积,与mm的区别就是不限制是不是二维,符合broadcasted规则的都可以
例如
if input is a (j×1×n×n)
tensor and other is a(k×n×n)
tensor, out will be a (j×k×n×n)
tensor.
import torch
num=10
mat1=torch.randn(2,3)
mat2=torch.randn(3,3)
mat3=torch.randn(3,2,3)
#
print("torch.mul的结果1\n",torch.mul(mat1,num))
print("torch.mul的结果2\n",torch.mul(mat1,mat1))
print("torch.mm的结果[二维矩阵相乘]\n",torch.mm(mat1,mat2))
print("torch.matmul的结果[二维矩阵相乘]\n",torch.matmul(mat3,mat2))
'''
torch.mul的结果1
tensor([[ 3.9774, 8.8436, -0.6948],
[20.7512, 6.7674, 8.6104]])
torch.mul的结果2
tensor([[0.1582, 0.7821, 0.0048],
[4.3061, 0.4580, 0.7414]])
torch.mm的结果[二维矩阵相乘]
tensor([[-0.4850, -1.0539, -0.9389],
[-0.4043, -3.4724, 0.1518]])
torch.matmul的结果[二维矩阵相乘]
tensor([[[-1.8273, -2.6934, -0.2099],
[-0.7700, 0.9479, 2.2710]],
[[-0.4027, 1.9117, 0.0667],
[ 1.1908, 1.0069, -2.3486]],
[[-1.1636, 0.1499, 3.8286],
[ 1.0558, -1.7520, 0.8731]]])
'''
注意:
*,@是两个运算符,他们分别映射到函数torch.mul和torch.matmul()