Deep Learning with PyTorch

Build, train, debug and ship neural networks in PyTorch: tensors and autograd, nn.Module, DataLoaders and the training loop, regularisation, checkpoints and export.

Intermediate · Machine Learning · about 15 hours · 5 modules · certificate on completion

Taught by Élèna Petrovà, Zawadì Mutembò, AI Learning Companions.

About this course

PyTorch is where most deep learning research is written and a common choice in production, and its appeal is that a model is ordinary Python: tensors you can inspect, a graph built as the code runs, and a training loop you write yourself. That freedom comes with responsibilities that higher-level libraries hide, such as zeroing gradients, switching between training and evaluation mode, and saving the right state, and this course teaches them precisely, from the tensor up.

You'll start with tensors, broadcasting, views and autograd, checking gradients against formulas and writing a custom backward pass. Then you'll build models as nn.Module classes with buffers and deliberate initialisation, feed them through your own Dataset and DataLoader, and write reusable training and evaluation loops with AdamW, parameter groups and learning-rate schedules. You'll regularise an overfitting network, debug with hooks and anomaly detection, resume a checkpointed run bit for bit, and export a model with torch.export. The capstone takes a synthetic pump-monitoring dataset from raw CSV to an exported model, compared honestly with scikit-learn. It's the PyTorch companion to Training Deep Neural Networks, and the foundation for Computer Vision with Convolutional Neural Networks, Reinforcement Learning: From Bandits to Policy Gradients and Fine-Tuning Open Language Models with LoRA.

What you'll learn

  • Work with tensors' shapes, dtypes, devices and memory, and use autograd to compute, check and customise gradients
  • Build models as nn.Module classes whose parameters, buffers and state_dict hold exactly what they should
  • Feed data through Dataset and DataLoader, and write training and evaluation loops with optimisers and learning-rate schedules
  • Control overfitting with dropout, weight decay and early stopping, and find silent bugs with hooks and anomaly detection
  • Checkpoint and resume training exactly, load files safely, and export a trained model with torch.export

Skills you'll gain

PyTorch, Tensors and autograd, nn.Module design, Dataset and DataLoader, Training and evaluation loops, Optimisers and learning-rate schedules, Regularisation and early stopping, Debugging neural networks, Checkpointing and model export

Tools

Python 3, PyTorch, NumPy, scikit-learn, pandas

Before you start

Machine Learning Foundations with Python (or experience of training models with scikit-learn), and how a neural network learns, as in Introduction to Neural Networks and TensorFlow. Comfortable Python and NumPy. Install Python 3.10 or later, PyTorch 2.6 or later (pip install torch, or the selector at pytorch.org; the CPU build is enough) and pip install scikit-learn pandas. Every lab runs on a laptop CPU in seconds or minutes.

Modules

  1. Tensors and automatic differentiation

    Work with tensors the way PyTorch stores them, with shapes, dtypes, devices and memory layout, then use autograd to compute gradients, check them and write your own.

    • Tensors: shapes, dtypes, devices and memory (12 min)
    • Autograd: the graph behind every operation (12 min)
    • Gradient descent by hand, and custom gradients (12 min)
    • Hands-on lab: Fit a model with raw autograd, and check its gradients
  2. Models with nn.Module

    Package layers, parameters and state into nn.Module classes, choose and initialise layers deliberately, and give each loss function exactly the inputs it expects.

    • nn.Module: parameters, buffers and the state_dict (12 min)
    • Layers, containers and initialisation (12 min)
    • Loss functions and the inputs they expect (12 min)
    • Hands-on lab: Build a configurable MLP and test what it registers
  3. Data and the training loop

    Feed batches through Dataset and DataLoader, write training and evaluation loops you can reuse, and configure optimisers and learning-rate schedules with torch.optim.

    • Dataset and DataLoader (12 min)
    • The training and evaluation loops (12 min)
    • Optimisers, parameter groups and learning-rate schedules (12 min)
    • Hands-on lab: Train a digit classifier end to end
  4. Regularisation and debugging in PyTorch

    Make PyTorch's training and evaluation behaviour work for you, with dropout, weight decay, early stopping and normalisation layers, then find the bugs that don't raise errors.

    • Dropout, weight decay and early stopping in practice (12 min)
    • Normalisation layers in PyTorch (10 min)
    • Hooks, anomaly detection and silent bugs (12 min)
    • Hands-on lab: Rein in an overfitting network
  5. Saving, resuming and shipping models

    Save and resume training without losing a step, load checkpoints safely, write device-agnostic and reproducible code, and export a model for inference with torch.export.

    • state_dicts, checkpoints and safe loading (12 min)
    • Devices, mixed precision and reproducibility (12 min)
    • Exporting a model for inference (10 min)
    • Hands-on lab: Resume training exactly, then export

Project: Train and ship a pump-fault classifier

make_data.py generates a synthetic maintenance dataset: hourly sensor summaries from water pumps (temperature, ambient temperature, vibration, pressure, flow, motor current, speed and age), each labelled with what an inspection found: normal, bearing_wear, overheating or cavitation. It writes train.csv (6,000 rows) and test.csv (2,000 rows); about 2% of the ambient readings are missing, and the classes are imbalanced, with normal about 57% of the rows. Build the whole PyTorch workflow: a Dataset that imputes and standardises with statistics from the training rows only, a model that carries those statistics as buffers, a seeded training loop with a validation split, a learning-rate schedule and early stopping, checkpoints you can resume from exactly, and an exported model with a small prediction script. Compare it with scikit-learn's LogisticRegression on the same split, and use test.csv once, at the end. For calibration, on the course's machine logistic regression reached a macro F1 of about 0.79 on test.csv, HistGradientBoostingClassifier about 0.84, and an MLP with two hidden layers of 128 ReLU units, AdamW and early stopping about 0.84.

Part of

The products this course names are trademarks of their owners, who haven't made or endorsed it.

Start the course free