How to compute the mean and standard deviation of a tensor in PyTorch?
Published January 02, 2022PyTorch may be used to do tensor computations such as calculating the mean and standard deviation. We utilize the torch.mean () method to determine the mean. The average score of all the components in the input tensor is returned by this method. On the other hand, to calculate the standard deviation of the tensor constituents, we utilize the torch.std () function. Both the standard deviation and mean are calculated either in a row or in a column.
How to Compute the Mean and Standard Deviation of Tensor Elements in PyTorch
It is quite simple to determine the Mean and standard deviation of Tensor Elements. Simply take these few steps:
Step 1: Import the torch library. This is a necessity.
Step 2: Create and output a PyTorch tensor.
Step 3: Using torch.mean, compute the mean (input, axis).
Step 4: Using torch.std, compute the standard deviation (input, axis). Make a new variable out of the calculated standard deviation.
Step 5: Print the mean and standard deviation calculated above.
Example
The Python program below demonstrates how to calculate the mean and standard deviation of tensor elements
import torch a = torch.Tensor([4.432, 2.453, -6.554, 0.754]) print("a:", a) mean = torch.mean(a) std = torch.std(a) print("Standard deviation:", std) print("Mean:", mean)
|
Output:
a: tensor([4.432, 2.453, -6.554, 0.754]) Standard deviation: tensor(4.7920) Mean: tensor(0.2713)
|
Article Contributed By :
|
|
|
|
614 Views |