torch.mul(input, other)是将input和other的对应位相乘,other可以是张量,可以是数字。
torch.mul(input, other)中input和bother维度相等,但是,对应维度上的数字可以不同,可以用利用广播机制扩展到相同的形状,再进行点乘操作。
tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
点乘示例:
print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
print(f"tensor * tensor \n {tensor * tensor} \n")
print(f"torch.mul(tensor,100) \n {torch.mul(tensor,10)} \n")
print(f"torch.mul(tensor*2,tensor) \n {torch.mul(tensor*2,tensor)} \n")
输出:
tensor.mul(tensor)
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
tensor * tensor
tensor([[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.],
[1., 0., 1., 1.]])
torch.mul(tensor,100)
tensor([[10., 0., 10., 10.],
[10., 0., 10., 10.],
[10., 0., 10., 10.],
[10., 0., 10., 10.]])
torch.mul(tensor*2,tensor)
tensor([[2., 0., 2., 2.],
[2., 0., 2., 2.],
[2., 0., 2., 2.],
[2., 0., 2., 2.]])
print(f"tensor.mm(tensor.T) \n {tensor.mm(tensor.T)} \n")
print(f"torch.mm(tensor,tensor.T) \n {tensor @ tensor.T} \n")
print(f"tensor @ tensor.T \n {tensor @ tensor.T} \n")
输出:
tensor.mm(tensor.T)
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
torch.mm(tensor,tensor.T)
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
tensor @ tensor.T
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
操作取决于张量的维度,如下所示:
请注意,广播逻辑在确定输入是否可广播时仅查看批次维度,而不是矩阵维度。例如,如果input是 ( j×1×n×m)张量并且other是( k×m×p ) 张量,即使最后两个维度(即矩阵维度)不同,这些输入也可用于广播。out将是一个( j×k×n×p ) 张量。
# vector x vector
tensor1 = torch.randn(3)
tensor2 = torch.randn(3)
torch.matmul(tensor1, tensor2).size()
# # torch.Size([])
# matrix x vector
tensor1 = torch.randn(3, 4)
tensor2 = torch.randn(4)
torch.matmul(tensor1, tensor2).size()
# # torch.Size([3])
# vector x matrix
tensor1 = torch.randn(4) # 1*4
tensor2 = torch.randn(4, 3)
print(torch.matmul(tensor1, tensor2).size())
# # torch.Size([3])
# batched matrix x broadcasted vector
tensor1 = torch.randn(10, 3, 4) # 30*4
tensor2 = torch.randn(4) # 4*1
torch.matmul(tensor1, tensor2).size()
# # torch.Size([10, 3])
# batched matrix x batched matrix
# 将b的第0维broadcast成10提出来,后两维做矩阵乘法即可。
tensor1 = torch.randn(10, 3, 4) # 10, 3*4
tensor2 = torch.randn(10, 4, 5) # 10, 4*5
torch.matmul(tensor1, tensor2).size()
# # torch.Size([10, 3, 5])
# batched matrix x broadcasted matrix
tensor1 = torch.randn(10, 3, 4) # 30*4
tensor2 = torch.randn(4, 5) # 4*5
torch.matmul(tensor1, tensor2).size()
# # torch.Size([10, 3, 5])