{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# For tips on running notebooks in Google Colab, see\n",
    "# https://docs.pytorch.org/tutorials/beginner/colab\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Learn the Basics](intro.html) \\|\\|\n",
    "[Quickstart](quickstart_tutorial.html) \\|\\|\n",
    "[Tensors](tensorqs_tutorial.html) \\|\\| [Datasets &\n",
    "DataLoaders](data_tutorial.html) \\|\\| **Transforms** \\|\\| [Build\n",
    "Model](buildmodel_tutorial.html) \\|\\|\n",
    "[Autograd](autogradqs_tutorial.html) \\|\\|\n",
    "[Optimization](optimization_tutorial.html) \\|\\| [Save & Load\n",
    "Model](saveloadrun_tutorial.html)\n",
    "\n",
    "# Transforms\n",
    "\n",
    "Data does not always come in its final processed form that is required\n",
    "for training machine learning algorithms. We use **transforms** to\n",
    "perform some manipulation of the data and make it suitable for training.\n",
    "\n",
    "All TorchVision datasets have two parameters -`transform` to modify the\n",
    "features and `target_transform` to modify the labels - that accept\n",
    "callables containing the transformation logic. The\n",
    "[torchvision.transforms](https://pytorch.org/vision/stable/transforms.html)\n",
    "module offers several commonly-used transforms out of the box.\n",
    "\n",
    "The FashionMNIST features are in PIL Image format, and the labels are\n",
    "integers. For training, we need the features as normalized tensors, and\n",
    "the labels as one-hot encoded tensors. To make these transformations, we\n",
    "use the `torchvision.transforms.v2` API along with\n",
    "`torch.nn.functional.one_hot`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "from torchvision import datasets\n",
    "from torchvision.transforms import v2\n",
    "\n",
    "ds = datasets.FashionMNIST(\n",
    "    root=\"data\",\n",
    "    train=True,\n",
    "    download=True,\n",
    "    transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)]),\n",
    "    target_transform=v2.Lambda(\n",
    "        lambda y: F.one_hot(torch.tensor(y), num_classes=10).float()\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ToImage() and ToDtype()\n",
    "\n",
    "The `torchvision.transforms.v2` API replaces the legacy `ToTensor`\n",
    "transform with a two-step pipeline.\n",
    "[v2.ToImage](https://pytorch.org/vision/stable/generated/torchvision.transforms.v2.ToImage.html)\n",
    "converts a PIL image or NumPy `ndarray` into a\n",
    "`torchvision.tv_tensors.Image` tensor, and\n",
    "[v2.ToDtype](https://pytorch.org/vision/stable/generated/torchvision.transforms.v2.ToDtype.html)\n",
    "with `scale=True` casts it to `float32` and scales the pixel intensity\n",
    "values to the range \\[0., 1.\\].\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Lambda Transforms\n",
    "\n",
    "Lambda transforms apply any user-defined lambda function. Here, we use\n",
    "[torch.nn.functional.one_hot](https://pytorch.org/docs/stable/generated/torch.nn.functional.one_hot.html)\n",
    "to turn the integer label into a one-hot encoded tensor of size 10 (the\n",
    "number of labels in our dataset), then cast it to `float` to match the\n",
    "expected dtype.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "target_transform = v2.Lambda(\n",
    "    lambda y: F.one_hot(torch.tensor(y), num_classes=10).float()\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "------------------------------------------------------------------------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Further Reading\n",
    "\n",
    "-   [Getting started with transforms\n",
    "    v2](https://pytorch.org/vision/stable/auto_examples/transforms/plot_transforms_getting_started.html)\n",
    "-   [torchvision.transforms.v2\n",
    "    API](https://pytorch.org/vision/stable/transforms.html#v2-api-reference-recommended)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
