The difference between pytorch study notes torch.mul() and torch.mm()

The difference between torch.mul() and torch.mm() in pytorch

torch.mul(a, b) is the multiplication of the corresponding bits of matrix a and b. The dimensions of a and b must be equal. For example, the dimension of a is (1, 2) and the dimension of b is (1, 2). Is a matrix of (1, 2)

torch.mm(a, b) is the matrix multiplication of matrix a and b. For example, the dimension of a is (1, 2), the dimension of b is (2, 3), and the returned matrix is ​​(1, 3)

import torch

a = torch.rand(1, 2)
b = torch.rand(1, 2)
c = torch.rand(2, 3)

print(torch.mul(a, b))  # 返回 1*2 的tensor
print(torch.mm(a, c))   # 返回 1*3 的tensor
print(torch.mul(a, c))  # 由于a、b维度不同,报错

Guess you like

Origin blog.csdn.net/m0_45388819/article/details/109907583