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

This tutorial covers the following topics:

  • Introduction to linear regression and gradient descent
  • Implementing a linear regression model using PyTorch tensors
  • Training a linear regression model using the gradient descent algorithm
  • Implementing gradient descent and linear regression using PyTorch built-in

How to run the code

This tutorial is an executable Jupyter notebook hosted on Jovian (don't worry if these terms seem unfamiliar; we'll learn more about them soon). You can run this tutorial and experiment with the code examples in a couple of ways: using free online resources (recommended) or on your computer.

Option 1: Running using free online resources (1-click, recommended)

The easiest way to start executing the code is to click the Run button at the top of this page and select Run on Colab. Google Colab is a free online platform for running Jupyter notebooks using Google's cloud infrastructure. You can also select "Run on Binder" or "Run on Kaggle" if you face issues running the notebook on Google Colab.

Option 2: Running on your computer locally

To run the code on your computer locally, you'll need to set up Python, download the notebook and install the required libraries. We recommend using the Conda distribution of Python. Click the Run button at the top of this page, select the Run Locally option, and follow the instructions.

Jupyter Notebooks: This tutorial is a Jupyter notebook - a document made of cells. Each cell can contain code written in Python or explanations in plain English. You can execute code cells and view the results, e.g., numbers, messages, graphs, tables, files, etc. instantly within the notebook. Jupyter is a powerful platform for experimentation and analysis. Don't be afraid to mess around with the code & break things - you'll learn a lot by encountering and fixing errors. You can use the "Kernel > Restart & Clear Output" or "Edit > Clear Outputs" menu option to clear all outputs and start again from the top.

Before we begin, we need to install the required libraries. The installation of PyTorch may differ based on your operating system / cloud environment. You can find detailed installation instructions here: https://pytorch.org .

# Uncomment and run the appropriate command for your operating system, if required

# Linux / Binder
# !pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# Windows
!pip install numpy torch==1.7.0+cpu torchvision==0.8.1+cpu torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

# MacOS
# !pip install numpy torch torchvision torchaudio
Looking in links: https://download.pytorch.org/whl/torch_stable.html Requirement already satisfied: numpy in c:\users\aryaman bansal\anaconda3\lib\site-packages (1.19.1) Collecting torch==1.7.0+cpu Downloading https://download.pytorch.org/whl/cpu/torch-1.7.0%2Bcpu-cp37-cp37m-win_amd64.whl (184.2 MB) Collecting torchvision==0.8.1+cpu Downloading https://download.pytorch.org/whl/cpu/torchvision-0.8.1%2Bcpu-cp37-cp37m-win_amd64.whl (808 kB) Collecting torchaudio==0.7.0 Downloading https://download.pytorch.org/whl/torchaudio-0.7.0-cp37-none-win_amd64.whl (103 kB) Requirement already satisfied: future in c:\users\aryaman bansal\anaconda3\lib\site-packages (from torch==1.7.0+cpu) (0.18.2) Requirement already satisfied: typing-extensions in c:\users\aryaman bansal\anaconda3\lib\site-packages (from torch==1.7.0+cpu) (3.7.4.3) Collecting dataclasses Downloading dataclasses-0.6-py3-none-any.whl (14 kB) Requirement already satisfied: pillow>=4.1.1 in c:\users\aryaman bansal\anaconda3\lib\site-packages (from torchvision==0.8.1+cpu) (7.2.0) Installing collected packages: dataclasses, torch, torchvision, torchaudio Attempting uninstall: torch Found existing installation: torch 1.8.1 Uninstalling torch-1.8.1: Successfully uninstalled torch-1.8.1 Successfully installed dataclasses-0.6 torch-1.7.0+cpu torchaudio-0.7.0 torchvision-0.8.1+cpu

Introduction to Linear Regression

In this tutorial, we'll discuss one of the foundational algorithms in machine learning: Linear regression. We'll create a model that predicts crop yields for apples and oranges (target variables) by looking at the average temperature, rainfall, and humidity (input variables or features) in a region. Here's the training data:

linear-regression-training-data

In a linear regression model, each target variable is estimated to be a weighted sum of the input variables, offset by some constant, known as a bias :

yield_apple  = w11 * temp + w12 * rainfall + w13 * humidity + b1
yield_orange = w21 * temp + w22 * rainfall + w23 * humidity + b2

Visually, it means that the yield of apples is a linear or planar function of temperature, rainfall and humidity:

linear-regression-graph