import torch
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[2, 1], [4, 3]])
print("x_shape", x.shape)
print("y_shape", y.shape)
c = x@y
print("c_shape", c.shape)
print(c)
# 结果:
x_shape torch.Size([2, 2])
y_shape torch.Size([2, 2])
c_shape torch.Size([2, 2])
tensor([[10, 7],
[22, 15]])
import torch
x = torch.tensor([[1, 2], [3, 4]])
y = torch.tensor([[2, 1], [4, 3]])
print("x_shape", x.shape)
print("y_shape", y.shape)
d = x*y
print("d_shape", d.shape)
print(d)
# 结果:
x_shape torch.Size([2, 2])
y_shape torch.Size([2, 2])
d_shape torch.Size([2, 2])
tensor([[ 2, 2],
[12, 12]])
所以,@ 和 * 代表矩阵的两种相乘方式:
@ 表示常规的数学上定义的矩阵相乘;
* 表示两个矩阵对应位置处的两个元素相乘