{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# For tips on running notebooks in Google Colab, see\n# https://pytorch.org/tutorials/beginner/colab\n%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Training a Classifier\n=====================\n\nThis is it. You have seen how to define neural networks, compute loss\nand make updates to the weights of the network.\n\nNow you might be thinking,\n\nWhat about data?\n----------------\n\nGenerally, when you have to deal with image, text, audio or video data,\nyou can use standard python packages that load data into a numpy array.\nThen you can convert this array into a `torch.*Tensor`.\n\n- For images, packages such as Pillow, OpenCV are useful\n- For audio, packages such as scipy and librosa\n- For text, either raw Python or Cython based loading, or NLTK and\n SpaCy are useful\n\nSpecifically for vision, we have created a package called `torchvision`,\nthat has data loaders for common datasets such as ImageNet, CIFAR10,\nMNIST, etc. and data transformers for images, viz.,\n`torchvision.datasets` and `torch.utils.data.DataLoader`.\n\nThis provides a huge convenience and avoids writing boilerplate code.\n\nFor this tutorial, we will use the CIFAR10 dataset. It has the classes:\n'airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse',\n'ship', 'truck'. The images in CIFAR-10 are of size 3x32x32, i.e.\n3-channel color images of 32x32 pixels in size.\n\n\n\nTraining an image classifier\n----------------------------\n\nWe will do the following steps in order:\n\n1. Load and normalize the CIFAR10 training and test datasets using\n `torchvision`\n2. Define a Convolutional Neural Network\n3. Define a loss function\n4. Train the network on the training data\n5. Test the network on the test data\n\n### 1. Load and normalize CIFAR10\n\nUsing `torchvision`, it's extremely easy to load CIFAR10.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import torch\nimport torchvision\nimport torchvision.transforms as transforms" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The output of torchvision datasets are PILImage images of range \\[0,\n1\\]. We transform them to Tensors of normalized range \\[-1, 1\\].\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "```{=html}\n
If running on Windows and you get a BrokenPipeError, try settingthe num_worker of torch.utils.data.DataLoader() to 0.
\n```\n```{=html}\n