在PyTorch中,@符号用作矩阵乘法的运算符,这与Python 3.5及以上版本引入的矩阵乘法运算符一致。@效果等同于使用torch.matmul()或torch.mm()函数(后者仅适用于二维张量)。
相乘的两个矩阵必须满足:矩阵A的列数必须与矩阵B的行数相匹配。
假设有两个张量A和B,你想计算它们的矩阵乘积,
- import torch
-
-
- A = torch.tensor([[1, 2],
- [3, 4]])
-
- B = torch.tensor([[2, 0],
- [0, 2]])
-
- C = A @ B
-
- print(C)
-
- """
- tensor([[2, 4],
- [6, 8]])
- """