AttributeError: 'DataLoader' object has no attribute '__getitem__'
I want to get the first image within Pytorch's dataloader as per this doc here: https://pytorch.org/vision/stable/generated/torchvision.datasets.CIFAR100.html
However I get the error in the title when I try doing this: trainloader.__getitem__(0)
Am I misunderstanding their docs?
Here's my code:
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

trainset = torchvision.datasets.CIFAR100(root= &quot;./data&quot;, train = True, transform=preprocess, download=True)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
                                          shuffle=True, num_workers=2)
trainloader.__getitem__(0)


Those are the docs for a torch Dataset, which does in fact have a __getitem__ implemented. However, you are calling __getitem__ on trainloader, a DataLoader which does not have that method.
So this should do what you expect:
trainset.__getitem__(0)

When you get something from a Dataset, you are getting a single observation. DataLoaders should not be used to return single items in this manner; instead, they are for fetching a sampled batch of observations. That's why they don't support getting a single obs through __getitem__.
It's also worth reading a bit about how __getitem__ works (for example here); you can use it to subscript an object directly. trainset[0] is equivalent to trainset.__getitem__(0).
