# Getting the number of training examples from the loader

**URL:** <https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868>\
**Category:** Uncategorized\
**Created:** [August 7, 2017, 9:54am UTC](https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868 "2017-08-07T09:54:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bixqu](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/b/958977/32.png) [@Bixqu](https://discuss.pytorch.org/u/Bixqu)\
**Post date:** [August 7, 2017, 9:54am UTC](https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868/1 "2017-08-07T09:54:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![fmassa](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fmassa/32/58_2.png) [@fmassa](https://discuss.pytorch.org/u/fmassa)\
**Post date:** [August 7, 2017, 10:25am UTC](https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868/2 "2017-08-07T10:25:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stash-196](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/s/c5a1d2/32.png) [@stash-196](https://discuss.pytorch.org/u/stash-196)\
**Post date:** [May 20, 2020, 3:48am UTC](https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868/3 "2020-05-20T03:48:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 21, 2020, 8:24am UTC](https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868/4 "2020-05-21T08:24:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Juans](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/c67d28/32.png) [@Juans](https://discuss.pytorch.org/u/Juans)\
**Post date:** [February 1, 2022, 11:12am UTC](https://discuss.pytorch.org/t/getting-the-number-of-training-examples-from-the-loader/5868/6 "2022-02-01T11:12:19Z")

</div>

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

```auto
len(trainloader.dataset)

```

In contrast `len(trainloader)` which gives the “number of batches”, see however second warning in the [docs](https://pytorch.org/docs/stable/data.html#torch.utils.data.DataLoader).
