How dataloader shuffled with enumerate()?


I am coding for training loop with dataloader which is flagged shuffle=True.

to my knowledge, people usually coding epoch and iteration for dataloader as follows:

for epoch in epochs:
   for iter, (input, target) in enumerate(dataloader):
     """Do Training"""

With this commonsense, I got question.
how enumerate function shuffle the data order in the dataloader?
what is in the dataloader called by the enumerate function?

Please let me solve this question.

Thank you in advance.

If you specify your dataloader with shuffle=True and a specific batch size the dataloader will shuffle your data and put the shuffled data into batches of your specified size.
If you then call enumerate() on the dataloader you will be able to loop over the shuffled batches of your predefined size and get a counter as well.

1 Like

enumerate() does not shuffle your data. It just loops over the already shuffled batches in your dataloader

1 Like

Thank @RaLo4 so much for your reply.

That means dataloader, with shuffle=True and given ‘batch_size’, calculate the number of batches and the loader will shuffle if it reaches to the end by enumerate.

Am I correct?

I’m not sure I understand what you mean by

but the data is already shuffled at the time you call enumerate() on the dataloader.

The dataloader first shuffles the data and puts it into batches. When you call enumerate(dataloader) you iterate over these shuffled batches.
The batches themselves get not shuffled by calling enumerate() but the data contained in these batches is shuffled by the dataloader (and also gets reshuffled every epoch according to the pytorch doc).

1 Like

Oh now I understand from your teaching.

enumerate call dataloader.

since the dataloader is called, it shuffles before enumerate works start.


1 Like

yes. basically.


1 Like

Thank you!!!
my question has been solved.

Another question is that how did you refer part of my reply in your reply??

mark the text you want to reply to and then a "Quote button pops up :wink:

1 Like

Thanks ~ XD
Have a good day

1 Like