torch.unique_consecutive#
- torch.unique_consecutive(*args,**kwargs)[source]#
Eliminates all but the first element from every consecutive group of equivalent elements.
Note
This function is different from
torch.unique()in the sense that this functiononly eliminates consecutive duplicate values. This semantics is similar tostd::uniquein C++.- Parameters
input (Tensor) – the input tensor
return_inverse (bool) – Whether to also return the indices for whereelements in the original input ended up in the returned unique list.
return_counts (bool) – Whether to also return the counts for each uniqueelement.
dim (int) – the dimension to apply unique. If
None, the unique of theflattened input is returned. default:None
- Returns
A tensor or a tuple of tensors containing
output (Tensor): the output list of unique scalar elements.
inverse_indices (Tensor): (optional) if
return_inverseis True, there will be an additionalreturned tensor (same shape as input) representing the indicesfor where elements in the original input map to in the output;otherwise, this function will only return a single tensor.counts (Tensor): (optional) if
return_countsis True, there will be an additionalreturned tensor (same shape as output or output.size(dim),if dim was specified) representing the number of occurrencesfor each unique value or tensor.
- Return type
Example:
>>>x=torch.tensor([1,1,2,2,3,1,1,2])>>>output=torch.unique_consecutive(x)>>>outputtensor([1, 2, 3, 1, 2])>>>output,inverse_indices=torch.unique_consecutive(x,return_inverse=True)>>>outputtensor([1, 2, 3, 1, 2])>>>inverse_indicestensor([0, 0, 1, 1, 2, 3, 3, 4])>>>output,counts=torch.unique_consecutive(x,return_counts=True)>>>outputtensor([1, 2, 3, 1, 2])>>>countstensor([2, 2, 1, 2, 1])