- 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 division on tensors in PyTorch?

To perform element-wise division on two tensors in PyTorch, we can use the **torch.div()** method. It divides each element of the first input tensor by the corresponding element of the second tensor. We can also divide a tensor by a scalar. A tensor can be divided by a tensor with same or different dimension. The dimension of the final tensor will be same as the dimension of the higher-dimensional tensor. If we divide a 1D tensor by a 2D tensor, then the final tensor will a 2D 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 divide a tensor by a scalar, define a scalar.

Divide a tensor by another tensor or scalar using

**torch.div()**and assign the value to a new variable. Dividing the tensors using this method does not make any change in the original tensors.Print the final tensor.

## Example 1

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

", t) # Divide a tensor by a scalar 4 v = torch.div(t, 4) print("Element-wise division result:

", v) # Same result can also be obtained as below t1 = torch.Tensor([4]) w = torch.div(t, t1) print("Element-wise division result:

", w) # other way to do above operation t2 = torch.Tensor([4,4,4,4]) x = torch.div(t, t2) print("Element-wise division result:

", x)

## Output

Original Tensor t: tensor([2., 3., 5., 9.]) Element-wise division result: tensor([0.5000, 0.7500, 1.2500, 2.2500]) Element-wise division result: tensor([0.5000, 0.7500, 1.2500, 2.2500]) Element-wise division result: tensor([0.5000, 0.7500, 1.2500, 2.2500])

## Example 2

The following Python program shows how to divide a 2D tensor by a 1D tensor.

# import the required library import torch # Create a 2D tensor T1 = torch.Tensor([[3,2],[7,5]]) # Create a 1-D tensor T2 = torch.Tensor([10, 8]) print("T1:

", T1) print("T2:

", T2) # Divide 2-D tensor by 1-D tensor v = torch.div(T1, T2) print("Element-wise division result:

", v)

## Output

T1: tensor([[3., 2.], [7., 5.]]) T2: tensor([10., 8.]) Element-wise division result: tensor([[0.3000, 0.2500], [0.7000, 0.6250]])

## Example 3

The following Python program shows how to divide a 1D tensor by a 2D tensor.

# Python program to dive a 1D tensor by a 2D tensor # import the required library import torch # Create a 2D tensor T1 = torch.Tensor([[8,7],[4,5]]) # Create a 1-D tensor T2 = torch.Tensor([10, 5]) print("T1:

", T1) print("T2:

", T2) # Divide 1-D tensor by 2-D tensor v = torch.div(T2, T1) print("Division 1D tensor by 2D tensor result:

", v)

## Output

T1: tensor([[8., 7.], [4., 5.]]) T2: tensor([10., 5.]) Division 1D tensor by 2D tensor result: tensor([[1.2500, 0.7143], [2.5000, 1.0000]])

You can notice the final tensor is a 2D tensor.

## Example 4

The following Python program shows how to divide a 2D tensor by a 2D tensor.

# import necessary library import torch # Create two 2-D tensors T1 = torch.Tensor([[8,7],[3,4]]) T2 = torch.Tensor([[0,3],[4,9]]) # Print the above tensors print("T1:

", T1) print("T2:

", T2) # Divide T1 by T2 v = torch.div(T1,T2) print("Element-wise division result:

", v)

## Output

T1: tensor([[8., 7.], [3., 4.]]) T2: tensor([[0., 3.], [4., 9.]]) Element-wise division result: tensor([[ inf, 2.3333], [0.7500, 0.4444]])

- Related Questions & Answers
- How to perform element-wise addition on tensors in PyTorch?
- How to perform element-wise subtraction on tensors in PyTorch?
- How to perform element-wise multiplication 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?
- Return the element-wise remainder of division in Numpy
- How to apply rectified linear unit function element-wise in PyTorch?
- PyTorch – How to compute element-wise entropy of an input tensor?
- 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?
- Return the element-wise remainder of division with modulo operation in Numpy
- Return the element-wise remainder of division with fmod() operation in Numpy