Shuffle images ina tensor during training

How do you randomly select a batchSize of fixed size during training?

Say I have my training input as a tensor of size [124, 3, 32, 32], during training, I want to randomly select a batch of 31 from the tensor e.g.

	for epoch in range(maxIter):
		images = Variable(train_X)  #e.g. train_X is a 124 tensor of images of size [3, 32, 32]
		labels = Variable(train_Y)    #e.g. train_Y is of size [124] of labels

		outputs = convnet(images)  #convnet is a cnn

Is there a quick and easy function that allows batchSelection from the train_X and train_Y tensors images?

1 Like

I think I found it out. One can index the tensor the way a python list could be index. For example,

The first tensor would be

 tensor_1 = train_X[0, :, :, :]

and so on ...

Yes, you can index a tensor like a Python list or NumPy array. If you want to sample without replacement you can use the TensorDataset and DataLoader classes:

import torch

inputs = torch.randn(124, 3, 32, 32)
targets = torch.LongTensor(124).random_(1, 10)

dataset =, targets)
loader =, batch_size=31, shuffle=True)
for images, labels in loader:
  print(images.size(), labels.size())
1 Like

Thanks a lot for the info. I did not realize you could use the class to load data that is not native to pytorch.