# What do TensorDataset and DataLoader do?

**URL:** <https://discuss.pytorch.org/t/what-do-tensordataset-and-dataloader-do/107017>\
**Category:** Uncategorized\
**Created:** [December 24, 2020, 3:07am UTC](https://discuss.pytorch.org/t/what-do-tensordataset-and-dataloader-do/107017 "2020-12-24T03:07:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![J253](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@J253](https://discuss.pytorch.org/u/J253)\
**Post date:** [December 24, 2020, 3:07am UTC](https://discuss.pytorch.org/t/what-do-tensordataset-and-dataloader-do/107017/1 "2020-12-24T03:07:38Z")

</div>

I am used to using numpy arrays in the form X,y and fitting a model to those. I can’t understand what Datasets and Dataloaders do to the X and y vectors. I have searched on the internet a fair amount and I still cannot figure out what those functions do.

I am hoping someone on here can give me a simple quick explanation of what these functions do and are for. Here’s an example of where how I use these functions:

trainset = torch.utils.data.TensorDataset(X\_train, y\_train)

trainloader = torch.utils.data.DataLoader(trainset, batch\_size=128, shuffle=True, drop\_last=True)

Why do I do these to load data into a Neural Network, instead of just using X\_train, y\_train?

---

<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:** [January 5, 2021, 9:02am UTC](https://discuss.pytorch.org/t/what-do-tensordataset-and-dataloader-do/107017/2 "2021-01-05T09:02:11Z")

</div>

You can use the plain tensors as `X_train` and `y_train`, if you are able to load them completely (and push to the GPU without sacrificing too much memory).  
The `Dataset` is ab abstraction to be able to load and process each sample of your dataset lazily, while the `DataLoader` takes care of shuffling/sampling/weigthed sampling, batching, using multiprocessing to load the data, use pinned memory etc.  
[This tutorial](https://pytorch.org/tutorials/beginner/data_loading_tutorial.html) might be helpful to see the advantages of using this approach.  
That being said, you are of course fine to use the tensors directly, which might also be faster if you are using a tiny dataset.

---

<div class="post-metadata">

**Author:** ![J253](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/7bcc69/32.png) [@J253](https://discuss.pytorch.org/u/J253)\
**Post date:** [January 11, 2021, 2:42pm UTC](https://discuss.pytorch.org/t/what-do-tensordataset-and-dataloader-do/107017/3 "2021-01-11T14:42:45Z")

</div>

Hey thank you so much ptrblck. This is very helpful to my understanding! I feel it is good for me to understand what these functions do, so it is not like some black box that I am too scared to touch!

I see the difference now, and I will use a DataLoader now for the helpful reasons you mentioned.

All the best!

---

<div class="post-metadata">

**Author:** ![Sam\_Winchester](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sam_winchester/32/79315_2.png) [@Sam\_Winchester](https://discuss.pytorch.org/u/Sam_Winchester)\
**Post date:** [February 26, 2026, 2:31pm UTC](https://discuss.pytorch.org/t/what-do-tensordataset-and-dataloader-do/107017/4 "2026-02-26T14:31:09Z")

</div>

Think of them as two layers of abstraction in the PyTorch data pipeline:

- **`TensorDataset`** : Implements the ` __getitem__ ` and ` __len__ ` protocols for a set of tensors. It ensures your input data and targets stay aligned [🤔](https://rickycasino3.com/au)

- **`DataLoader`** : An iterable that abstracts away the complexity of batching, shuffling, and memory pinning (`pin_memory=True`).

Pro-tip: If you’re moving from NumPy/Scikit-learn, `TensorDataset` is the direct equivalent of your `(X, y)` tuple, but optimized for the PyTorch ecosystem.
