qfeval_functions.functions.nanmin

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

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

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

The function handles edge cases including:

  • Pure NaN slices (returns NaN for values)

  • Mixed NaN and positive infinity values

  • All positive infinity values (returns positive infinity)

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

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

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

Returns:

A named tuple containing:

  • values (Tensor): The minimum 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 minimum values in the original tensor.

Return type:

NanminResult

Example

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

See also

  • nanmax(): NaN-aware maximum function.

  • nanargmin(): NaN-aware argument minimum function.

  • torch.min(): Standard minimum function (NaN propagates).