How to compute mean, standard deviation, and variance of a tensor in PyTorch

 PyTorch is a machine learning framework developed  at Facebook's AI Research Lab. It is used in many applications such as computer vision, natural language processing. A PyTorch tensor is basically same as NumPy array. It is a multidimensional matrix that contains elements of a single data type. A PyTorch Tensor may be one, two or multidimensional. The difference between the NumPy array and PyTorch Tensor is that the PyTorch Tensor can run on the CPU or GPU.

In this post we try to understand following:

  • How to compute mean of a PyTorch Tensor
  • How to compute standard deviation of a PyTorch Tensor
  • How to compute variance of a PyTorch Tensor
  • PyTorch
  • Python
  • NumPy

Installing PyTorch

Install PyTorch using pip command as bellow

pip install torch

Define a PyTorch Tensor

A PyTorch Tensor can be defined using Python List or numpy array using torch.tensor() constructor.

torch.tensor([[2.,1.], [3.,4.]])

Lets have a look on the complete Python program to define the PyTorch Tensor.

import torch
import numpy as np
#define a PyTorch Tensor usning Python List
a = torch.tensor([[2.,1.], [3.,4.]])
# define a PyTorch Tensor using numpy array
b = torch.tensor(np.array([[1,2,3],[9,0,2]]))
tensor([[2., 1.],
        [3., 4.]])
tensor([[1, 2, 3],
        [9, 0, 2]])

Compute mean, standard deviation, and variance of a PyTorch Tensor

We can compute the mean, standard deviation, and the variance of a Tensor using following


Lets have a look on the complete example.

import torch
import numpy as np
#define a PyTorch Tensor usning Python List
a = torch.tensor([[2.,1.], [3.,4.]])
# compute mean, std, and var
m = torch.mean(a)
s = torch.std(a)
v = torch.var(a)
# print mean, std, and var
print("Mean:{}\n std: {}\n Var: {}".format(m,s,v))


 std: 1.29099440574646
 Var: 1.6666666269302368

We can also compute mean, std, and var row and column wise.

import torch
import numpy as np
#define a PyTorch Tensor usning Python List
a = torch.tensor([[2.,1.], [3.,4.]])
# compute mean, std, and var column-wise
m = torch.mean(a, axis = 0)
s = torch.std(a, axis = 0)
v = torch.var(a, axis = 0)
# print mean, std, and var
print("Column wise\nMean:{}\n std: {}\n Var: {}".format(m,s,v))
# compute mean, std, and var row-wise
m = torch.mean(a, axis = 1)
s = torch.std(a, axis = 1)
v = torch.var(a, axis = 1)
# print mean, std, and var
print("Row wise\nMean:{}\n std: {}\n Var: {}".format(m,s,v))


Column wise
Mean:tensor([2.5000, 2.5000])
 std: tensor([0.7071, 2.1213])
 Var: tensor([0.5000, 4.5000])
Row wise
Mean:tensor([1.5000, 3.5000])
 std: tensor([0.7071, 0.7071])
 Var: tensor([0.5000, 0.5000])

Further Reading:

Useful Resources: