qfeval_functions.functions.cumcount

cumcount(x, dim=-1)[source]

Number each occurrence of unique values along a dimension.

This function assigns a cumulative count to each occurrence of unique values along the specified dimension. For each unique value, the first occurrence is numbered 0, the second occurrence is numbered 1, and so on. This is similar to the behavior of pandas.GroupBy.cumcount().

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

  • dim (int) – The dimension along which to perform cumulative counting. Default is -1 (the last dimension).

Returns:

A tensor of the same shape as the input, where each element contains the cumulative count (0-indexed) of that value’s occurrence along the specified dimension.

Return type:

Tensor

Example

>>> x = torch.tensor([1, 2, 1, 3, 2, 1, 3])
>>> QF.cumcount(x)
tensor([0, 0, 1, 0, 1, 2, 1])
>>> x = torch.tensor([[1, 2, 1, 2],
...                   [3, 3, 4, 3]])
>>> QF.cumcount(x, dim=1)
tensor([[0, 0, 1, 1],
        [0, 1, 0, 2]])
>>> x = torch.tensor([[1, 2, 3],
...                   [1, 2, 3],
...                   [1, 2, 3]])
>>> QF.cumcount(x, dim=0)
tensor([[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]])