Rate this Page

torch.nanmedian#

torch.nanmedian(input)Tensor#

Returns the median of the values ininput, ignoringNaN values.

This function is identical totorch.median() when there are noNaN values ininput.Wheninput has one or moreNaN values,torch.median() will always returnNaN,while this function will return the median of the non-NaN elements ininput.If all the elements ininput areNaN it will also returnNaN.

Parameters

input (Tensor) – the input tensor.

Example:

>>>a=torch.tensor([1,float('nan'),3,2])>>>a.median()tensor(nan)>>>a.nanmedian()tensor(2.)
torch.nanmedian(input,dim=-1,keepdim=False,*,out=None)

Returns a namedtuple(values,indices) wherevalues contains the median of each row ofinputin the dimensiondim, ignoringNaN values, andindices contains the index of the median valuesfound in the dimensiondim.

This function is identical totorch.median() when there are noNaN values in a reduced row. When a reduced row hasone or moreNaN values,torch.median() will always reduce it toNaN, while this function will reduce it to themedian of the non-NaN elements. If all the elements in a reduced row areNaN then it will be reduced toNaN, too.

Parameters
  • input (Tensor) – the input tensor.

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

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

Keyword Arguments

out ((Tensor,Tensor),optional) – The first tensor will be populated with the median values and the secondtensor, which must have dtype long, with their indices in the dimensiondim ofinput.

Example:

>>>a=torch.tensor([[2,3,1],[float('nan'),1,float('nan')]])>>>atensor([[2., 3., 1.],        [nan, 1., nan]])>>>a.median(0)torch.return_types.median(values=tensor([nan, 1., nan]), indices=tensor([1, 1, 1]))>>>a.nanmedian(0)torch.return_types.nanmedian(values=tensor([2., 1., 1.]), indices=tensor([0, 1, 0]))