- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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.

## Example 1

The following Python program shows how to add a scalar quantity to a tensor. We see three different ways to perform the same task.

# Python program to perform element-wise Addition # import the required library import torch # Create a tensor t = torch.Tensor([1,2,3,2]) print("Original Tensor t:

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

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

", 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:

", x)

## Output

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.])

## Example 2

The following Python program shows how to add 1D and 2D tensors.

# 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:

", T1) print("T2:

", T2) # Add 1-D tensor to 2-D tensor v = torch.add(T1, T2) print("Element-wise addition result:

", v)

## Output

T1: tensor([[1., 2.], [4., 5.]]) T2: tensor([10.]) Element-wise addition result: tensor([[11., 12.], [14., 15.]])

## Example 3

The following program shows how to add 2D tensors.

# 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:

", T1) print("T2:

", T2) # Add the above two 2-D tensors v = torch.add(T1,T2) print("Element-wise addition result:

", v)

## Output

T1: tensor([[1., 2.], [3., 4.]]) T2: tensor([[0., 3.], [4., 1.]]) Element-wise addition result: tensor([[1., 5.], [7., 5.]])

- Related Questions & Answers
- How to perform element-wise subtraction on tensors in PyTorch?
- How to perform element-wise multiplication on tensors in PyTorch?
- How to perform element-wise division on tensors in PyTorch?
- PyTorch – How to compute element-wise logical XOR of tensors?
- How to join tensors in PyTorch?
- How to find element-wise remainder in PyTorch?
- How to compare two tensors in PyTorch?
- How to create tensors with gradients in PyTorch?
- How to apply rectified linear unit function element-wise in PyTorch?
- PyTorch – How to compute element-wise entropy of an input tensor?
- How to perform Matrix Addition using C#?
- How to compute the Cosine Similarity between two tensors in PyTorch?
- How to compute the element-wise angle of the given input tensor in PyTorch?
- Instructions to perform addition in 8085 Microprocessor
- How to perform in-place operations in PyTorch?