pytorch / pytorch/tutorials

💡 [REQUEST] - Tutorial: Visualizing the Mathematics Behind Neural Network Training

Open
#3,907 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
9.3k
Forks
4.4k
Avg merge
1d 21h
Merged PRs (30d)
4

Description

🚀 Describe the improvement or the new tutorial

A beginner-friendly tutorial that traces the mathematical operations inside a PyTorch training loop — connecting linear algebra to torch.nn.Linear, the chain rule to torch.autograd.backward(), and gradient descent to torch.optim — with runnable code that makes each step observable and verifiable.

Why this is needed:
Many learners use autograd as a black box. Existing tutorials explain the API (how to call .backward(), how to access .grad) but do not trace what is happening mathematically at each step. This tutorial bridges that gap by making the math visible through code:

  1. Print a weight matrix, verify it matches x @ W.T + b
  2. Call .backward(), then manually verify the gradient using the chain rule
  3. Walk the grad_fn graph to see the operations autograd recorded
  4. Compare parameters before and after optimizer.step() to see gradient descent in action

Proposed structure:

  1. Linear Algebra — what nn.Linear computes, verified with manual matrix multiplication
  2. Activation Functions — why nonlinearity matters, visualizing ReLU gradients
  3. The Chain Rule — what backward() computes, manually verifying gradients
  4. The Computational Graph — inspecting grad_fn to trace operations
  5. Gradient Descent — what optimizer.step() does, before/after comparison
  6. Putting It Together — forward, backward, step mapped to three branches of mathematics

This approach has been validated in practice: I presented this material at Grace Hopper Celebration 2025 ("The Math Behind the Magic: Understanding the Role of Mathematics in Deep Learning"). Talk materials: https://github.com/AddyM/Math_behind_ML

Difficulty level: Beginner
Target location: beginner_source/

I am happy to write and submit this tutorial as a PR.

Existing tutorials on this topic
  1. autogradqs_tutorial.py — covers autograd API usage (requires_grad, backward, grad). Focuses on how to use autograd, not what it computes mathematically.
    https://pytorch.org/tutorials/beginner/basics/autogradqs_tutorial.html

  2. autograd_tutorial.py — conceptual overview of automatic differentiation and computational graphs. Does not include manual verification of gradients or tracing the chain rule step by step.
    https://pytorch.org/tutorials/beginner/blitz/autograd_tutorial.html

  3. buildmodel_tutorial.py — covers nn.Module and model building. Does not connect layer operations to their underlying linear algebra.
    https://pytorch.org/tutorials/beginner/basics/buildmodel_tutorial.html

Additional context

PyTorch's eager execution model makes the mathematics of deep learning observable in a way static graph frameworks do not allow. This tutorial leans into that strength — every intermediate result is printable, every gradient is inspectable, every operation in the computational graph is traceable.
No GPU required — the entire tutorial runs on CPU, making it accessible to all learners.
References:

GHC 2025 talk materials: https://github.com/AddyM/Math_behind_ML
PyTorch Discuss thread on this approach: https://discuss.pytorch.org/ (search: "Building a PyTorch curriculum that teaches math through code")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in beginner_source/ and compare autogradqs_tutorial.py, autograd_tutorial.py, and buildmodel_tutorial.py to identify the remaining instructional gap. Build a CPU-runnable tutorial covering the six proposed sections, including observable matrix operations, gradients, grad_fn, and optimizer changes. Done means the mathematical steps are traceable and verifiable in code.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
documentation, machine-learning
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.