已知三个矩阵 A , B , C A,B,C A,B,C
多维矩阵:torch.matmul(A,B)
if: A ∈ R n × m , B ∈ R m × n A\in R^{n\times m},B\in R^{m\times n} A∈Rn×m,B∈Rm×n
then: torch.matmul(A, B) ∈ R n × 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]])
torch.bmm(A, B)
A ∈ R B × n × m A\in R^{B\times n\times m} A∈RB×n×m, B ∈ R B × m × d B\in R^{B\times m\times d} B∈RB×m×d
torch.bmm(A, B) ∈ R B × 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])