Getting the number of training examples from the loader

I am training a very simple net on CIFAR10 and I want to get the number of examples from the loader,

trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2)

Is there a way to use trainloader.number_of_examples or something similar?

The number of examples is present from the dataset, not the dataloader.
So you can get the number of examples by using len(trainset)

1 Like

how about telling dataloader how many samples to load? Is that possible?

You could use a SubsetSampler, pass the desired indices to it, and pass the sampler to the DataLoader in order to define the number of returned samples.

1 Like

If anyone is looking to get the number of data points in the training set from the DataLoader object:

len(trainloader.dataset)

In contrast len(trainloader) which gives the “number of batches”, see however second warning in the docs.

8 Likes