手写自动微分引擎:造你的迷你 PyTorch

Build an Autograd Engine: Your Own Mini-PyTorch

Write every line behind loss.backward() yourself — PyTorch stops being a black box

6 labs9 AI-mentored sessions~5 hoursBilingual · EN / 中
Start learning →

About this course

You call loss.backward() every day — but what actually happens inside that one line? In this
course you don't memorize concepts, you build the thing: first a Value class that remembers
how it was computed, so gradients flow backwards through the graph automatically; then a
finite-difference audit that proves, op by op, that your backprop is correct. Next you upgrade
the scalar engine into a tensor engine that eats numpy arrays — backprop through broadcasting
and the two transpose rules of matmul, no hand-waving. On top of it you build your own torch.nn
and torch.optim: Module, Linear, numerically-stable cross-entropy, SGD and Adam. Finally you use
this mini-PyTorch — every line yours — to train XOR, a two-moons classifier and a character-level
name generator, and map it all back to real PyTorch: detach, no_grad, and the memory bill.
Pure Python + numpy, fully offline, every step runs in your own sandbox in under a second.
By the end you'll see it plainly: the heart of PyTorch is a few dozen lines you wrote and verified yourself.

What you'll learn

  • Hand-implement a graph-recording Value class and a topological-sort backward() — every line behind loss.backward()
  • Audit analytic gradients against finite differences op by op, proving your backprop correct and catching a planted derivative bug
  • Upgrade the scalar engine into a numpy tensor autograd: derive and implement backprop through broadcasting and matmul's two transpose rules
  • Build your own torch.nn and torch.optim: Module/Linear/stable cross-entropy/SGD/momentum/Adam
  • Train XOR, a two-moons classifier and a character-level name generator from scratch on your own engine, with deterministic assertions
  • Explain real PyTorch's internals: why reverse mode wins, the activation-memory bill and checkpointing, what detach/no_grad do

Syllabus

1A Backprop Engine: from Scalar Autograd to Training an MLP3 sessions
  • 1The Heart of the Graph: a Value That Remembers How It Was Made30 minStart →
  • 2Backprop for Real: Topological backward() and the Numerical Gradient Audit30 minStart →
  • 3Train an MLP on XOR with Your Own Engine30 minStart →
2The Op Zoo & Gradient Audits: Hardening Your Engine3 sessions
  • 1Growing the Op Zoo: Local Derivatives for exp, log, pow & ReLU30 minStart →
  • 2The Gradient Auditor: a grad_check That Catches Real Bugs30 minStart →
  • 3Two Ways Gradients Lie: Accumulation and zero_grad30 minStart →
3Tensor Autograd: Teaching Your Engine numpy3 sessions
  • 1From Scalars to Tensors: Teaching Your Engine numpy30 minStart →
  • 2Backprop Through Broadcasting: Shrinking Gradients Back to Shape30 minStart →
  • 3Backprop Through matmul: Two Transposes That Power Deep Learning30 minStart →
4Build Your Own torch.nn & torch.optim0 sessions

Sessions are on the way.

5Capstone: Train Real Models on Your Mini-PyTorch0 sessions

Sessions are on the way.

6Under the Hood: Your Engine vs Real PyTorch0 sessions

Sessions are on the way.

Same series · 人工智能全透析

AI, End to End

Synthesized from the best material across the web — rebuild the core of AI from scratch, the efficient way.