pytorch

How to Find Mean, Variance, and Standard Deviation of Tensors in PyTorch?

Mean, variance, and standard deviation are useful statistics that help users to analyze the distribution and shape of the data. Mean is the average value of the specific dataset, and the variance of a dataset is a measurement of how much the values differ from the mean. The standard deviation is the variance’s square root. PyTorch provides built-in functions to find/compute the mean, standard deviation, and variance of tensors or datasets.

This blog will exemplify the method of calculating the mean, variance, and standard deviation of tensors in PyTorch.

How to Find/Calculate Mean, Variance, and Standard Deviation of Tensors in PyTorch?

To find the mean, variance, and standard deviation of tensors in PyTorch, follow the provided steps:

  • Import torch library
  • Create a 1D or 2D tensor and print its elements
  • Find/compute the mean, variance, and standard deviation of the tensor
  • Print calculated mean, variance, and standard deviation

Go through the following examples for a practical demonstration.

Example 1: Find/Calculate Mean, Variance, and Standard Deviation of 1D Tensor

In the first example, we will create a simple 1D tensor and compute its mean, standard deviation, and variance. To do so, follow the next provided steps:

Step 1: Import PyTorch Library
First, import the “torch” library:

import torch

Step 2: Create 1D Tensor
Then, utilize the “torch.Tensor()” function to create/define a 1D tensor from the list and print its content. Here, we are creating the following tensor and storing it in a “Tens” variable:

Tens = torch.Tensor([7, 3, 0.75, -3.5])

print("Tensor:", Tens)

This has created a 1D tensor as seen below:

Step 3: Compute Mean, Variance, and Standard Deviation
Next, find the mean, variance, and standard deviation of the above-created tensor using the “torch.mean()”, “torch.var()”, and “torch.std()” methods respectively:

Mean = torch.mean(Tens)

Var = torch.var(Tens)

STD= torch.std(Tens)

Step 4: Print Mean, Variance, and Standard Deviation
Finally, print calculated mean, variance, and standard deviation:

print("Mean:", Mean)

print("variance:", Var)

print("Standard Deviation:", STD)

The below output shows the mean, variance, and standard deviation of the “Tens” tensor:

Example 2: Find/Calculate Mean, Variance, and Standard Deviation of 2D Tensor

In the second section, we will create a simple 2D tensor and compute its mean, standard deviation, and variance. To do so, follow the next provided steps:

Step 1: Import PyTorch Library
First, import the “torch” library:

import torch

Step 2: Create 2D Tensor
Then, create a desired 2D tensor using the “torch.Tensor()” function and print its content. Here, we are creating the following tensor and storing it in a “Tens” variable:

Tens = torch.Tensor([[2,4,7], [5,3,-9]])

print("Tensor:\n", Tens)

This has created a 2D tensor:

Step 3: Compute Mean, Variance, and Standard Deviation
Next, use the “torch.mean()”, “torch.var()”, and “torch.std()” methods to find the mean, variance, and standard deviation of the tensor respectively:

Mean = torch.mean(Tens)

Var = torch.var(Tens)

STD= torch.std(Tens)

Step 4: Print Mean, Variance, and Standard Deviation
Finally, print the tensor’s calculated mean, standard deviation, and variance:

print("Mean:", Mean)

print("variance:", Var)

print("Standard Deviation:", STD)

In the below output, the mean, variance, and standard deviation of the 2D tensor can be seen:

Example 3: Find/Calculate Column-wise and Row-wise Mean, Variance, and Standard Deviation of 2D Tensor

Users can also calculate the column-wise and row-wise mean, variance, and standard deviation of the 2D tensor. To do so, follow the next provided steps:

Step 1: Compute Column-wise Mean, Variance, and Standard Deviation
To find the column-wise mean, variance, and standard deviation of the tensor, use the “torch.mean()”, “torch.var()”, and “torch.std()” methods respectively, and specify the tensor name and “axis = 0”:

Col_mean = torch.mean(Tens, axis = 0)
Col_var =  torch.var(Tens, axis = 0)
Col_std = torch.std(Tens, axis = 0)

print("Column-wise Mean:\n", Col_mean)
print("Column-wise variance:\n", Col_var)
print("Column-wise Standard deviation:\n", Col_std)

Upon doing so, the column-wise mean, variance, and standard deviation of the tensor have been calculated as seen below:

Step 2: Compute Row-wise Mean, Variance, and Standard Deviation
To find the row-wise mean, variance, and standard deviation of the tensor, use the same methods and specify the tensor name and “axis = 1”:

Row_mean = torch.mean(Tens, axis = 1)
Row_var =  torch.var(Tens, axis = 1)
Row_std = torch.std(Tens, axis = 1)

print("Row-wise Mean:\n", Row_mean)
print("Row-wise variance:\n", Row_var)
print("Row-wise Standard deviation:\n", Row_std)

The below output shows the row-wise mean, variance, and standard deviation of the 2D tensor:

We have efficiently explained the method to calculate the mean, variance, and standard deviation of tensors in PyTorch.

Note: You can access our Google Colab Notebook at this link.

Conclusion

To find/calculate the mean, variance, and standard deviation of tensors in PyTorch, first, import the “torch” library. Then, create the desired 1D or 2D tensors, and print their elements. After that, use the “torch.mean()”, “torch.var()”, and “torch.std()” methods to calculate the mean, variance, and standard deviation of the tensors, respectively. Lastly, print the calculated mean, standard deviation, and variance. This blog has exemplified the method to compute the mean, variance, and standard deviation of tensors in PyTorch.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.