Rate this Page

torch.mean#

torch.mean(input,*,dtype=None)Tensor#

Note

If theinput tensor is empty,torch.mean() returnsnan.This behavior is consistent with NumPy and follows the definitionthat the mean over an empty set is undefined.

Returns the mean value of all elements in theinput tensor. Input must be floating point or complex.

Parameters:

input (Tensor) – the input tensor, either of floating point or complex dtype

Keyword Arguments:

dtype (torch.dtype, optional) – the desired data type of returned tensor.If specified, the input tensor is casted todtype before the operationis performed. This is useful for preventing data type overflows. Default: None.

Example:

>>>a=torch.randn(1,3)>>>atensor([[ 0.2294, -0.5481,  1.3288]])>>>torch.mean(a)tensor(0.3367)
torch.mean(input,dim,keepdim=False,*,dtype=None,out=None)Tensor

Returns the mean value of each row of theinput tensor in the givendimensiondim. Ifdim is a list of dimensions,reduce over all of them.

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.

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

Keyword Arguments:
  • dtype (torch.dtype, optional) – the desired data type of returned tensor.If specified, the input tensor is casted todtype before the operationis performed. This is useful for preventing data type overflows. Default: None.

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

See also

torch.nanmean() computes the mean value ofnon-NaN elements.

Example:

>>>a=torch.randn(4,4)>>>atensor([[-0.3841,  0.6320,  0.4254, -0.7384],        [-0.9644,  1.0131, -0.6549, -1.4279],        [-0.2951, -1.3350, -0.7694,  0.5600],        [ 1.0842, -0.9580,  0.3623,  0.2343]])>>>torch.mean(a,1)tensor([-0.0163, -0.5085, -0.4599,  0.1807])>>>torch.mean(a,1,True)tensor([[-0.0163],        [-0.5085],        [-0.4599],        [ 0.1807]])