Drawing a single random batch from a dataloader

What is the recommended way to draw a single random batch with replacement from a DataLoader nowadays?

A very similar question has been asked here, but I don’t understand how to actually extract the data points. I would expect something like:

x_batch, y_batch = train_loader.sample(batch_size=64, replacement=True)
1 Like

I normally use this:

train_loader = DataLoader(train_dataset, batch_size=64, ...)
iterator = iter(train_loader)
x_batch, y_batch = iterator.next()
2 Likes

How do I properly incorporate random sampling with replacement with this method?

I tried the following, but it does not work:

rand_sampler = torch.utils.data.RandomSampler(train_set, num_samples=64, replacement=True)
train_sampler = torch.utils.data.DataLoader(train, batch_size=64, sampler=rand_sampler)

When I try to do what you suggested, I get the following error:

> iterator = iter(train_sampler)
> iterator.next()

*** TypeError: 'function' object is not subscriptable
1 Like

Your code works for me.

You might need to pass train_set instead of train to DataLoader.