giagrad.Tensor.var#

Tensor.var(axis=None, ddof=1, keepdims=False)[source]#

Calculates the variance over the axis specified by axis.

The variance (\(\sigma^2\)) is calculated as:

\[\sigma^2 = \frac{1}{N-\text{ddof}}\sum_{i=0}^{N-1}(x_i-\bar{x})^2\]

If keepdims is True, the output tensor is of the same size as the input except in the axis where it is of size 1. Otherwise, every axis is squeezed, leading to an output tensor with fewer dimensions. If no axis is supplied all data is reduced to a scalar value.

Parameters:
  • axis ((int, ...) or int or None, default: None) – The dimension or dimension to reduce. If None, var reduces all dimensions.

  • ddof (int, default: 1) – Degrees of freedom substracted to N. ddof=1 equals sample variance, ddof=0 equals population variance.

  • keepdims (bool, default: False) – Whether te output tensor should retain the reduced dimensions.

Examples

>>> a = Tensor.empty(2, 2, 4, dtype=int).uniform(-10, 10)
>>> a
tensor: [[[ 3  6  2 -5]
          [ 7  3 -4 -8]]
...
         [[ 1  2 -6  6]
          [ 4  9  0 -5]]]
>>> a.var()
tensor: 24.808594 fn: Div
>>> a.var((1, 2), ddof=1)
tensor: [30.         26.26785714] fn: Div
>>> a.std((1, 2), ddof=1)**2
tensor: [30.         26.26785714] fn: Pow