Random Choice with Pytorch?

Question:

I have a tensor of pictures, and would like to randomly select from it. I’m looking for the equivalent of np.random.choice().

import torch

pictures = torch.randint(0, 256, (1000, 28, 28, 3))

Let’s say I want 10 of these pictures.

Asked By: Nicolas Gervais

||

Answers:

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:

  1. Generate n random indices
  2. Index your original tensor with these indices
pictures[torch.randint(len(pictures), (10,))]  

To do it without replacement:

  1. Shuffle the index
  2. 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.

Answered By: Nicolas Gervais

As the other answer mentioned, torch does not have choice. You can use randint or permutation instead:

import torch

n = 4
replace = True # Can change
choices = torch.rand(4, 3)
choices_flat = choices.view(-1)

if replace:
    index = torch.randint(choices_flat.numel(), (n,))
else:
    index = torch.randperm(choices_flat.numel())[:n]

select = choices_flat[index]
Answered By: Qianyi Zhang

For this size of tensor:

N, D = 386363948, 2
k = 190973
values = torch.randn(N, D)

The following code works fairly fast. It takes around 0.2s:

indices = torch.tensor(random.sample(range(N), k))
indices = torch.tensor(indices)
sampled_values = values[indices]

Using torch.randperm, however, would take more than 20s:

sampled_values = values[torch.randperm(N)[:k]]
Answered By: 刘致远

torch.multinomial provides equivalent behaviour to numpy’s random.choice (including sampling with/without replacement):

# Uniform weights for random draw
unif = torch.ones(pictures.shape[0])

idx = unif.multinomial(10, replacement=True)
samples = pictures[idx]
samples.shape
>>> torch.Size([10, 28, 28, 3])
Answered By: iacob

Try this:

input_tensor = torch.randn(5, 8)
print(input_tensor)
indices = torch.LongTensor(np.random.choice(5,2, replace=False)) 
output_tensor = torch.index_select(input_tensor, 0, indices)
print(output_tensor)
Answered By: Saptarshi Roy

One simple approach is using codes to choose an element from a tensor. in your case, you have a tensor of size (1000, 28, 28, 3) and we want to choose 10 pictures out of 1000 ones.

index = torch.randint(0,1000,(10,))
selected_pics = [pictures[i] for i in index]
Answered By: Alireza Gilaki