torch.var_mean(): Computing the Variance and Mean of Tensors – Pytorch Tutorial

By | October 13, 2022

In pytorch, in order to compute the variance and mean of a tensor, we can use torch.var_mean() function. In this tutorial, we will use some examples to show you how to do.

Syntax

torch.var_mean(input, dim, unbiased, keepdim=False, *, out=None)

It will return a tuple (variance, mean)

Here we should notice unbiased parameter.

  • If unbiased = True, Bessel’s correction will be used to calculate the variance.
  • If unbiased = False, Bessel’s correction will be not used.

When we are computing the variance and mean of a batch, we should set unbiased = False, we can find this in batch norm.

how to compute variance in batch norm

We also should notice, this function can compute variance,not standard deviation of this tensor.

How to use torch.var_mean()?

Here is an example:

import torch

x_len = torch.randn([30,20])

var, mean = torch.var_mean(x_len, unbiased=False)
print(var, mean)

Run this code, we will see:

tensor(0.9043) tensor(0.0207)

If dim = -1, we can get:

import torch

x_len = torch.randn([30,20])

var, mean = torch.var_mean(x_len, dim= -1, unbiased=False)
print(var, mean)

The result is:

tensor([0.7645, 1.1956, 0.8395, 0.6493, 1.7741, 1.1854, 1.0927, 0.9121, 0.8873,
        0.6731, 0.7896, 1.2818, 0.8340, 0.4995, 0.5665, 1.5005, 0.9981, 1.2174,
        0.9213, 0.8254, 0.7897, 1.2548, 0.6810, 1.5065, 0.7350, 0.8097, 0.9055,
        1.1010, 0.9379, 0.7091]) tensor([-0.1640, -0.3001,  0.1981, -0.2262,  0.0627,  0.1597, -0.1240, -0.0013,
        -0.0126,  0.2951, -0.1276,  0.2819,  0.5180, -0.1067,  0.1062, -0.0230,
        -0.0640, -0.0270, -0.0241, -0.4303, -0.1213, -0.0258,  0.1430,  0.1553,
        -0.2193, -0.1737,  0.2104, -0.1345,  0.0473,  0.8273])

Moreover, if we want to compute standard deviation, we can do as follows:

std = torch.sqrt(var+1e-8)

Leave a Reply