Antonio_Ossa:

According to the docs, I understand that I have to provide my CustomBatchSampler instance as batch_sampler , not sampler , because it yields batches of indices. Is my understanding wrong or should the docs be improved (in case an issue about it is needed)?


I think I might have misunderstood the use case and might have given a wrong example.
The batch_sampler argument in the DataLoader will accept a sampler, which returns a batch of indices. Internally it will use the list comprehension (which you’ve linked to in the first post) and pass each index separately to __getitem__. This would make sure that the behavior of your custom Dataset can stay the same using the “standard” sampler and a BatchSampler.
However, we could just pass the BatchSampler as the sampler argument to the DataLoader (which I’ve done), so that we will get the complete batch of indices in __getitem__.
Let me know, if this is your use case or if I misunderstood it.



 Antonio_Ossa:

Curiously, using a for loop on my CustomBatchSampler in the training loop takes almost the same amount of time than using the DataLoader adapted from your example. Is that expected?


Besides the batching, shuffling, drop_last feature etc., the DataLoader might speed up your data loading pipeline, if you could load the data and process it in the background while your GPU is busy.
If your data is already in the system memory and you could just slice it to create batches, you most likely won’t see a speedup (and might even slow down the code due to some overhead in the DataLoader).