Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/main/beginner_source/basics/transforms_tutorial.py
Views: 713
"""1`Learn the Basics <intro.html>`_ ||2`Quickstart <quickstart_tutorial.html>`_ ||3`Tensors <tensorqs_tutorial.html>`_ ||4`Datasets & DataLoaders <data_tutorial.html>`_ ||5**Transforms** ||6`Build Model <buildmodel_tutorial.html>`_ ||7`Autograd <autogradqs_tutorial.html>`_ ||8`Optimization <optimization_tutorial.html>`_ ||9`Save & Load Model <saveloadrun_tutorial.html>`_1011Transforms12===================1314Data does not always come in its final processed form that is required for15training machine learning algorithms. We use **transforms** to perform some16manipulation of the data and make it suitable for training.1718All TorchVision datasets have two parameters -``transform`` to modify the features and19``target_transform`` to modify the labels - that accept callables containing the transformation logic.20The `torchvision.transforms <https://pytorch.org/vision/stable/transforms.html>`_ module offers21several commonly-used transforms out of the box.2223The FashionMNIST features are in PIL Image format, and the labels are integers.24For training, we need the features as normalized tensors, and the labels as one-hot encoded tensors.25To make these transformations, we use ``ToTensor`` and ``Lambda``.26"""2728import torch29from torchvision import datasets30from torchvision.transforms import ToTensor, Lambda3132ds = datasets.FashionMNIST(33root="data",34train=True,35download=True,36transform=ToTensor(),37target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))38)3940#################################################41# ToTensor()42# -------------------------------43#44# `ToTensor <https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.ToTensor>`_45# converts a PIL image or NumPy ``ndarray`` into a ``FloatTensor``. and scales46# the image's pixel intensity values in the range [0., 1.]47#4849##############################################50# Lambda Transforms51# -------------------------------52#53# Lambda transforms apply any user-defined lambda function. Here, we define a function54# to turn the integer into a one-hot encoded tensor.55# It first creates a zero tensor of size 10 (the number of labels in our dataset) and calls56# `scatter_ <https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_.html>`_ which assigns a57# ``value=1`` on the index as given by the label ``y``.5859target_transform = Lambda(lambda y: torch.zeros(6010, dtype=torch.float).scatter_(dim=0, index=torch.tensor(y), value=1))6162######################################################################63# --------------64#6566#################################################################67# Further Reading68# ~~~~~~~~~~~~~~~~~69# - `torchvision.transforms API <https://pytorch.org/vision/stable/transforms.html>`_707172