qfeval_functions.functions.nanmax

nanmax(x, dim, keepdim=False)[source]

Return the maximum values and indices along a dimension, ignoring NaN values.

This function computes the maximum value along the specified dimension, excluding NaN values from consideration. It returns both the maximum values and their corresponding indices in the original tensor. This is similar to torch.max but with NaN-aware behavior.

Parameters:
  • x (Tensor) – The input tensor containing values.

  • dim (int) – The dimension along which to find the maximum values.

  • keepdim (bool) – Whether the output tensors have dim retained or not. Default is False.

Returns:

A named tuple containing:

  • values (Tensor): The maximum values along the specified dimension, with NaN values ignored. If a slice contains only NaN values, the result is NaN.

  • indices (Tensor): The indices of the maximum values in the original tensor.

Return type:

NanmaxResult

Example

>>> # Simple maximum with NaN values
>>> x = torch.tensor([[1.0, nan, 3.0],
...                   [4.0, 5.0, nan]])
>>> result = QF.nanmax(x, dim=1)
>>> result.values
tensor([3., 5.])
>>> result.indices
tensor([2, 1])
>>> # All NaN slice
>>> x = torch.tensor([[1.0, 2.0],
...                   [nan, nan]])
>>> result = QF.nanmax(x, dim=1)
>>> result.values
tensor([2., nan])
>>> result.indices
tensor([1, 0])
>>> # With negative infinity
>>> x = torch.tensor([[1.0, -inf, 3.0],
...                   [nan, 2.0, -inf]])
>>> result = QF.nanmax(x, dim=1)
>>> result.values
tensor([3., 2.])
>>> result.indices
tensor([2, 1])
>>> # With keepdim
>>> x = torch.tensor([[nan, 2.0, 1.0],
...                   [3.0, nan, 4.0]])
>>> result = QF.nanmax(x, dim=1, keepdim=True)
>>> result.values
tensor([[2.],
        [4.]])
>>> result.indices
tensor([[1],
        [2]])

See also

nanmin(): NaN-aware minimum function. nanargmax(): NaN-aware argument maximum function. torch.max: Standard maximum function (NaN propagates).