Rate this Page

torch.var_mean#

torch.var_mean(input,dim=None,*,correction=1,keepdim=False,out=None)#

Calculates the variance and mean over the dimensions specified bydim.dim can be a single dimension, list of dimensions, orNone toreduce over all dimensions.

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

σ2=1max(0, NδN)i=0N1(xixˉ)2\sigma^2 = \frac{1}{\max(0,~N - \delta N)}\sum_{i=0}^{N-1}(x_i-\bar{x})^2

wherexx is the sample set of elements,xˉ\bar{x} is thesample mean,NN is the number of samples andδN\delta N isthecorrection.

Ifkeepdim isTrue, the output tensor is of the same sizeasinput except in the dimension(s)dim where it is of size 1.Otherwise,dim is squeezed (seetorch.squeeze()), resulting in theoutput tensor having 1 (orlen(dim)) fewer dimension(s).

Parameters
  • input (Tensor) – the input tensor.

  • dim (int ortuple ofints,optional) – the dimension or dimensions to reduce.IfNone, all dimensions are reduced.

Keyword Arguments
  • correction (int) –

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

    Changed in version 2.0:Previously this argument was calledunbiased and was a booleanwithTrue corresponding tocorrection=1 andFalse beingcorrection=0.

  • keepdim (bool,optional) – whether the output tensor hasdim retained or not. Default:False.

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

Returns

A tuple (var, mean) containing the variance and mean.

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]]...)# fmt: skip>>>torch.var_mean(a,dim=0,keepdim=True)(tensor([[1.5926, 1.0056, 1.2005, 0.3646]]), tensor([[ 0.0645,  0.4485,  0.8707, -0.0665]]))