@ptrblck Thanks.
However, if i want to iterate over all unique number, i have to use a python for loop:
x = torch.tensor([2, 3, 2, 1, 2, 3, 2])
unique = torch.unique(x)
for u in unique: #!!! time consuming!
    to_sample = u
    idx_all = (x == to_sample).nonzero()
    idx = idx_all[torch.randperm(len(idx))[0]]

any concise way to finish it without python for loop?
Thank you.