i have a tensor x = [2, 3, 2, 1, 2, 3, 2]
I want to randomly sample a number from all same number, e.g. randomly sample the 2 from all 2’s index, and get index may be 0, namely sampled the first 2; randomly sample the 3, and get index may be 5. and randomly sample the 1, get index 3 since only single 1 exists. so the return index may like [0, 5, 3] or mask: [1, 0, 0, 1, 0, 1, 0].
I dnt know how to implement this logic by pytorch? please kindly help me.
@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.