Learn practical skills, build real-world projects, and advance your career

Training Deep Neural Networks on a GPU with PyTorch

Part 4 of "PyTorch: Zero to GANs"

This notebook is the fourth in a series of tutorials on building deep learning models with PyTorch, an open source neural networks library. Check out the full series:

  1. PyTorch Basics: Tensors & Gradients
  2. Linear Regression & Gradient Descent
  3. Image Classfication using Logistic Regression
  4. Training Deep Neural Networks on a GPU
  5. Image Classification using Convolutional Neural Networks
  6. Data Augmentation, Regularization and ResNets
  7. Generating Images using Generative Adverserial Networks

In the previous tutorial, we trained a logistic regression model to identify handwritten digits from the MNIST dataset with an accuracy of around 86%.

alt

However, we also noticed that it's quite difficult to improve the accuracy beyond 87%, due to the limited power of the model. In this post, we'll try to improve upon it using a feedforward neural network.

System Setup

This tutorial takes a code-first approach, and you should try to follow along by running and experimenting with the code yourself. The easiest way to start executing this notebook is to click the "Run" button at the top of this page, and select "Run on Kaggle". This will run the notebook on Kaggle, a free online service for running Jupyter notebooks (you might need to create an account).

Running on your computer locally

(Skip this if you are running on Kaggle) You can clone this notebook, install the required dependencies using conda, and start Jupyter by running the following commands on the terminal:

pip install jovian --upgrade                # Install the jovian library 
jovian clone aakashns/04-feedforward-nn     # Download notebook
cd 04-feedforward-nn                        # Enter the created directory 
conda create -n 04-feedfoward-nn python=3.8 # Create a conda environment
conda activate 04-feedforward-nn            # Activate virtual environment
conda install jupyter                       # Install Jupyter
jupyter notebook                            # Start Jupyter

On older versions of conda, you might need to run source activate 04-feedfoward-nn to activate the virtual environment. For a more detailed explanation of the above steps, check out the System setup section in the first notebook.

Preparing the Data

The data preparation is identical to the previous tutorial. We begin by importing the required modules & classes.

# Uncomment and run the commands below if imports fail
!conda install numpy pandas pytorch torchvision cpuonly -c pytorch -y
!pip install matplotlib --upgrade --quiet
Collecting package metadata (current_repodata.json): - ^C / ^C Traceback (most recent call last): File "/opt/conda/bin/pip", line 5, in <module> from pip._internal.cli.main import main File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/main.py", line 10, in <module> from pip._internal.cli.autocompletion import autocomplete File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module> from pip._internal.cli.main_parser import create_main_parser File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module> from pip._internal.cli import cmdoptions File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/cmdoptions.py", line 24, in <module> from pip._internal.cli.progress_bars import BAR_TYPES File "/opt/conda/lib/python3.7/site-packages/pip/_internal/cli/progress_bars.py", line 12, in <module> from pip._internal.utils.logging import get_indentation File "/opt/conda/lib/python3.7/site-packages/pip/_internal/utils/logging.py", line 18, in <module> from pip._internal.utils.misc import ensure_dir File "/opt/conda/lib/python3.7/site-packages/pip/_internal/utils/misc.py", line 20, in <module> from pip._vendor import pkg_resources File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3251, in <module> @_call_aside File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3235, in _call_aside f(*args, **kwargs) File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 3264, in _initialize_master_working_set working_set = WorkingSet._build_master() File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 574, in _build_master ws = cls() File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 567, in __init__ self.add_entry(entry) File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 623, in add_entry for dist in find_distributions(entry, True): File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2065, in find_on_path for dist in factory(fullpath): File "/opt/conda/lib/python3.7/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2126, in distributions_from_metadata if os.path.isdir(path): File "/opt/conda/lib/python3.7/genericpath.py", line 45, in isdir return stat.S_ISDIR(st.st_mode) KeyboardInterrupt
import torch
import torchvision
import numpy as np
import matplotlib.pyplot as plt
import torch.nn as nn
import torch.nn.functional as F
from torchvision.datasets import MNIST
from torchvision.transforms import ToTensor
from torchvision.utils import make_grid
from torch.utils.data.dataloader import DataLoader
from torch.utils.data import random_split
%matplotlib inline