How to compute the histogram of a tensor in PyTorch?
Published January 03, 2022Torch.histc is used to calculate a tensor's histogram (). It returns a tensor representation of a histogram. It requires four input parameters: max, min, bins, and input. It divides the components into bins of identical width between min and max. It disregards items less than the minimum and more than the maximum.
Computing the histogram of a tensor in PyTorch
It is much simpler to compute a tensor's histogram. Take a look at the simple steps below:
Step 1: Import the necessary library. Torch and Matplotlib are required Python libraries in all of the following Python examples. Ascertain that you have already installed them.
Step 2: Create and print a tensor. Calculate histc (input, bins = 100, min = 0 and max = 100). It gives back a tensor of histogram values—set bins, min, and max to appropriate values based on your requirements.
Step 3: Print the calculated histogram above.
Step 4: Consider the histogram to be a bar graph.
Example
import torch import matplotlib.pyplot as plt T = torch.Tensor([2,3,1,2,3,4,3,2,3,4,3,4]) print("Initial Tensor T:\n",T) hist = torch.histc(T, bins = 5, min = 0, max = 4) print("Tensor Histogram:\n", hist)
|
Output:
Initial Tensor T: tensor([2., 3., 1., 2., 3., 4., 3., 2., 3., 4., 3., 4.]) Tensor Histogram: tensor([0., 1., 3., 5., 3.])
|
Article Contributed By :
|
|
|
|
608 Views |