{
 "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) \\|\\|\n",
    "[Transforms](transforms_tutorial.html) \\|\\| [Build\n",
    "Model](buildmodel_tutorial.html) \\|\\|\n",
    "[Autograd](autogradqs_tutorial.html) \\|\\| **Optimization** \\|\\| [Save &\n",
    "Load Model](saveloadrun_tutorial.html)\n",
    "\n",
    "# Optimizing Model Parameters\n",
    "\n",
    "Now that we have a model and data it\\'s time to train, validate and test\n",
    "our model by optimizing its parameters on our data. Training a model is\n",
    "an iterative process; in each iteration the model makes a guess about\n",
    "the output, calculates the error in its guess (*loss*), collects the\n",
    "derivatives of the error with respect to its parameters (as we saw in\n",
    "the [previous section](autogradqs_tutorial.html)), and **optimizes**\n",
    "these parameters using gradient descent. For a more detailed walkthrough\n",
    "of this process, check out this video on [backpropagation from\n",
    "3Blue1Brown](https://www.youtube.com/watch?v=tIeHLnjs5U8).\n",
    "\n",
    "## Prerequisite Code\n",
    "\n",
    "We load the code from the previous sections on [Datasets &\n",
    "DataLoaders](data_tutorial.html) and [Build\n",
    "Model](buildmodel_tutorial.html).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn\n",
    "from torch.utils.data import DataLoader\n",
    "from torchvision import datasets\n",
    "from torchvision.transforms import v2\n",
    "\n",
    "training_data = datasets.FashionMNIST(\n",
    "    root=\"data\",\n",
    "    train=True,\n",
    "    download=True,\n",
    "    transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])\n",
    ")\n",
    "\n",
    "test_data = datasets.FashionMNIST(\n",
    "    root=\"data\",\n",
    "    train=False,\n",
    "    download=True,\n",
    "    transform=v2.Compose([v2.ToImage(), v2.ToDtype(torch.float32, scale=True)])\n",
    ")\n",
    "\n",
    "train_dataloader = DataLoader(training_data, batch_size=64)\n",
    "test_dataloader = DataLoader(test_data, batch_size=64)\n",
    "\n",
    "class NeuralNetwork(nn.Module):\n",
    "    def __init__(self):\n",
    "        super().__init__()\n",
    "        self.flatten = nn.Flatten()\n",
    "        self.linear_relu_stack = nn.Sequential(\n",
    "            nn.Linear(28*28, 512),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(512, 512),\n",
    "            nn.ReLU(),\n",
    "            nn.Linear(512, 10),\n",
    "        )\n",
    "\n",
    "    def forward(self, x):\n",
    "        x = self.flatten(x)\n",
    "        logits = self.linear_relu_stack(x)\n",
    "        return logits\n",
    "\n",
    "model = NeuralNetwork()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hyperparameters\n",
    "\n",
    "Hyperparameters are adjustable parameters that let you control the model\n",
    "optimization process. Different hyperparameter values can impact model\n",
    "training and convergence rates ([read\n",
    "more](https://pytorch.org/tutorials/beginner/hyperparameter_tuning_tutorial.html)\n",
    "about hyperparameter tuning)\n",
    "\n",
    "We define the following hyperparameters for training:\n",
    "\n",
    "-   **Number of Epochs** - the number of times to iterate over the\n",
    "    dataset\n",
    "-   **Batch Size** - the number of data samples propagated through the\n",
    "    network before the parameters are updated\n",
    "-   **Learning Rate** - how much to update models parameters at each\n",
    "    batch/epoch. Smaller values yield slow learning speed, while large\n",
    "    values may result in unpredictable behavior during training.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "learning_rate = 1e-3\n",
    "batch_size = 64\n",
    "epochs = 5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Optimization Loop\n",
    "\n",
    "Once we set our hyperparameters, we can then train and optimize our\n",
    "model with an optimization loop. Each iteration of the optimization loop\n",
    "is called an **epoch**.\n",
    "\n",
    "Each epoch consists of two main parts:\n",
    "\n",
    "-   **The Train Loop** - iterate over the training dataset and try to\n",
    "    converge to optimal parameters.\n",
    "-   **The Validation/Test Loop** - iterate over the test dataset to\n",
    "    check if model performance is improving.\n",
    "\n",
    "Let\\'s briefly familiarize ourselves with some of the concepts used in\n",
    "the training loop. Jump ahead to see the\n",
    "`full-impl-label`{.interpreted-text role=\"ref\"} of the optimization\n",
    "loop.\n",
    "\n",
    "## Loss Function\n",
    "\n",
    "When presented with some training data, our untrained network is likely\n",
    "not to give the correct answer. **Loss function** measures the degree of\n",
    "dissimilarity of obtained result to the target value, and it is the loss\n",
    "function that we want to minimize during training. To calculate the loss\n",
    "we make a prediction using the inputs of our given data sample and\n",
    "compare it against the true data label value.\n",
    "\n",
    "Common loss functions include\n",
    "[nn.MSELoss](https://pytorch.org/docs/stable/generated/torch.nn.MSELoss.html#torch.nn.MSELoss)\n",
    "(Mean Square Error) for regression tasks, and\n",
    "[nn.NLLLoss](https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html#torch.nn.NLLLoss)\n",
    "(Negative Log Likelihood) for classification.\n",
    "[nn.CrossEntropyLoss](https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html#torch.nn.CrossEntropyLoss)\n",
    "combines `nn.LogSoftmax` and `nn.NLLLoss`.\n",
    "\n",
    "We pass our model\\'s output logits to `nn.CrossEntropyLoss`, which will\n",
    "normalize the logits and compute the prediction error.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Initialize the loss function\n",
    "loss_fn = nn.CrossEntropyLoss()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Optimizer\n",
    "\n",
    "Optimization is the process of adjusting model parameters to reduce\n",
    "model error in each training step. **Optimization algorithms** define\n",
    "how this process is performed (in this example we use Stochastic\n",
    "Gradient Descent). All optimization logic is encapsulated in the\n",
    "`optimizer` object. Here, we use the SGD optimizer; additionally, there\n",
    "are many [different\n",
    "optimizers](https://pytorch.org/docs/stable/optim.html) available in\n",
    "PyTorch such as ADAM and RMSProp, that work better for different kinds\n",
    "of models and data.\n",
    "\n",
    "We initialize the optimizer by registering the model\\'s parameters that\n",
    "need to be trained, and passing in the learning rate hyperparameter.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Inside the training loop, optimization happens in three steps:\n",
    "\n",
    "-   Call `optimizer.zero_grad()` to reset the gradients of model\n",
    "    parameters. Gradients by default add up; to prevent double-counting,\n",
    "    we explicitly zero them at each iteration.\n",
    "-   Backpropagate the prediction loss with a call to `loss.backward()`.\n",
    "    PyTorch deposits the gradients of the loss w.r.t. each parameter.\n",
    "-   Once we have our gradients, we call `optimizer.step()` to adjust the\n",
    "    parameters by the gradients collected in the backward pass.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Full Implementation {#full-impl-label}\n",
    "\n",
    "We define `train_loop` that loops over our optimization code, and\n",
    "`test_loop` that evaluates the model\\'s performance against our test\n",
    "data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def train_loop(dataloader, model, loss_fn, optimizer):\n",
    "    size = len(dataloader.dataset)\n",
    "    # Set the model to training mode - important for batch normalization and dropout layers\n",
    "    # Unnecessary in this situation but added for best practices\n",
    "    model.train()\n",
    "    for batch, (X, y) in enumerate(dataloader):\n",
    "        # Compute prediction and loss\n",
    "        pred = model(X)\n",
    "        loss = loss_fn(pred, y)\n",
    "\n",
    "        # Backpropagation\n",
    "        loss.backward()\n",
    "        optimizer.step()\n",
    "        optimizer.zero_grad()\n",
    "\n",
    "        if batch % 100 == 0:\n",
    "            loss, current = loss.item(), batch * batch_size + len(X)\n",
    "            print(f\"loss: {loss:>7f}  [{current:>5d}/{size:>5d}]\")\n",
    "\n",
    "\n",
    "def test_loop(dataloader, model, loss_fn):\n",
    "    # Set the model to evaluation mode - important for batch normalization and dropout layers\n",
    "    # Unnecessary in this situation but added for best practices\n",
    "    model.eval()\n",
    "    size = len(dataloader.dataset)\n",
    "    num_batches = len(dataloader)\n",
    "    test_loss, correct = 0, 0\n",
    "\n",
    "    # Evaluating the model with torch.no_grad() ensures that no gradients are computed during test mode\n",
    "    # also serves to reduce unnecessary gradient computations and memory usage for tensors with requires_grad=True\n",
    "    with torch.no_grad():\n",
    "        for X, y in dataloader:\n",
    "            pred = model(X)\n",
    "            test_loss += loss_fn(pred, y).item()\n",
    "            correct += (pred.argmax(1) == y).type(torch.float).sum().item()\n",
    "\n",
    "    test_loss /= num_batches\n",
    "    correct /= size\n",
    "    print(f\"Test Error: \\n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} \\n\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We initialize the loss function and optimizer, and pass it to\n",
    "`train_loop` and `test_loop`. Feel free to increase the number of epochs\n",
    "to track the model\\'s improving performance.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "loss_fn = nn.CrossEntropyLoss()\n",
    "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)\n",
    "\n",
    "epochs = 10\n",
    "for t in range(epochs):\n",
    "    print(f\"Epoch {t+1}\\n-------------------------------\")\n",
    "    train_loop(train_dataloader, model, loss_fn, optimizer)\n",
    "    test_loop(test_dataloader, model, loss_fn)\n",
    "print(\"Done!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Further Reading\n",
    "\n",
    "-   [Loss\n",
    "    Functions](https://pytorch.org/docs/stable/nn.html#loss-functions)\n",
    "-   [torch.optim](https://pytorch.org/docs/stable/optim.html)\n",
    "-   [Warmstart Training a\n",
    "    Model](https://pytorch.org/tutorials/recipes/recipes/warmstarting_model_using_parameters_from_a_different_model.html)\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
}
