How to compute element-wise entropy of an input tensor in PyTorch
Last Updated :
21 Apr, 2022
In this article, we are going to discuss how to compute the element-wise entropy of an input tensor in PyTorch, we can compute this by using torch.special.entr() method.
torch.special.entr() method
torch.special.entr() method computes the element-wise entropy, This method accepts a tensor as input and returns a tensor with the element-wise entropy of the input tensor. if the element is zero, or negative then entropy is also zero, or negative infinity respectively. before moving further let’s see the syntax of the given method.
Syntax: torch.special.entr(tens)
Parameters:
- tens: This is our input tensor.
Returns: Returns the elements-wise entropy of an input tensor.
Example 1:
The following program is to understand how to compute the element-wise entropy of a 1D tensor.
Python
import torch
tens = torch.tensor([ 4 , 5 , 0 , - 5 , - 4 ])
print ( "\n\nInput Tensor: " , tens)
entr = torch.special.entr(tens)
print ( "\n\nComputed Entropy: " , entr)
|
Output:
Example 2:
The following program is to know how to compute the element-wise entropy of a 2D tensor.
Python
import torch
tens = torch.tensor([[ 1 , 2 , - 3 ],
[ 0 , - 3 , 2 ],
[ - 2 , 0 , - 3 ]])
print ( "\n Input Tensor: \n" , tens)
entr = torch.special.entr(tens)
print ( "\n Computed Entropy: \n" , entr)
|
Output:
Share your thoughts in the comments
Please Login to comment...