Unique values in PyTorch tensor

Viewed 19941

I'm tying to find distinct values in a PyTorch tensor. Is there an efficient analogue of Tensorflow's unique op?

3 Answers
  1. get common items between two tensors with torch.eq()
  2. fetch indices and concatenate tensors
  3. finally get common items via torch.unique:
import torch as pt

a = pt.tensor([1,2,3,2,3,4,3,4,5,6])
b = pt.tensor([7,2,3,2,7,4,9,4,9,8])

equal_data = pt.eq(a, b)
pt.unique(pt.cat([a[equal_data],b[equal_data]]))
Related