torch
has no equivalent implementation of np.random.choice()
, see the discussion here. The alternative is indexing with a shuffled index or random integers.
To do it with replacement:
- Generate n random indices
- Index your original tensor with these indices
pictures[torch.randint(len(pictures), (10,))]
To do it without replacement:
- Shuffle the index
- Take the n first elements
indices = torch.randperm(len(pictures))[:10]
pictures[indices]
Read more about torch.randint
and torch.randperm
. Second code snippet is inspired by this post in PyTorch Forums.