5

In Pytorch, is there any way of loading a specific single sample using the torch.utils.data.DataLoader class? I'd like to do some testing with it.

The tutorial uses

trainloader = torch.utils.data.DataLoader(...)
images, labels = next(iter(trainloader))

to fetch a random batch of samples. Is there are way, using DataLoader, to get a specific sample?

Cheers

1

2 Answers 2

7
  • Turn off the shuffle in DataLoader
  • Use batch_size to calculate the batch in which the desired sample you are looking for falls in
  • Iterate to the desired batch

Code

import torch 
import numpy as np
import itertools

X= np.arange(100)
batch_size = 2

dataloader = torch.utils.data.DataLoader(X, batch_size=batch_size, shuffle=False)
sample_at = 5
k = int(np.floor(sample_at/batch_size))

my_sample = next(itertools.islice(dataloader, k, None))
print (my_sample)

Output:

tensor([4, 5])
2
  • Thanks for your answer @mujjiga, works like a charm!
    – MJimitater
    Jul 6, 2020 at 16:09
  • Great answer, exactly what was required. Apr 5, 2022 at 7:14
3

if you want to get a specific signle sample from your dataset you can
you should check Subset class.(https://pytorch.org/docs/stable/data.html#torch.utils.data.Subset) something like this:

indices =  [0,1,2]  # select your indices here as a list  
subset = torch.utils.data.Subset(train_set, indices)
trainloader = DataLoader(subset , batch_size =  16  , shuffle =False) #set shuffle to False 

for image , label in trainloader:
   print(image.size() , '\t' , label.size())
   print(image[0], '\t' , label[0]) # index the specific sample 

here is a useful link if you want to learn more about the Pytorch data loading utility (https://pytorch.org/docs/stable/data.html)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.