1. torch.dot()函数,专门计算两个一维张量的点积;不支持广播规则
import torch
x1 = torch.tensor([1, 2, 3]) # shape: (3, )
y1 = torch.tensor([2, 3, 4]) # shape: (3, )
z1 = x1.dot(y1) # 也可写成torch.dot(x1, y1)
print(z1) # tensor(20)
2. torch.mm()函数,专门计算两个二维张量矩阵的乘法;不支持广播规则
# 将x1, y1扩展一个维度使其为二维张量矩阵
x2 = x1.unsqueeze(dim=0) # shape: (1, 3)
y2 = y1.unsqueeze(dim=0) # shape: (1, 3)
# 对y2进行转置,使得符合矩阵乘法规则
y2 = y2.T # shape: (3, 1)
z2 = torch.mm(x2, y2) # 根据矩阵乘法规则,得到1*1的二维张量矩阵
print(z2) # tensor([[20]])
3. torch.matmul()函数,可用于任意维度的张量乘法;当处理一维张量,类似于torch.dot(),当处理二维张量,类似于torch.mm();
# 创建两个随机张量
x3 = torch.randint(low=1, high=10, size=(2, 3, 2))
print(x3)
"""
tensor([[[9, 6],
[6, 5],
[8, 1]],
[[1, 1],
[1, 4],
[1, 8]]])
"""
y3 = torch.randint(low=1, high=10, size=(2, 3))
print(y3)
"""
tensor([[1, 4, 6],
[3, 3, 6]])
"""
z3 = torch.matmul(x3, y3)
print(z3)
"""
tensor([[[27, 54, 90],
[21, 39, 66],
[11, 35, 54]],
[[ 4, 7, 12],
[13, 16, 30],
[25, 28, 54]]])
"""
print(z3.shape) # torch.Size([2, 3, 3])
4. torch.add()函数,用于张量之间的逐元素相加,支持广播规则;
x4 = torch.tensor([1, 2, 3]) # shape: (3, )
y4 = torch.tensor([[1, 1, 1], [2, 2, 2]]) # shape: (2, 3)
z4 = x4.add(y4)
print(z4)
# tensor([[2, 3, 4],
# [3, 4, 5]])
print(x4) # tensor([1, 2, 3])
5. torch.add_()函数,是torch.add()函数的in_place版本;in_place操作在函数作用后即将结果重新赋值给变量。比如x.add_(y),x+y的结果会存储到原来的x中。pytorch里面所有带"_"的操作,都是in-place的。
x4 = torch.tensor([1, 2, 3]) # shape: (3, )
y4 = torch.tensor([[1, 1, 1], [2, 2, 2]]) # shape: (2, 3)
z5 = y4.add_(x4) # 注意这里是y4作为主元,因为我们要保证in_place之后的张量shape满足广播规则
print(y4)
# tensor([[2, 3, 4],
# [3, 4, 5]])
print(y4 == z5)
# tensor([[True, True, True],
# [True, True, True]])
6. torch.mul()函数,用于张量之间的逐元素相乘,支持广播规则;
x4 = torch.tensor([1, 2, 3]) # shape: (3, )
y4 = torch.tensor([[1, 1, 1], [2, 2, 2]]) # shape: (2, 3)
z6 = torch.mul(x4, y4)
print(z6)
# tensor([[1, 2, 3],
# [2, 4, 6]])
print(x4)
# tensor([1, 2, 3])
print(y4)
# tensor([[1, 1, 1],
# [2, 2, 2]])
7. torch.mul_()函数,是torch.mul()函数的in_place版本;
x4 = torch.tensor([1, 2, 3]) # shape: (3, )
y4 = torch.tensor([[1, 1, 1], [2, 2, 2]]) # shape: (2, 3)
z7 = y4.mul_(x4)
print(y4)
# tensor([[1, 2, 3],
# [2, 4, 6]])
print(y4 == z7)
# tensor([[True, True, True],
# [True, True, True]])
8. torch.zero_()函数,将张量元素清0,shape不变,也是in_place操作;
y4 = torch.tensor([[1, 1, 1], [2, 2, 2]]) # shape: (2, 3)
z8 = y4.zero_()
print(y4)
# tensor([[0, 0, 0],
# [0, 0, 0]])
print(y4 == z8)
# tensor([[True, True, True],
# [True, True, True]])
参考博客:PyTorch: .add()和.add_(),.mul()和.mul_(),.exp()和.exp_()_exp() in-place操作-CSDN博客