Rate this Page

torch.functional.unique#

torch.functional.unique(input,sorted=True,return_inverse=False,return_counts=False,dim=None)tuple[Tensor,Tensor,Tensor][source]#

Returns the unique elements of the input tensor.

Note

This function is different fromtorch.unique_consecutive() in the sense thatthis function also eliminates non-consecutive duplicate values.

Note

Currently in the CUDA implementation and the CPU implementation,torch.unique always sort the tensor at the beginning regardless of thesort argument.Sorting could be slow, so if your input tensor is already sorted, it is recommended to usetorch.unique_consecutive() which avoids the sorting.

Parameters
  • input (Tensor) – the input tensor

  • sorted (bool) – Whether to sort the unique elements in ascending orderbefore returning as output.

  • 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,optional) – the dimension to operate upon. IfNone, theunique of the flattened input is returned. Otherwise, each of thetensors indexed by the given dimension is treated as one of theelements to apply the unique operation upon. See examples for moredetails. Default:None

Returns

A tensor or a tuple of tensors containing

  • output (Tensor): the output list of unique scalar elements.

  • inverse_indices (Tensor): (optional) ifreturn_inverse is 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) ifreturn_counts is 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

(Tensor,Tensor (optional),Tensor (optional))

Example:

>>>output=torch.unique(torch.tensor([1,3,2,3],dtype=torch.long))>>>outputtensor([1, 2, 3])>>>output,inverse_indices=torch.unique(...torch.tensor([1,3,2,3],dtype=torch.long),sorted=True,return_inverse=True)>>>outputtensor([1, 2, 3])>>>inverse_indicestensor([0, 2, 1, 2])>>>output,inverse_indices=torch.unique(...torch.tensor([[1,3],[2,3]],dtype=torch.long),sorted=True,return_inverse=True)>>>outputtensor([1, 2, 3])>>>inverse_indicestensor([[0, 2],        [1, 2]])>>>a=torch.tensor([...[...[1,1,0,0],...[1,1,0,0],...[0,0,1,1],...],...[...[0,0,1,1],...[0,0,1,1],...[1,1,1,1],...],...[...[1,1,0,0],...[1,1,0,0],...[0,0,1,1],...],...])>>># If we call `torch.unique(a, dim=0)`, each of the tensors `a[idx, :, :]`>>># will be compared. We can see that `a[0, :, :]` and `a[2, :, :]` match>>># each other, so one of them will be removed.>>>(a[0,:,:]==a[2,:,:]).all()tensor(True)>>>a_unique_dim0=torch.unique(a,dim=0)>>>a_unique_dim0tensor([[[0, 0, 1, 1],         [0, 0, 1, 1],         [1, 1, 1, 1]],        [[1, 1, 0, 0],         [1, 1, 0, 0],         [0, 0, 1, 1]]])>>># Notice which sub-tensors from `a` match with the sub-tensors from>>># `a_unique_dim0`:>>>(a_unique_dim0[0,:,:]==a[1,:,:]).all()tensor(True)>>>(a_unique_dim0[1,:,:]==a[0,:,:]).all()tensor(True)>>># For `torch.unique(a, dim=1)`, each of the tensors `a[:, idx, :]` are>>># compared. `a[:, 0, :]` and `a[:, 1, :]` match each other, so one of>>># them will be removed.>>>(a[:,0,:]==a[:,1,:]).all()tensor(True)>>>torch.unique(a,dim=1)tensor([[[0, 0, 1, 1],         [1, 1, 0, 0]],        [[1, 1, 1, 1],         [0, 0, 1, 1]],        [[0, 0, 1, 1],         [1, 1, 0, 0]]])>>># For `torch.unique(a, dim=2)`, the tensors `a[:, :, idx]` are compared.>>># `a[:, :, 0]` and `a[:, :, 1]` match each other. Also, `a[:, :, 2]` and>>># `a[:, :, 3]` match each other as well. So in this case, two of the>>># sub-tensors will be removed.>>>(a[:,:,0]==a[:,:,1]).all()tensor(True)>>>(a[:,:,2]==a[:,:,3]).all()tensor(True)>>>torch.unique(a,dim=2)tensor([[[0, 1],         [0, 1],         [1, 0]],        [[1, 0],         [1, 0],         [1, 1]],        [[0, 1],         [0, 1],         [1, 0]]])