1. 加法运算
示例代码:
import torch
# 这两个Tensor加减乘除会对b自动进行Broadcasting
a = torch.rand(3, 4)
b = torch.rand(4)
c1 = a + b
c2 = torch.add(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
输出结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
2. 减法运算
示例代码:
a = torch.rand(3, 4)
b = torch.rand(4)
c1 = a - b
c2 = torch.sub(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2)))
输出结果:
torch.Size([3, 4]) torch.Size([3, 4])
tensor(1, dtype=torch.uint8)
3. 哈达玛积(element wise,对应元素相乘)
示例代码:
c1 = a * b
c2 = torch.mul(a, b)
print(c1.shape, c2.shape)
print(torch.all(torch.eq(c1, c2