这里总结一下pytorch常用的乘法运算以及相关的运算符(@、*)。
总结放前面:
import torch
a = torch.ones(1, 2)
print(a)
b = torch.ones(2, 3)
print(b)
output = torch.mm(a, b)
print(output)
print(output.size())
"""
tensor([[1., 1.]])
tensor([[1., 1., 1.],
[1., 1., 1.]])
tensor([[2., 2., 2.]])
torch.Size([1, 3])
"""
a = torch.randn(2, 1, 2)
print(a)
b = torch.randn(2, 2, 3)
print(b)
output = torch.bmm(a, b)
print(output)
print(output.size())
"""
tensor([[[-0.1187, 0.2110]],
[[ 0.7463, -0.6136]]])
tensor([[[-0.1186, 1.5565, 1.3662],
[ 1.0199, 2.4644, 1.1630]],
[[-1.9483, -1.6258, -0.4654],
[-0.1424, 1.3892, 0.7559]]])
tensor([[[ 0.2293, 0.3352, 0.0832]],
[[-1.3666, -2.0657, -0.8111]]])
torch.Size([2, 1, 3])
"""
a = torch.ones(2, 3) * 2
print(a)
b = torch.randn(2, 3)
print(b)
output = torch.mul(a, b)
print(output)
print(output.size())
"""
tensor([[2., 2., 2.],
[2., 2., 2.]])
tensor([[-0.1187, 0.2110, 0.7463],
[-0.6136, -0.1186, 1.5565]])
tensor([[-0.2375, 0.4220, 1.4925],
[-1.2271, -0.2371, 3.1130]])
torch.Size([2, 3])
"""
mat = torch.randn(3, 4)
print(mat)
vec = torch.randn(4)
print(vec)
output = torch.mv(mat, vec)
print(output)
print(output.size())
print(torch.mm(mat, vec.unsqueeze(1)).squeeze(1))
"""
tensor([[-0.1187, 0.2110, 0.7463, -0.6136],
[-0.1186, 1.5565, 1.3662, 1.0199],
[ 2.4644, 1.1630, -1.9483, -1.6258]])
tensor([-0.4654, -0.1424, 1.3892, 0.7559])
tensor([ 0.5982, 2.5024, -5.2481])
torch.Size([3])
tensor([ 0.5982, 2.5024, -5.2481])
"""
# 其作用包含torch.mm、torch.bmm和torch.mv。其他类似,不一一举例。
a = torch.randn(2, 1, 2)
print(a)
b = torch.randn(2, 2, 3)
print(b)
output = torch.bmm(a, b)
print(output)
output1 = torch.matmul(a, b)
print(output1)
print(output1.size())
"""
tensor([[[-0.1187, 0.2110]],
[[ 0.7463, -0.6136]]])
tensor([[[-0.1186, 1.5565, 1.3662],
[ 1.0199, 2.4644, 1.1630]],
[[-1.9483, -1.6258, -0.4654],
[-0.1424, 1.3892, 0.7559]]])
tensor([[[ 0.2293, 0.3352, 0.0832]],
[[-1.3666, -2.0657, -0.8111]]])
tensor([[[ 0.2293, 0.3352, 0.0832]],
[[-1.3666, -2.0657, -0.8111]]])
torch.Size([2, 1, 3])
"""
# 维度为(b,l,m)和(b,m,n);(l,m)和(b,m,n);(b,c,l,m)和(b,c,m,n);(l,m)和(m)等
a = torch.randn(2, 3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
a = torch.randn(3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
a = torch.randn(2, 3, 3, 4)
b = torch.randn(2, 3, 4, 5)
print(torch.matmul(a, b).size())
a = torch.randn(2, 3)
b = torch.randn(3)
print(torch.matmul(a, b).size())
"""
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 3, 5])
torch.Size([2])
"""
# @运算符:其作用类似于torch.matmul
a = torch.randn(2, 3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
print((a @ b).size())
a = torch.randn(3, 4)
b = torch.randn(2, 4, 5)
print(torch.matmul(a, b).size())
print((a @ b).size())
a = torch.randn(2, 3, 3, 4)
b = torch.randn(2, 3, 4, 5)
print(torch.matmul(a, b).size())
print((a @ b).size())
a = torch.randn(2, 3)
b = torch.randn(3)
print(torch.matmul(a, b).size())
print((a @ b).size())
"""
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 5])
torch.Size([2, 3, 3, 5])
torch.Size([2, 3, 3, 5])
torch.Size([2])
torch.Size([2])
"""
# *运算符:其作用类似于torch.mul
a = torch.ones(2, 3) * 2
print(a)
b = torch.ones(2, 3) * 3
print(b)
output = torch.mul(a, b)
print(output)
print(output.size())
output1 = a * b
print(output1)
print(output1.size())
"""
tensor([[2., 2., 2.],
[2., 2., 2.]])
tensor([[3., 3., 3.],
[3., 3., 3.]])
tensor([[6., 6., 6.],
[6., 6., 6.]])
torch.Size([2, 3])
tensor([[6., 6., 6.],
[6., 6., 6.]])
torch.Size([2, 3])
"""