I’m not sure, if I understand the question correctly, but I think you are concerned about the print(idx) line and are wondering why it’s printing so many indices before yielding the first batch?
If you are using multiprocessing, the workers will load multiple batches in the background and add them to a queue.
Since the batch creation in your code snippet is really simple, the background worker is able to fetch 3 batches before the DataLoader loop returns the first data batch.
Setting num_workers=0 will only print the 0 and 1 indices before returning the batch.