Table of Contents
Shortcuts

# torch.var

torch.var(input, dim=None, *, correction=1, keepdim=False, out=None) → Tensor

  Calculates the variance over the dimensions specified by `dim`. `dim` can be a single dimension, list of dimensions, or `None` to reduce over all dimensions.

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

  σ 2=1 N−δ N∑i=0 N−1(x i−x ˉ)2\sigma^2 = \frac{1}{N - \delta N}\sum_{i=0}^{N-1}(x_i-\bar{x})^2 σ 2=N−δ N 1​i=0∑N−1​(x i​−x ˉ)2

  where x x x is the sample set of elements, x ˉ\bar{x}x ˉ is the sample mean, N N N is the number of samples and δ N\delta N δ N is the `correction`.

  If `keepdim` is `True`, the output tensor is of the same size as `input` except in the dimension(s) `dim` where it is of size 1. Otherwise, `dim` is squeezed (see `torch.squeeze()`), resulting in the output tensor having 1 (or `len(dim)`) fewer dimension(s).

  Parameters:

    - input (Tensor) – the input tensor.

    - dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If `None`, all dimensions are reduced.

  Keyword Arguments:

    - correction (int) –

      difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, `correction=1`.

      Changed in version 2.0: Previously this argument was called `unbiased` and was a boolean with `True` corresponding to `correction=1` and `False` being `correction=0`.

    - keepdim (bool) – whether the output tensor has `dim` retained or not.

    - out (Tensor, optional) – the output tensor.

  Example

      >>> a = torch.tensor(
      ...     [[ 0.2035,  1.2959,  1.8101, -0.4644],
      ...      [ 1.5027, -0.3270,  0.5905,  0.6538],
      ...      [-1.5745,  1.3330, -0.5596, -0.6548],
      ...      [ 0.1264, -0.5080,  1.6420,  0.1992]])
      >>> torch.var(a, dim=1, keepdim=True)
      tensor([[1.0631],
              [0.5590],
              [1.4893],
              [0.8258]])

## Docs

Access comprehensive developer documentation for PyTorch

View Docs

## Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

## Resources

Find development resources and get your questions answered

View Resources

To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.