IndexError when indexing Tensor (Iterating through DataLoader)

Hi, very new to Pytorch and this forum generally. I hope i’m posting in the right thread, and/or using the right terminology. Please feel free to correct me if i’m wrong (very willing to learn)!

I’m trying to iterate through a DataLoader i’ve created for my validation dataset (batch size 16, sampler is Sequential Sampler). The original dataset I pass to the sampler isof length 22. The problem is, it’s raising an IndexError

Printing the indices out, it seems the sample in index 1 is causing the error (see below)

Any hints on how to fix, or insight as to what the error might be? Thanks!

If your Dataset has the length 22, then the last index should be 21, which would also explain the IndexError.

Could you check the length of the Dataset via print(len(dataset))? I assume it would return a value which is larger than 22.
In that case, check the Dataset.__len__ method and make sure you are returning the right value for the length. :wink:

Let me know, if this helps.

Thank you so much for your help! It turns out the issue wasn’t that, but my loading of a different-sized data-set (instead of the intended one) into DataLoader. Very rookie mistake, but taught me to be more careful next time!