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

Image Classification using Logistic Regression in PyTorch

Part 3 of "PyTorch: Zero to GANs"

This post is the third 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. Classification using Logistic Regression (this post)
  4. Coming soon.. (neural networks, CNNs, RNNs, GANs etc.)

In this tutorial, we'll use our existing knowledge of PyTorch and linear regression to solve a very different kind of problem: image classification. We'll use the famous MNIST Handwritten Digits Database as our training dataset. It consists of 28px by 28px grayscale images of handwritten digits (0 to 9), along with labels for each image indicating which digit it represents. Here are some sample images from the dataset:

mnist-sample

System setup

If you want to follow along and run the code as you read, 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 <notebook_id> # Download notebook & dependencies cd 03-logistic-regression # Enter the created directory conda env update # Install the dependencies conda activate 03-logistic-regression # Activate virtual env jupyter notebook # Start Jupyter

You can find the notebook_id by cliking the Clone button at the top of this page on Jovian. On older versions of conda, you might need to run source activate 03-logistic-regression to activate the environment. For a more detailed explanation of the above steps, check out the System setup section in the first notebook.

Exploring the Data

We begin by importing torch and torchvision. torchvision contains some utilities for working with image data. It also contains helper classes to automatically download and import popular datasets like MNIST.