Equi probable selection from Tensor List

Is there any pytorch function that can help select every time equal no of times from Tensor list
say tensor([0, 1])
If i make sample(tensor[0,1],1) 20 times i should nearly 50 pct of times 0 ,50 pct of time 1

You could use torch.multinomial to sample using weights:

x = torch.tensor([0, 1])
weights = torch.tensor([0.5, 0.5])
idx = torch.multinomial(weights, num_samples=100, replacement=True)
out = x[idx]

Note that the indexing is not really necessary here, since the indices are already the target output.

1 Like