Question on next(iter(dataloader))

Hello peop!
So I know the following code
next(iter(dataloader))
makes dataloader object to be a iterable and “next” helps it to iterate over it.
I thought this code will give me a size of (batch_num, channel_num, image_size1, image_size2)
but this isn’t true, instead next(iter(dataloader))[0] (or fixing it with any index) gives me what I thought it should…

is there any answer for this?

The dataloader object might return multiple values as it’s calling into Dataset.__getitem__ to create the batch.
For a “standard” classification use case, your would return a data and target batch, so the first output would correspond to your desired shape.

thanks for the answer!
hm… so it’s getting multiple batches??
(In my case I think I specified 8 samples per batch and getitem gave me len() of 3 batches)
I’m still a little confused
you mind clarifying a bit?

thank you so much

If your Dataset.__getitem__ returns multiple tensors, next(iter(loader)) will return a batch for each output.
For the “standard” use case you would thus get a batch of inputs and a batch of targets.

Are you calling __getitem__ manually or how did you use the len() operation on it?
If next(iter(loader)) returned a batch of shape 3 even though you specified batch_size=5, then your Dataset might not have enough samples.
In that case you should check how the data is loaded and make sure you are able to load more than 3 samples.