PyTorch torch.add Function
Pytorch torch Reference Manual
torch.addis a function in PyTorch used to perform element-wise addition. It adds two tensors or a tensor and a scalar.
This is one of the most basic mathematical operations and is used in various computation scenarios in deep learning.
Function Definition
torch.add(input, other, alpha=1, out=None)
Parameters:
input(Tensor): The first input tensor.other(Tensor or float): The second input tensor or scalar.alpha(float, optional): scaling factor,otherwhich is multiplied by this value and then added toinputit, default is 1.out(Tensor, optional): output tensor.
Return Value:
torch.Tensor: Returns the tensor after addition.
Usage Examples
Example 1: Adding Two Tensors
Example
import torch
# Create two tensors
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
# Add them
c = torch.add(a, b)
print(c)
# Create two tensors
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
# Add them
c = torch.add(a, b)
print(c)
The output is:
tensor([5, 7, 9])
Example 2: Tensor Plus Scalar
Example
import torch
# Create a tensor
a = torch.tensor([1, 2, 3])
# Add the scalar
b = torch.add(a, 10)
print(b)
# Create a tensor
a = torch.tensor([1, 2, 3])
# Add the scalar
b = torch.add(a, 10)
print(b)
The output is:
tensor([11, 12, 13])
Example 3: Using the alpha Parameter
Example
import torch
# Create two tensors
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([1.0, 2.0, 3.0])
# Compute a + 2 * b
c = torch.add(a, b, alpha=2)
print(c)
# Create two tensors
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([1.0, 2.0, 3.0])
# Compute a + 2 * b
c = torch.add(a, b, alpha=2)
print(c)
The output is:
tensor([3., 6., 9.])
alphaThe parameter is useful when implementing certain algorithms, such as computinginput + alpha * other。
Example 4: Broadcasting Mechanism
Example
import torch
# Add tensors of different shapes (broadcasting)
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([1, 2, 3])
c = torch.add(a, b)
print(c)
# Add tensors of different shapes (broadcasting)
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([1, 2, 3])
c = torch.add(a, b)
print(c)
The output is:
tensor([[2, 4, 6],
[5, 7, 9])
PyTorch supports the broadcasting mechanism, which allows tensors of different shapes to be operated on.
Using the Plus Operator
In addition to using thetorch.add()function, you can also directly use the+operator, and the two have the same effect:
Example
import torch
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
# The two methods are equivalent
c1 = torch.add(a, b)
c2 = a + b
print(c1)
print(c2)
print(torch.equal(c1, c2))
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
# The two methods are equivalent
c1 = torch.add(a, b)
c2 = a + b
print(c1)
print(c2)
print(torch.equal(c1, c2))
Other Extensions