Skip to main content

Lab 1: Linear regression

Linear regression from first principles to PyTorch

Overview

In this 90-minute lab, you will train your first machine-learning model and solve one regression problem three ways: with NumPy analytical gradients, with native PyTorch autograd, and with mini-batches provided by a custom Dataset and DataLoader.

You will implement eight TODO(student) tasks, compare numerical differentiation, analytical gradients, and autograd, inspect visible training progress, and collect all three solutions in a final comparison table.

Learning objectives

By the end of the lab, you should be able to:

  • identify the data, model, predictions, loss, optimization, and evaluation in a basic ML task;
  • implement mean squared error, numerical gradients, and analytical gradients;
  • distinguish finite differences from PyTorch automatic differentiation;
  • explain predict → loss → gradients → update;
  • train the same linear model with NumPy and PyTorch;
  • use a custom PyTorch Dataset and a DataLoader;
  • keep training and validation data separate;
  • interpret progress logs, learning curves, validation metrics, and residuals;
  • compare three implementations without confusing implementation details with the underlying model.

Start the lab

Download the student notebook

To work in Google Colab:

  1. Download the notebook using the button above.
  2. Open Google Colab.
  3. Choose File → Upload notebook and select 01-linear-regression.ipynb.
  4. Save your own copy before editing.
  5. Run the notebook from the top and complete the eight tasks in order.

The notebook uses packages already available in Colab. A collapsible “help-me mode” prompt shows how to use a chat assistant as a tutor without requesting complete solutions.

Submission checklist

  • Complete all eight TODO(student) tasks.
  • Restart the runtime and verify that every cell runs in order.
  • Confirm that all assertions pass, selected-epoch progress is printed, and the requested plots appear.
  • Confirm that the final table compares NumPy, full-batch PyTorch, and mini-batch PyTorch.
  • Answer all seven interpretation questions in your own words.
  • Submit the completed notebook through the course submission channel specified by the instructor.