How to perform element-wise division on tensors in PyTorch?
Published January 02, 2022Using the torch.div() method, you can do an element-wise division on multiple tensors. Every tensor in the first input is divided by the next tensor in the second input. It is possible to split a two-dimensional vector into a two-dimensional scalar. A tensor may be subdivided by another tensor regardless of the dimension. The resulting tensor's dimension will be the same as the higher-dimensional tensor's dimension. In other words, if you divide one tensor by another, the result will be a 2D tensor.
PyTorch element-wise division on Tensors
Follow these easy steps to divide tensors element-by-element in PyTorch:
Step1: Import the torch libraries you need and verify to see whether it's already been installed.
Step 2: Create and output several PyTorch tensors. To divide a tensor by a scalar, you must first define the scalar in question.
Step 4: Using the torch.div(), divide the defined tensors and assign the output to the new variable
Step 5: Print out the final tensor.
Example
import torch a = torch.Tensor([[4,4],[6,7]]) b = torch.Tensor([10, 10]) print("Tensor1:\n", a) print("Tensor2:\n", b) v = torch.div(a, b) print("Result:\n", v)
|
Output:
Tensor1: tensor([[4., 4.], [6., 7.]]) Tensor2: tensor([10., 10.]) Result: tensor([[0.4000, 0.4000], [0.6000, 0.7000]]) |
Article Contributed By :
|
|
|
|
969 Views |