How to perform element-wise addition on tensors in PyTorch?

We can use torch.add() to perform element-wise addition on tensors in PyTorch. It adds the corresponding elements of the tensors. We can add a scalar or tensor to another tensor. We can add tensors with same or different dimensions. The dimension of the final tensor will be same as the dimension of the higher dimension tensor.

Steps

  • Import the required library. In all the following Python examples, the required Python library is torch. Make sure you have already installed it.

  • Define two or more PyTorch tensors and print them. If you want to add a scalar quantity, define it.

  • Add two or more tensors using torch.add() and assign the value to a new variable. You can also add a scalar quantity to the tensor. Adding the tensors using this method does not make any change in the original tensors.

  • Print the final tensor.

Adding a Scalar to a Tensor

The following Python program shows how to add a scalar quantity to a tensor −

# Python program to perform element-wise Addition
import torch

# Create a tensor
t = torch.Tensor([1, 2, 3, 2])
print("Original Tensor t:\n", t)

# Add a scalar value to a tensor
v = torch.add(t, 10)
print("Element-wise addition result:\n", v)

# Same operation can also be done as below
t1 = torch.Tensor([10])
w = torch.add(t, t1)
print("Element-wise addition result:\n", w)

# Other way to perform the above operation
t2 = torch.Tensor([10, 10, 10, 10])
x = torch.add(t, t2)
print("Element-wise addition result:\n", x)
Original Tensor t:
tensor([1., 2., 3., 2.])
Element-wise addition result:
tensor([11., 12., 13., 12.])
Element-wise addition result:
tensor([11., 12., 13., 12.])
Element-wise addition result:
tensor([11., 12., 13., 12.])

Adding 1D and 2D Tensors

The following Python program shows how to add 1D and 2D tensors using broadcasting −

# Import the library
import torch

# Create a 2-D tensor
T1 = torch.Tensor([[1, 2], [4, 5]])

# Create a 1-D tensor
T2 = torch.Tensor([10])  # also t2 = torch.Tensor([10,10])
print("T1:\n", T1)
print("T2:\n", T2)

# Add 1-D tensor to 2-D tensor
v = torch.add(T1, T2)
print("Element-wise addition result:\n", v)
T1:
tensor([[1., 2.],
        [4., 5.]])
T2:
tensor([10.])
Element-wise addition result:
tensor([[11., 12.],
        [14., 15.]])

Adding Two 2D Tensors

The following program shows how to add two 2D tensors of the same shape −

# Import the library
import torch

# create two 2-D tensors
T1 = torch.Tensor([[1, 2], [3, 4]])
T2 = torch.Tensor([[0, 3], [4, 1]])
print("T1:\n", T1)
print("T2:\n", T2)

# Add the above two 2-D tensors
v = torch.add(T1, T2)
print("Element-wise addition result:\n", v)
T1:
tensor([[1., 2.],
        [3., 4.]])
T2:
tensor([[0., 3.],
        [4., 1.]])
Element-wise addition result:
tensor([[1., 5.],
        [7., 5.]])

Alternative Syntax

PyTorch also provides the + operator for element-wise addition −

import torch

# Create two tensors
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])

# Using + operator
result1 = a + b
print("Using + operator:", result1)

# Using torch.add()
result2 = torch.add(a, b)
print("Using torch.add():", result2)
Using + operator: tensor([5, 7, 9])
Using torch.add(): tensor([5, 7, 9])

Conclusion

Use torch.add() or the + operator for element-wise tensor addition in PyTorch. Both methods support broadcasting, allowing addition of tensors with different dimensions. The operation creates a new tensor without modifying the original tensors.

Updated on: 2026-03-26T18:40:31+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements