矩阵相乘详解
-
矩阵相乘详解
已知三个矩阵A,B,CA,B,CA,B,C
数学上的矩阵相乘 C = A ×\times× B
数学表示
程序表示
多维矩阵:torch.matmul(A,B)
if: A∈Rn×m,B∈Rm×nA\in R^{n\times m},B\in R^{m\times n}A∈Rn×m,B∈Rm×n
then: torch.matmul(A, B) ∈Rn×n\in R^{n\times n}∈Rn×n
二维矩阵相乘:torch.mm(A,B)
# 矩阵相乘 x = tensor([[1, 2, 3], [3, 3, 4], [3, 3, 3]]) # torch.matmul表示矩阵的乘法 torch.matmul(x,x) Out[1]: tensor([[16, 17, 20], [24, 27, 33], [21, 24, 30]]) # 两个维度对上就可以进行运算 x = tensor([[1, 2, 3], [3, 3, 4], [3, 3, 3]]) y = tensor([[1, 2], [3, 3], [4, 4]]) torch.matmul(x, y) Out[2]: tensor([[19, 20], [28, 31], [24, 27]])
数学上的矩阵对位相乘
数学表示
程序表示
torch.mul(A,B)
# 表示矩阵对位相乘 x = tensor([[1, 2, 3], [3, 3, 4], [3, 3, 3]]) # 方法1 x * x Out[3]: tensor([[ 1, 4, 9], [ 9, 9, 16], [ 9, 9, 9]]) # 方法2 torch.mul(x,x) Out[4]: tensor([[ 1, 4, 9], [ 9, 9, 16], [ 9, 9, 9]])
带有batch的三维就一阵相乘
torch.bmm(A, B)
A∈RB×n×mA\in R^{B\times n\times m}A∈RB×n×m,B∈RB×m×dB\in R^{B\times m\times d}B∈RB×m×d
torch.bmm(A, B) ∈RB×n×d\in R^{B\times n\times d}∈RB×n×d
t = tensor([[[1, 2, 3], [3, 3, 4], [3, 3, 3]], [[1, 2, 3], [3, 3, 4], [3, 3, 3]]]) T = torch.bmm(t, t) T.shape Out[5]: torch.Size([2, 3, 3]) T Out[6]: tensor([[[16, 17, 20], [24, 27, 33], [21, 24, 30]], [[16, 17, 20], [24, 27, 33], [21, 24, 30]]]) # 两个维度不同 u = tensor([[[1, 2], [3, 3], [4, 4]], [[1, 2], [3, 3], [4, 4]]]) t = tensor([[[1, 2, 3], [3, 3, 4], [3, 3, 3]], [[1, 2, 3], [3, 3, 4], [3, 3, 3]]]) u.shape Out[7]: torch.Size([2, 3, 2]) t.shape Out[8]: torch.Size([2, 3, 3]) torch.bmm(t, u) Out[9]: tensor([[[19, 20], [28, 31], [24, 27]], [[19, 20], [28, 31], [24, 27]]]) torch.bmm(t, u).shape Out[10]: torch.Size([2, 3, 2])