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

PyTorch Basics: Tensors & Gradients

Part 1 of "Pytorch: Zero to GANs"

This post is the first in a series of tutorials on building deep learning models with PyTorch, an open source neural networks library developed and maintained by Facebook. 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

This series attempts to make PyTorch a bit more approachable for people starting out with deep learning and neural networks. In this notebook, we’ll cover the basic building blocks of PyTorch models: tensors and gradients.

System setup

This tutorial takes a code-first approach towards learning PyTorch, 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 Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks.

NOTE: If you're running this notebook on Binder, please skip ahead to the next section.

Running on your computer locally

We'll use the Anaconda distribution of Python to install libraries and manage virtual environments. For interactive coding and experimentation, we'll use Jupyter notebooks. All the tutorials in this series are available as Jupyter notebooks hosted on Jovian.ml: a sharing and collaboration platform for Jupyter notebooks & machine learning experiments.

Jovian.ml makes it easy to share Jupyter notebooks on the cloud by running a single command directly within Jupyter. It also captures the Python environment and libraries required to run your notebook, so anyone (including you) can reproduce your work.

Here's what you need to do to get started:

  1. Install Anaconda by following the instructions given here. You might also need to add Anaconda binaries to your system PATH to be able to run the conda command line tool.

  2. Install the jovian Python library by the running the following command (without the $) on your Mac/Linux terminal or Windows command prompt:

$ pip install jovian --upgrade
  1. Download the notebook for this tutorial using the jovian clone command:
$ jovian clone aakashns/01-pytorch-basics

(You can copy this command to clipboard by clicking the 'Clone' button at the top of this page on Jovian.ml)

Running the clone command creates a directory 01-pytorch-basics containing a Jupyter notebook and an Anaconda environment file.

$ ls 01-pytorch-basics
01-pytorch-basics.ipynb  environment.yml
  1. Now we can enter the directory and install the required Python libraries (Jupyter, PyTorch etc.) with a single command using jovian:
$ cd 01-pytorch-basics
$ jovian install

jovian install reads the environment.yml file, identifies the right dependencies for your operating system, creates a virtual environment with the given name (01-pytorch-basics by default) and installs all the required libraries inside the environment, to avoid modifying your system-wide installation of Python. It uses conda internally. If you face issues with jovian install, try running conda env update instead.

  1. We can activate the virtual environment by running
$ conda activate 01-pytorch-basics

For older installations of conda, you might need to run the command: source activate 01-pytorch-basics.

  1. Once the virtual environment is active, we can start Jupyter by running
$ jupyter notebook
  1. You can now access Jupyter's web interface by clicking the link that shows up on the terminal or by visiting http://localhost:8888 on your browser. At this point, you can click on the notebook 01-pytorch-basics.ipynb to open it and run the code. If you want to type out the code yourself, you can also create a new notebook using the 'New' button.

We begin by importing PyTorch:

# Uncomment the command below if PyTorch is not installed
# !conda install pytorch cpuonly -c pytorch -y
Collecting package metadata (current_repodata.json): done Solving environment: done ==> WARNING: A newer version of conda exists. <== current version: 4.8.2 latest version: 4.8.3 Please update conda by running $ conda update -n base conda # All requested packages already installed.
import torch