Introduction
In this tutorial, we will learn how to use torch.add() function to add tensors in PyTorch. We will see various examples of this function for easy understanding by beginners. So let us get started.
Adding Tensors in PyTorch with torch.add()
In PyTorch, the tensors can be added easily with the torch add function whose usage is quite intuitive. It also supports broadcasting to a common shape this means the tensors of different sizes can be added together if their shapes are compatible for broadcasting.
Syntax
The syntax of torch.add function is as follows â€“
torch.add(input, other, alpha=1, out=None)
 input:Â This is the input tensor for addition.
 other: This is the other tensor or scalar to be added to the input tensor above.
 alpha: An optional multiplier to other tensor before it is added to the input tensor. By default, it is 1.
 out: This is the output tensor.
Example of torch.add() function
Import Torch Library
Before going through examples of the torch add function let us first import the torch library.
In [0]:
import torch;
Example 1: Adding a Scalar Value to Tensor with torch.add()
In this example, we create a 2D tensor and add a scalar value of 5 to it by using the torch.add function. The scalar value is broadcasted and added to all elements of the tensor as evident in the output.
In [1]:
tensor1 = torch.arange(12).reshape(4,3) scalar = 5 sumÂ =Â torch.add(tensor1,scalar) print("First Tensor:") print(tensor1) print("Scalar:" + str(scalar)) print("SumÂ ofÂ TensorÂ andÂ Scalar:") print(sum)
Out[1]:
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Scalar:5 Sum of Tensor and Scalar: tensor([[ 5, 6, 7], [ 8, 9, 10], [11, 12, 13], [14, 15, 16]])
Example 2: Adding a 1D Tensor to 2D Tensor
In this example, we create a 1D tensor of size 1Ã—3 and add it to a 2D tensor of size 4Ã—3. The 1D tensor is added to each row of the 2D tensor elementwise.
In [2]:
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(3) sum = torch.add(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([0, 1, 2]) Sum of Tensor1 and Tensor2: tensor([[ 0, 2, 4], [ 3, 5, 7], [ 6, 8, 10], [ 9, 11, 13]])
Example 3: Adding a 1D Tensor to 2D Tensor
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(4).reshape(4,1) sum = torch.add(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([[0], [1], [2], [3]]) Sum of Tensor1 and Tensor2: tensor([[ 0, 1, 2], [ 4, 5, 6], [ 8, 9, 10], [12, 13, 14]])
Example 4: Element Wise Addition of two 2D Tensors
In this example, we create two 2D Tensors of the same size 4Ã—3 and then do elementwise addition with the torch add function.
In [4]:
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(start=4, end=16).reshape(4,3) sumÂ =Â torch.add(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([[ 4, 5, 6], [ 7, 8, 9], [10, 11, 12], [13, 14, 15]]) Sum of Tensor1 and Tensor2: tensor([[ 4, 6, 8], [10, 12, 14], [16, 18, 20], [22, 24, 26]])
Example 5: Adding a 1D Tensor to 3D Tensor
In this example, we create a 1D tensor of size 1Ã—3 and add it to a 3D tensor of size 2x4x3. In this case, intuitively we can say that the 1D tensor is added elementwise to each row of the 3D tensor.
In [5]:
tensor1 = torch.arange(24).reshape(2,4,3) tensor2 = torch.arange(3) sumÂ =Â torch.add(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) Second Tensor: tensor([0, 1, 2]) Sum of Tensor1 and Tensor2: tensor([[[ 0, 2, 4], [ 3, 5, 7], [ 6, 8, 10], [ 9, 11, 13]], [[12, 14, 16], [15, 17, 19], [18, 20, 22], [21, 23, 25]]])
Example 6: Adding a 2D Tensor to 3D Tensor
In [6]:
tensor1 = torch.arange(24).reshape(2,4,3) tensor2 = torch.arange(12).reshape(4,3) sumÂ =Â torch.add(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) Second Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Sum of Tensor1 and Tensor2: tensor([[[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16], [18, 20, 22]], [[12, 14, 16], [18, 20, 22], [24, 26, 28], [30, 32, 34]]])
Example 7: Adding two 3D Tensors Element Wise
In this example, we create two 3D Tensors of the same size 2x4x3 and then do elementwise addition with the torch add function.
In [7]:
tensor1 = torch.arange(24).reshape(2,4,3) tensor2 = torch.arange(start=4, end=28).reshape(2,4,3) sumÂ =Â torch.add(tensor1,tensor2) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]], [[12, 13, 14], [15, 16, 17], [18, 19, 20], [21, 22, 23]]]) Second Tensor: tensor([[[ 4, 5, 6], [ 7, 8, 9], [10, 11, 12], [13, 14, 15]], [[16, 17, 18], [19, 20, 21], [22, 23, 24], [25, 26, 27]]]) Sum of Tensor1 and Tensor2: tensor([[[ 4, 6, 8], [10, 12, 14], [16, 18, 20], [22, 24, 26]], [[28, 30, 32], [34, 36, 38], [40, 42, 44], [46, 48, 50]]])
Example 8: Using torch.add() with Alpha Parameter
In this example, we will see how to use the parameter alpha with torch.add() function. The second tensor is multiplied by the value of alpha = 5 and its output is added to the first tensor.
In [8]:
tensor1 = torch.arange(12).reshape(4,3) tensor2 = torch.arange(3) sum = torch.add(tensor1,tensor2, alpha=5) print("First Tensor:") print(tensor1) print("Second Tensor:") print(tensor2) print("Sum of Tensor1 and Tensor2:") print(sum)
First Tensor: tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]]) Second Tensor: tensor([0, 1, 2]) Sum of Tensor1 and Tensor2: tensor([[ 0, 6, 12], [ 3, 9, 15], [ 6, 12, 18], [ 9, 15, 21]])

MLK is a knowledge sharing community platform for machine learning enthusiasts, beginners and experts. Let us create a powerful hub together to Make AI Simple for everyone.
View all posts