Rate this Page

torch.nanmean#

torch.nanmean(input,dim=None,keepdim=False,*,dtype=None,out=None)Tensor#

Computes the mean of allnon-NaN elements along the specified dimensions.Input must be floating point or complex.

This function is identical totorch.mean() when there are noNaN valuesin theinput tensor. In the presence ofNaN,torch.mean() willpropagate theNaN to the output whereastorch.nanmean() will ignore theNaN values (torch.nanmean(a) is equivalent totorch.mean(a[~a.isnan()])).

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, either of floating point or complex dtype

  • 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.mean() computes the mean value, propagatingNaN.

Example:

>>>x=torch.tensor([[torch.nan,1,2],[1,2,3]])>>>x.mean()tensor(nan)>>>x.nanmean()tensor(1.8000)>>>x.mean(dim=0)tensor([   nan, 1.5000, 2.5000])>>>x.nanmean(dim=0)tensor([1.0000, 1.5000, 2.5000])# If all elements in the reduced dimensions are NaN then the result is NaN>>>torch.tensor([torch.nan]).nanmean()tensor(nan)