.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "beginner/basics/data_tutorial.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_beginner_basics_data_tutorial.py: `Learn the Basics `_ || `Quickstart `_ || `Tensors `_ || **Datasets & DataLoaders** || `Transforms `_ || `Build Model `_ || `Autograd `_ || `Optimization `_ || `Save & Load Model `_ Datasets & DataLoaders ====================== .. GENERATED FROM PYTHON SOURCE LINES 18-32 Code for processing data samples can get messy and hard to maintain; we ideally want our dataset code to be decoupled from our model training code for better readability and modularity. PyTorch provides two data primitives: ``torch.utils.data.DataLoader`` and ``torch.utils.data.Dataset`` that allow you to use pre-loaded datasets as well as your own data. ``Dataset`` stores the samples and their corresponding labels, and ``DataLoader`` wraps an iterable around the ``Dataset`` to enable easy access to the samples. PyTorch domain libraries provide a number of pre-loaded datasets (such as FashionMNIST) that subclass ``torch.utils.data.Dataset`` and implement functions specific to the particular data. They can be used to prototype and benchmark your model. You can find them here: `Image Datasets `_, `Text Datasets `_, and `Audio Datasets `_ .. GENERATED FROM PYTHON SOURCE LINES 34-46 Loading a Dataset ------------------- Here is an example of how to load the `Fashion-MNIST `_ dataset from TorchVision. Fashion-MNIST is a dataset of Zalando’s article images consisting of 60,000 training examples and 10,000 test examples. Each example comprises a 28×28 grayscale image and an associated label from one of 10 classes. We load the `FashionMNIST Dataset `_ with the following parameters: - ``root`` is the path where the train/test data is stored, - ``train`` specifies training or test dataset, - ``download=True`` downloads the data from the internet if it's not available at ``root``. - ``transform`` and ``target_transform`` specify the feature and label transformations .. GENERATED FROM PYTHON SOURCE LINES 46-70 .. code-block:: default import torch from torch.utils.data import Dataset from torchvision import datasets from torchvision.transforms import ToTensor import matplotlib.pyplot as plt training_data = datasets.FashionMNIST( root="data", train=True, download=True, transform=ToTensor() ) test_data = datasets.FashionMNIST( root="data", train=False, download=True, transform=ToTensor() ) .. rst-class:: sphx-glr-script-out .. code-block:: none Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz Downloading http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/train-images-idx3-ubyte.gz to data/FashionMNIST/raw/train-images-idx3-ubyte.gz 0%| | 0/26421880 [00:00`_). .. GENERATED FROM PYTHON SOURCE LINES 229-240 .. code-block:: default # Display image and label. train_features, train_labels = next(iter(train_dataloader)) print(f"Feature batch shape: {train_features.size()}") print(f"Labels batch shape: {train_labels.size()}") img = train_features[0].squeeze() label = train_labels[0] plt.imshow(img, cmap="gray") plt.show() print(f"Label: {label}") .. image-sg:: /beginner/basics/images/sphx_glr_data_tutorial_002.png :alt: data tutorial :srcset: /beginner/basics/images/sphx_glr_data_tutorial_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Feature batch shape: torch.Size([64, 1, 28, 28]) Labels batch shape: torch.Size([64]) Label: 5 .. GENERATED FROM PYTHON SOURCE LINES 241-243 -------------- .. GENERATED FROM PYTHON SOURCE LINES 245-248 Further Reading ---------------- - `torch.utils.data API `_ .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 4.855 seconds) .. _sphx_glr_download_beginner_basics_data_tutorial.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: data_tutorial.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: data_tutorial.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_