{
 "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": [
    "# What is [torch.nn]{.title-ref} *really*?\n",
    "\n",
    "**Authors:** Jeremy Howard, [fast.ai](https://www.fast.ai). Thanks to\n",
    "Rachel Thomas and Francisco Ingham.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We recommend running this tutorial as a notebook, not a script. To\n",
    "download the notebook (`.ipynb`) file, click the link at the top of the\n",
    "page.\n",
    "\n",
    "PyTorch provides the elegantly designed modules and classes\n",
    "[torch.nn](https://pytorch.org/docs/stable/nn.html) ,\n",
    "[torch.optim](https://pytorch.org/docs/stable/optim.html) ,\n",
    "[Dataset](https://pytorch.org/docs/stable/data.html?highlight=dataset#torch.utils.data.Dataset)\n",
    ", and\n",
    "[DataLoader](https://pytorch.org/docs/stable/data.html?highlight=dataloader#torch.utils.data.DataLoader)\n",
    "to help you create and train neural networks. In order to fully utilize\n",
    "their power and customize them for your problem, you need to really\n",
    "understand exactly what they\\'re doing. To develop this understanding,\n",
    "we will first train basic neural net on the MNIST data set without using\n",
    "any features from these models; we will initially only use the most\n",
    "basic PyTorch tensor functionality. Then, we will incrementally add one\n",
    "feature from `torch.nn`, `torch.optim`, `Dataset`, or `DataLoader` at a\n",
    "time, showing exactly what each piece does, and how it works to make the\n",
    "code either more concise, or more flexible.\n",
    "\n",
    "**This tutorial assumes you already have PyTorch installed, and are\n",
    "familiar with the basics of tensor operations.** (If you\\'re familiar\n",
    "with Numpy array operations, you\\'ll find the PyTorch tensor operations\n",
    "used here nearly identical).\n",
    "\n",
    "# MNIST data setup\n",
    "\n",
    "We will use the classic\n",
    "[MNIST](https://yann.lecun.com/exdb/mnist/index.html) dataset, which\n",
    "consists of black-and-white images of hand-drawn digits (between 0 and\n",
    "9).\n",
    "\n",
    "We will use [pathlib](https://docs.python.org/3/library/pathlib.html)\n",
    "for dealing with paths (part of the Python 3 standard library), and will\n",
    "download the dataset using\n",
    "[requests](http://docs.python-requests.org/en/master/). We will only\n",
    "import modules when we use them, so you can see exactly what\\'s being\n",
    "used at each point.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import requests\n",
    "\n",
    "DATA_PATH = Path(\"data\")\n",
    "PATH = DATA_PATH / \"mnist\"\n",
    "\n",
    "PATH.mkdir(parents=True, exist_ok=True)\n",
    "\n",
    "URL = \"https://github.com/pytorch/tutorials/raw/main/_static/\"\n",
    "FILENAME = \"mnist.pkl.gz\"\n",
    "\n",
    "if not (PATH / FILENAME).exists():\n",
    "        content = requests.get(URL + FILENAME).content\n",
    "        (PATH / FILENAME).open(\"wb\").write(content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This dataset is in numpy array format, and has been stored using pickle,\n",
    "a python-specific format for serializing data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pickle\n",
    "import gzip\n",
    "\n",
    "with gzip.open((PATH / FILENAME).as_posix(), \"rb\") as f:\n",
    "        ((x_train, y_train), (x_valid, y_valid), _) = pickle.load(f, encoding=\"latin-1\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Each image is 28 x 28, and is being stored as a flattened row of length\n",
    "784 (=28x28). Let\\'s take a look at one; we need to reshape it to 2d\n",
    "first.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from matplotlib import pyplot\n",
    "import numpy as np\n",
    "\n",
    "pyplot.imshow(x_train[0].reshape((28, 28)), cmap=\"gray\")\n",
    "# ``pyplot.show()`` only if not on Colab\n",
    "try:\n",
    "    import google.colab\n",
    "except ImportError:\n",
    "    pyplot.show()\n",
    "print(x_train.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "PyTorch uses `torch.tensor`, rather than numpy arrays, so we need to\n",
    "convert our data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "\n",
    "x_train, y_train, x_valid, y_valid = map(\n",
    "    torch.tensor, (x_train, y_train, x_valid, y_valid)\n",
    ")\n",
    "n, c = x_train.shape\n",
    "print(x_train, y_train)\n",
    "print(x_train.shape)\n",
    "print(y_train.min(), y_train.max())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Neural net from scratch (without `torch.nn`)\n",
    "\n",
    "Let\\'s first create a model using nothing but PyTorch tensor operations.\n",
    "We\\'re assuming you\\'re already familiar with the basics of neural\n",
    "networks. (If you\\'re not, you can learn them at\n",
    "[course.fast.ai](https://course.fast.ai)).\n",
    "\n",
    "PyTorch provides methods to create random or zero-filled tensors, which\n",
    "we will use to create our weights and bias for a simple linear model.\n",
    "These are just regular tensors, with one very special addition: we tell\n",
    "PyTorch that they require a gradient. This causes PyTorch to record all\n",
    "of the operations done on the tensor, so that it can calculate the\n",
    "gradient during back-propagation *automatically*!\n",
    "\n",
    "For the weights, we set `requires_grad` **after** the initialization,\n",
    "since we don\\'t want that step included in the gradient. (Note that a\n",
    "trailing `_` in PyTorch signifies that the operation is performed\n",
    "in-place.)\n",
    "\n",
    "<div style=\"background-color: #54c7ec; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>NOTE:</strong></div>\n",
    "\n",
    "<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "\n",
    "<p>We are initializing the weights here with<a href=\"http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf\">Xavier initialisation</a>(by multiplying with <code>1/sqrt(n)</code>).</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import math\n",
    "\n",
    "weights = torch.randn(784, 10) / math.sqrt(784)\n",
    "weights.requires_grad_()\n",
    "bias = torch.zeros(10, requires_grad=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thanks to PyTorch\\'s ability to calculate gradients automatically, we\n",
    "can use any standard Python function (or callable object) as a model! So\n",
    "let\\'s just write a plain matrix multiplication and broadcasted addition\n",
    "to create a simple linear model. We also need an activation function, so\n",
    "we\\'ll write [log_softmax]{.title-ref} and use it. Remember: although\n",
    "PyTorch provides lots of prewritten loss functions, activation\n",
    "functions, and so forth, you can easily write your own using plain\n",
    "python. PyTorch will even create fast accelerator or vectorized CPU code\n",
    "for your function automatically.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def log_softmax(x):\n",
    "    return x - x.exp().sum(-1).log().unsqueeze(-1)\n",
    "\n",
    "def model(xb):\n",
    "    return log_softmax(xb @ weights + bias)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the above, the `@` stands for the matrix multiplication operation. We\n",
    "will call our function on one batch of data (in this case, 64 images).\n",
    "This is one *forward pass*. Note that our predictions won\\'t be any\n",
    "better than random at this stage, since we start with random weights.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "bs = 64  # batch size\n",
    "\n",
    "xb = x_train[0:bs]  # a mini-batch from x\n",
    "preds = model(xb)  # predictions\n",
    "preds[0], preds.shape\n",
    "print(preds[0], preds.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As you see, the `preds` tensor contains not only the tensor values, but\n",
    "also a gradient function. We\\'ll use this later to do backprop.\n",
    "\n",
    "Let\\'s implement negative log-likelihood to use as the loss function\n",
    "(again, we can just use standard Python):\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def nll(input, target):\n",
    "    return -input[range(target.shape[0]), target].mean()\n",
    "\n",
    "loss_func = nll"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s check our loss with our random model, so we can see if we improve\n",
    "after a backprop pass later.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "yb = y_train[0:bs]\n",
    "print(loss_func(preds, yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s also implement a function to calculate the accuracy of our model.\n",
    "For each prediction, if the index with the largest value matches the\n",
    "target value, then the prediction was correct.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def accuracy(out, yb):\n",
    "    preds = torch.argmax(out, dim=1)\n",
    "    return (preds == yb).float().mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s check the accuracy of our random model, so we can see if our\n",
    "accuracy improves as our loss improves.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(accuracy(preds, yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now run a training loop. For each iteration, we will:\n",
    "\n",
    "-   select a mini-batch of data (of size `bs`)\n",
    "-   use the model to make predictions\n",
    "-   calculate the loss\n",
    "-   `loss.backward()` updates the gradients of the model, in this case,\n",
    "    `weights` and `bias`.\n",
    "\n",
    "We now use these gradients to update the weights and bias. We do this\n",
    "within the `torch.no_grad()` context manager, because we do not want\n",
    "these actions to be recorded for our next calculation of the gradient.\n",
    "You can read more about how PyTorch\\'s Autograd records operations\n",
    "[here](https://pytorch.org/docs/stable/notes/autograd.html).\n",
    "\n",
    "We then set the gradients to zero, so that we are ready for the next\n",
    "loop. Otherwise, our gradients would record a running tally of all the\n",
    "operations that had happened (i.e. `loss.backward()` *adds* the\n",
    "gradients to whatever is already stored, rather than replacing them).\n",
    "\n",
    "<div style=\"background-color: #6bcebb; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>TIP:</strong></div>\n",
    "\n",
    "<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "\n",
    "<p>You can use the standard python debugger to step through PyTorchcode, allowing you to check the various variable values at each step.Uncomment <code>set_trace()</code> below to try it out.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from IPython.core.debugger import set_trace\n",
    "\n",
    "lr = 0.5  # learning rate\n",
    "epochs = 2  # how many epochs to train for\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    for i in range((n - 1) // bs + 1):\n",
    "        #         set_trace()\n",
    "        start_i = i * bs\n",
    "        end_i = start_i + bs\n",
    "        xb = x_train[start_i:end_i]\n",
    "        yb = y_train[start_i:end_i]\n",
    "        pred = model(xb)\n",
    "        loss = loss_func(pred, yb)\n",
    "\n",
    "        loss.backward()\n",
    "        with torch.no_grad():\n",
    "            weights -= weights.grad * lr\n",
    "            bias -= bias.grad * lr\n",
    "            weights.grad.zero_()\n",
    "            bias.grad.zero_()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That\\'s it: we\\'ve created and trained a minimal neural network (in this\n",
    "case, a logistic regression, since we have no hidden layers) entirely\n",
    "from scratch!\n",
    "\n",
    "Let\\'s check the loss and accuracy and compare those to what we got\n",
    "earlier. We expect that the loss will have decreased and accuracy to\n",
    "have increased, and they have.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(loss_func(model(xb), yb), accuracy(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using `torch.nn.functional`\n",
    "\n",
    "We will now refactor our code, so that it does the same thing as before,\n",
    "only we\\'ll start taking advantage of PyTorch\\'s `nn` classes to make it\n",
    "more concise and flexible. At each step from here, we should be making\n",
    "our code one or more of: shorter, more understandable, and/or more\n",
    "flexible.\n",
    "\n",
    "The first and easiest step is to make our code shorter by replacing our\n",
    "hand-written activation and loss functions with those from\n",
    "`torch.nn.functional` (which is generally imported into the namespace\n",
    "`F` by convention). This module contains all the functions in the\n",
    "`torch.nn` library (whereas other parts of the library contain classes).\n",
    "As well as a wide range of loss and activation functions, you\\'ll also\n",
    "find here some convenient functions for creating neural nets, such as\n",
    "pooling functions. (There are also functions for doing convolutions,\n",
    "linear layers, etc, but as we\\'ll see, these are usually better handled\n",
    "using other parts of the library.)\n",
    "\n",
    "If you\\'re using negative log likelihood loss and log softmax\n",
    "activation, then Pytorch provides a single function `F.cross_entropy`\n",
    "that combines the two. So we can even remove the activation function\n",
    "from our model.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch.nn.functional as F\n",
    "\n",
    "loss_func = F.cross_entropy\n",
    "\n",
    "def model(xb):\n",
    "    return xb @ weights + bias"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that we no longer call `log_softmax` in the `model` function.\n",
    "Let\\'s confirm that our loss and accuracy are the same as before:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(loss_func(model(xb), yb), accuracy(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refactor using `nn.Module`\n",
    "\n",
    "Next up, we\\'ll use `nn.Module` and `nn.Parameter`, for a clearer and\n",
    "more concise training loop. We subclass `nn.Module` (which itself is a\n",
    "class and able to keep track of state). In this case, we want to create\n",
    "a class that holds our weights, bias, and method for the forward step.\n",
    "`nn.Module` has a number of attributes and methods (such as\n",
    "`.parameters()` and `.zero_grad()`) which we will be using.\n",
    "\n",
    "<div style=\"background-color: #54c7ec; color: #fff; font-weight: 700; padding-left: 10px; padding-top: 5px; padding-bottom: 5px\"><strong>NOTE:</strong></div>\n",
    "\n",
    "<div style=\"background-color: #f3f4f7; padding-left: 10px; padding-top: 10px; padding-bottom: 10px; padding-right: 10px\">\n",
    "\n",
    "<p><code>nn.Module</code> (uppercase M) is a PyTorch specific concept, and is aclass we'll be using a lot. <code>nn.Module</code> is not to be confused with the Pythonconcept of a (lowercase <code>m</code>) <a href=\"https://docs.python.org/3/tutorial/modules.html\">module</a>,which is a file of Python code that can be imported.</p>\n",
    "\n",
    "</div>\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch import nn\n",
    "\n",
    "class Mnist_Logistic(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.weights = nn.Parameter(torch.randn(784, 10) / math.sqrt(784))\n",
    "        self.bias = nn.Parameter(torch.zeros(10))\n",
    "\n",
    "    def forward(self, xb):\n",
    "        return xb @ self.weights + self.bias"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since we\\'re now using an object instead of just using a function, we\n",
    "first have to instantiate our model:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = Mnist_Logistic()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can calculate the loss in the same way as before. Note that\n",
    "`nn.Module` objects are used as if they are functions (i.e they are\n",
    "*callable*), but behind the scenes Pytorch will call our `forward`\n",
    "method automatically.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Previously for our training loop we had to update the values for each\n",
    "parameter by name, and manually zero out the grads for each parameter\n",
    "separately, like this:\n",
    "\n",
    "``` python\n",
    "with torch.no_grad():\n",
    "    weights -= weights.grad * lr\n",
    "    bias -= bias.grad * lr\n",
    "    weights.grad.zero_()\n",
    "    bias.grad.zero_()\n",
    "```\n",
    "\n",
    "Now we can take advantage of model.parameters() and model.zero_grad()\n",
    "(which are both defined by PyTorch for `nn.Module`) to make those steps\n",
    "more concise and less prone to the error of forgetting some of our\n",
    "parameters, particularly if we had a more complicated model:\n",
    "\n",
    "``` python\n",
    "with torch.no_grad():\n",
    "    for p in model.parameters(): p -= p.grad * lr\n",
    "    model.zero_grad()\n",
    "```\n",
    "\n",
    "We\\'ll wrap our little training loop in a `fit` function so we can run\n",
    "it again later.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def fit():\n",
    "    for epoch in range(epochs):\n",
    "        for i in range((n - 1) // bs + 1):\n",
    "            start_i = i * bs\n",
    "            end_i = start_i + bs\n",
    "            xb = x_train[start_i:end_i]\n",
    "            yb = y_train[start_i:end_i]\n",
    "            pred = model(xb)\n",
    "            loss = loss_func(pred, yb)\n",
    "\n",
    "            loss.backward()\n",
    "            with torch.no_grad():\n",
    "                for p in model.parameters():\n",
    "                    p -= p.grad * lr\n",
    "                model.zero_grad()\n",
    "\n",
    "fit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s double-check that our loss has gone down:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refactor using `nn.Linear`\n",
    "\n",
    "We continue to refactor our code. Instead of manually defining and\n",
    "initializing `self.weights` and `self.bias`, and calculating\n",
    "`xb  @ self.weights + self.bias`, we will instead use the Pytorch class\n",
    "[nn.Linear](https://pytorch.org/docs/stable/nn.html#linear-layers) for a\n",
    "linear layer, which does all that for us. Pytorch has many types of\n",
    "predefined layers that can greatly simplify our code, and often makes it\n",
    "faster too.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class Mnist_Logistic(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.lin = nn.Linear(784, 10)\n",
    "\n",
    "    def forward(self, xb):\n",
    "        return self.lin(xb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We instantiate our model and calculate the loss in the same way as\n",
    "before:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = Mnist_Logistic()\n",
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We are still able to use our same `fit` method as before.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fit()\n",
    "\n",
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refactor using `torch.optim`\n",
    "\n",
    "Pytorch also has a package with various optimization algorithms,\n",
    "`torch.optim`. We can use the `step` method from our optimizer to take a\n",
    "forward step, instead of manually updating each parameter.\n",
    "\n",
    "This will let us replace our previous manually coded optimization step:\n",
    "\n",
    "``` python\n",
    "with torch.no_grad():\n",
    "    for p in model.parameters(): p -= p.grad * lr\n",
    "    model.zero_grad()\n",
    "```\n",
    "\n",
    "and instead use just:\n",
    "\n",
    "``` python\n",
    "opt.step()\n",
    "opt.zero_grad()\n",
    "```\n",
    "\n",
    "(`optim.zero_grad()` resets the gradient to 0 and we need to call it\n",
    "before computing the gradient for the next minibatch.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch import optim"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We\\'ll define a little function to create our model and optimizer so we\n",
    "can reuse it in the future.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def get_model():\n",
    "    model = Mnist_Logistic()\n",
    "    return model, optim.SGD(model.parameters(), lr=lr)\n",
    "\n",
    "model, opt = get_model()\n",
    "print(loss_func(model(xb), yb))\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    for i in range((n - 1) // bs + 1):\n",
    "        start_i = i * bs\n",
    "        end_i = start_i + bs\n",
    "        xb = x_train[start_i:end_i]\n",
    "        yb = y_train[start_i:end_i]\n",
    "        pred = model(xb)\n",
    "        loss = loss_func(pred, yb)\n",
    "\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        opt.zero_grad()\n",
    "\n",
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refactor using Dataset\n",
    "\n",
    "PyTorch has an abstract Dataset class. A Dataset can be anything that\n",
    "has a `__len__` function (called by Python\\'s standard `len` function)\n",
    "and a `__getitem__` function as a way of indexing into it. [This\n",
    "tutorial](https://pytorch.org/tutorials/beginner/data_loading_tutorial.html)\n",
    "walks through a nice example of creating a custom\n",
    "`FacialLandmarkDataset` class as a subclass of `Dataset`.\n",
    "\n",
    "PyTorch\\'s\n",
    "[TensorDataset](https://pytorch.org/docs/stable/_modules/torch/utils/data/dataset.html#TensorDataset)\n",
    "is a Dataset wrapping tensors. By defining a length and way of indexing,\n",
    "this also gives us a way to iterate, index, and slice along the first\n",
    "dimension of a tensor. This will make it easier to access both the\n",
    "independent and dependent variables in the same line as we train.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.utils.data import TensorDataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Both `x_train` and `y_train` can be combined in a single\n",
    "`TensorDataset`, which will be easier to iterate over and slice.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_ds = TensorDataset(x_train, y_train)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Previously, we had to iterate through minibatches of `x` and `y` values\n",
    "separately:\n",
    "\n",
    "``` python\n",
    "xb = x_train[start_i:end_i]\n",
    "yb = y_train[start_i:end_i]\n",
    "```\n",
    "\n",
    "Now, we can do these two steps together:\n",
    "\n",
    "``` python\n",
    "xb,yb = train_ds[i*bs : i*bs+bs]\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model, opt = get_model()\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    for i in range((n - 1) // bs + 1):\n",
    "        xb, yb = train_ds[i * bs: i * bs + bs]\n",
    "        pred = model(xb)\n",
    "        loss = loss_func(pred, yb)\n",
    "\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        opt.zero_grad()\n",
    "\n",
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Refactor using `DataLoader`\n",
    "\n",
    "PyTorch\\'s `DataLoader` is responsible for managing batches. You can\n",
    "create a `DataLoader` from any `Dataset`. `DataLoader` makes it easier\n",
    "to iterate over batches. Rather than having to use\n",
    "`train_ds[i*bs : i*bs+bs]`, the `DataLoader` gives us each minibatch\n",
    "automatically.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from torch.utils.data import DataLoader\n",
    "\n",
    "train_ds = TensorDataset(x_train, y_train)\n",
    "train_dl = DataLoader(train_ds, batch_size=bs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Previously, our loop iterated over batches `(xb, yb)` like this:\n",
    "\n",
    "``` python\n",
    "for i in range((n-1)//bs + 1):\n",
    "    xb,yb = train_ds[i*bs : i*bs+bs]\n",
    "    pred = model(xb)\n",
    "```\n",
    "\n",
    "Now, our loop is much cleaner, as `(xb, yb)` are loaded automatically\n",
    "from the data loader:\n",
    "\n",
    "``` python\n",
    "for xb,yb in train_dl:\n",
    "    pred = model(xb)\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model, opt = get_model()\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    for xb, yb in train_dl:\n",
    "        pred = model(xb)\n",
    "        loss = loss_func(pred, yb)\n",
    "\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        opt.zero_grad()\n",
    "\n",
    "print(loss_func(model(xb), yb))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Thanks to PyTorch\\'s `nn.Module`, `nn.Parameter`, `Dataset`, and\n",
    "`DataLoader`, our training loop is now dramatically smaller and easier\n",
    "to understand. Let\\'s now try to add the basic features necessary to\n",
    "create effective models in practice.\n",
    "\n",
    "# Add validation\n",
    "\n",
    "In section 1, we were just trying to get a reasonable training loop set\n",
    "up for use on our training data. In reality, you **always** should also\n",
    "have a [validation\n",
    "set](https://www.fast.ai/2017/11/13/validation-sets/), in order to\n",
    "identify if you are overfitting.\n",
    "\n",
    "Shuffling the training data is\n",
    "[important](https://www.quora.com/Does-the-order-of-training-data-matter-when-training-neural-networks)\n",
    "to prevent correlation between batches and overfitting. On the other\n",
    "hand, the validation loss will be identical whether we shuffle the\n",
    "validation set or not. Since shuffling takes extra time, it makes no\n",
    "sense to shuffle the validation data.\n",
    "\n",
    "We\\'ll use a batch size for the validation set that is twice as large as\n",
    "that for the training set. This is because the validation set does not\n",
    "need backpropagation and thus takes less memory (it doesn\\'t need to\n",
    "store the gradients). We take advantage of this to use a larger batch\n",
    "size and compute the loss more quickly.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_ds = TensorDataset(x_train, y_train)\n",
    "train_dl = DataLoader(train_ds, batch_size=bs, shuffle=True)\n",
    "\n",
    "valid_ds = TensorDataset(x_valid, y_valid)\n",
    "valid_dl = DataLoader(valid_ds, batch_size=bs * 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will calculate and print the validation loss at the end of each\n",
    "epoch.\n",
    "\n",
    "(Note that we always call `model.train()` before training, and\n",
    "`model.eval()` before inference, because these are used by layers such\n",
    "as `nn.BatchNorm2d` and `nn.Dropout` to ensure appropriate behavior for\n",
    "these different phases.)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model, opt = get_model()\n",
    "\n",
    "for epoch in range(epochs):\n",
    "    model.train()\n",
    "    for xb, yb in train_dl:\n",
    "        pred = model(xb)\n",
    "        loss = loss_func(pred, yb)\n",
    "\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        opt.zero_grad()\n",
    "\n",
    "    model.eval()\n",
    "    with torch.no_grad():\n",
    "        valid_loss = sum(loss_func(model(xb), yb) for xb, yb in valid_dl)\n",
    "\n",
    "    print(epoch, valid_loss / len(valid_dl))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create fit() and get_data()\n",
    "\n",
    "We\\'ll now do a little refactoring of our own. Since we go through a\n",
    "similar process twice of calculating the loss for both the training set\n",
    "and the validation set, let\\'s make that into its own function,\n",
    "`loss_batch`, which computes the loss for one batch.\n",
    "\n",
    "We pass an optimizer in for the training set, and use it to perform\n",
    "backprop. For the validation set, we don\\'t pass an optimizer, so the\n",
    "method doesn\\'t perform backprop.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def loss_batch(model, loss_func, xb, yb, opt=None):\n",
    "    loss = loss_func(model(xb), yb)\n",
    "\n",
    "    if opt is not None:\n",
    "        loss.backward()\n",
    "        opt.step()\n",
    "        opt.zero_grad()\n",
    "\n",
    "    return loss.item(), len(xb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`fit` runs the necessary operations to train our model and compute the\n",
    "training and validation losses for each epoch.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def fit(epochs, model, loss_func, opt, train_dl, valid_dl):\n",
    "    for epoch in range(epochs):\n",
    "        model.train()\n",
    "        for xb, yb in train_dl:\n",
    "            loss_batch(model, loss_func, xb, yb, opt)\n",
    "\n",
    "        model.eval()\n",
    "        with torch.no_grad():\n",
    "            losses, nums = zip(\n",
    "                *[loss_batch(model, loss_func, xb, yb) for xb, yb in valid_dl]\n",
    "            )\n",
    "        val_loss = np.sum(np.multiply(losses, nums)) / np.sum(nums)\n",
    "\n",
    "        print(epoch, val_loss)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`get_data` returns dataloaders for the training and validation sets.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def get_data(train_ds, valid_ds, bs):\n",
    "    return (\n",
    "        DataLoader(train_ds, batch_size=bs, shuffle=True),\n",
    "        DataLoader(valid_ds, batch_size=bs * 2),\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now, our whole process of obtaining the data loaders and fitting the\n",
    "model can be run in 3 lines of code:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "train_dl, valid_dl = get_data(train_ds, valid_ds, bs)\n",
    "model, opt = get_model()\n",
    "fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can use these basic 3 lines of code to train a wide variety of\n",
    "models. Let\\'s see if we can use them to train a convolutional neural\n",
    "network (CNN)!\n",
    "\n",
    "# Switch to CNN\n",
    "\n",
    "We are now going to build our neural network with three convolutional\n",
    "layers. Because none of the functions in the previous section assume\n",
    "anything about the model form, we\\'ll be able to use them to train a CNN\n",
    "without any modification.\n",
    "\n",
    "We will use PyTorch\\'s predefined\n",
    "[Conv2d](https://pytorch.org/docs/stable/nn.html#torch.nn.Conv2d) class\n",
    "as our convolutional layer. We define a CNN with 3 convolutional layers.\n",
    "Each convolution is followed by a ReLU. At the end, we perform an\n",
    "average pooling. (Note that `view` is PyTorch\\'s version of Numpy\\'s\n",
    "`reshape`)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class Mnist_CNN(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1)\n",
    "        self.conv2 = nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1)\n",
    "        self.conv3 = nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1)\n",
    "\n",
    "    def forward(self, xb):\n",
    "        xb = xb.view(-1, 1, 28, 28)\n",
    "        xb = F.relu(self.conv1(xb))\n",
    "        xb = F.relu(self.conv2(xb))\n",
    "        xb = F.relu(self.conv3(xb))\n",
    "        xb = F.avg_pool2d(xb, 4)\n",
    "        return xb.view(-1, xb.size(1))\n",
    "\n",
    "lr = 0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Momentum](https://cs231n.github.io/neural-networks-3/#sgd) is a\n",
    "variation on stochastic gradient descent that takes previous updates\n",
    "into account as well and generally leads to faster training.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = Mnist_CNN()\n",
    "opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)\n",
    "\n",
    "fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using `nn.Sequential`\n",
    "\n",
    "`torch.nn` has another handy class we can use to simplify our code:\n",
    "[Sequential](https://pytorch.org/docs/stable/nn.html#torch.nn.Sequential)\n",
    ". A `Sequential` object runs each of the modules contained within it, in\n",
    "a sequential manner. This is a simpler way of writing our neural\n",
    "network.\n",
    "\n",
    "To take advantage of this, we need to be able to easily define a\n",
    "**custom layer** from a given function. For instance, PyTorch doesn\\'t\n",
    "have a [view]{.title-ref} layer, and we need to create one for our\n",
    "network. `Lambda` will create a layer that we can then use when defining\n",
    "a network with `Sequential`.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "class Lambda(nn.Module):\n",
    "    def __init__(self, func):\n",
    "        super().__init__()\n",
    "        self.func = func\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.func(x)\n",
    "\n",
    "\n",
    "def preprocess(x):\n",
    "    return x.view(-1, 1, 28, 28)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The model created with `Sequential` is simple:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = nn.Sequential(\n",
    "    Lambda(preprocess),\n",
    "    nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1),\n",
    "    nn.ReLU(),\n",
    "    nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1),\n",
    "    nn.ReLU(),\n",
    "    nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1),\n",
    "    nn.ReLU(),\n",
    "    nn.AvgPool2d(4),\n",
    "    Lambda(lambda x: x.view(x.size(0), -1)),\n",
    ")\n",
    "\n",
    "opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)\n",
    "\n",
    "fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Wrapping `DataLoader`\n",
    "\n",
    "Our CNN is fairly concise, but it only works with MNIST, because:\n",
    "\n",
    ":   -   It assumes the input is a 28\\*28 long vector\n",
    "    -   It assumes that the final CNN grid size is 4\\*4 (since that\\'s\n",
    "        the average pooling kernel size we used)\n",
    "\n",
    "Let\\'s get rid of these two assumptions, so our model works with any 2d\n",
    "single channel image. First, we can remove the initial Lambda layer by\n",
    "moving the data preprocessing into a generator:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def preprocess(x, y):\n",
    "    return x.view(-1, 1, 28, 28), y\n",
    "\n",
    "\n",
    "class WrappedDataLoader:\n",
    "    def __init__(self, dl, func):\n",
    "        self.dl = dl\n",
    "        self.func = func\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.dl)\n",
    "\n",
    "    def __iter__(self):\n",
    "        for b in self.dl:\n",
    "            yield (self.func(*b))\n",
    "\n",
    "train_dl, valid_dl = get_data(train_ds, valid_ds, bs)\n",
    "train_dl = WrappedDataLoader(train_dl, preprocess)\n",
    "valid_dl = WrappedDataLoader(valid_dl, preprocess)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we can replace `nn.AvgPool2d` with `nn.AdaptiveAvgPool2d`, which\n",
    "allows us to define the size of the *output* tensor we want, rather than\n",
    "the *input* tensor we have. As a result, our model will work with any\n",
    "size input.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model = nn.Sequential(\n",
    "    nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1),\n",
    "    nn.ReLU(),\n",
    "    nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1),\n",
    "    nn.ReLU(),\n",
    "    nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1),\n",
    "    nn.ReLU(),\n",
    "    nn.AdaptiveAvgPool2d(1),\n",
    "    Lambda(lambda x: x.view(x.size(0), -1)),\n",
    ")\n",
    "\n",
    "opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s try it out:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using your\n",
    "[Accelerator](https://pytorch.org/docs/stable/torch.html#accelerators)\n",
    "\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\-\\--\n",
    "\n",
    "If you\\'re lucky enough to have access to an accelerator such as CUDA\n",
    "(you can rent one for about \\$0.50/hour from most cloud providers) you\n",
    "can use it to speed up your code. First check that your accelerator is\n",
    "working in Pytorch:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# If the current accelerator is available, we will use it. Otherwise, we use the CPU.\n",
    "device = torch.accelerator.current_accelerator().type if torch.accelerator.is_available() else \"cpu\"\n",
    "print(f\"Using {device} device\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let\\'s update `preprocess` to move batches to the accelerator:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def preprocess(x, y):\n",
    "    return x.view(-1, 1, 28, 28).to(device), y.to(device)\n",
    "\n",
    "\n",
    "train_dl, valid_dl = get_data(train_ds, valid_ds, bs)\n",
    "train_dl = WrappedDataLoader(train_dl, preprocess)\n",
    "valid_dl = WrappedDataLoader(valid_dl, preprocess)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, we can move our model to the accelerator.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model.to(device)\n",
    "opt = optim.SGD(model.parameters(), lr=lr, momentum=0.9)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should find it runs faster now:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fit(epochs, model, loss_func, opt, train_dl, valid_dl)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Closing thoughts\n",
    "\n",
    "We now have a general data pipeline and training loop which you can use\n",
    "for training many types of models using Pytorch. To see how simple\n",
    "training a model can now be, take a look at the [mnist_sample\n",
    "notebook](https://github.com/fastai/fastai_dev/blob/master/dev_nb/mnist_sample.ipynb).\n",
    "\n",
    "Of course, there are many things you\\'ll want to add, such as data\n",
    "augmentation, hyperparameter tuning, monitoring training, transfer\n",
    "learning, and so forth. These features are available in the fastai\n",
    "library, which has been developed using the same design approach shown\n",
    "in this tutorial, providing a natural next step for practitioners\n",
    "looking to take their models further.\n",
    "\n",
    "We promised at the start of this tutorial we\\'d explain through example\n",
    "each of `torch.nn`, `torch.optim`, `Dataset`, and `DataLoader`. So\n",
    "let\\'s summarize what we\\'ve seen:\n",
    "\n",
    "> -   `torch.nn`:\n",
    ">     -   `Module`: creates a callable which behaves like a function,\n",
    ">         but can also contain state(such as neural net layer weights).\n",
    ">         It knows what `Parameter` (s) it contains and can zero all\n",
    ">         their gradients, loop through them for weight updates, etc.\n",
    ">     -   `Parameter`: a wrapper for a tensor that tells a `Module` that\n",
    ">         it has weights that need updating during backprop. Only\n",
    ">         tensors with the [requires_grad]{.title-ref} attribute set are\n",
    ">         updated\n",
    ">     -   `functional`: a module(usually imported into the `F` namespace\n",
    ">         by convention) which contains activation functions, loss\n",
    ">         functions, etc, as well as non-stateful versions of layers\n",
    ">         such as convolutional and linear layers.\n",
    "> -   `torch.optim`: Contains optimizers such as `SGD`, which update the\n",
    ">     weights of `Parameter` during the backward step\n",
    "> -   `Dataset`: An abstract interface of objects with a `__len__` and a\n",
    ">     `__getitem__`, including classes provided with Pytorch such as\n",
    ">     `TensorDataset`\n",
    "> -   `DataLoader`: Takes any `Dataset` and creates an iterator which\n",
    ">     returns batches of data.\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
}
