---
title: "How a network learns"
lesson_id: "01"
---

# How a network learns

- **Lesson ID:** 01
- **Goal:** Teach a tiny scalar autograd engine, check it against micrograd's published numbers, and set up `uv` with PyTorch on MPS or CPU.
- **Human lesson:** [01-how-a-network-learns.html](01-how-a-network-learns.html)

## Prerequisites

- Solid Python. A vague memory of derivatives is enough.

## Inputs, outputs, and artifacts

- **Inputs:** The habit of calling an LLM without knowing how a net learns.
- **Outputs:** A working `Value` engine and a device check for MPS, CUDA, or CPU.
- **Artifacts:** `engine.py` that prints 24.7041, 138.8338, and 645.5773. Three small progressive mermaid diagrams of the graph and the backward walk.

## Agent build steps

1. Define autograd as automatic gradients: a graph on the forward pass, a backward walk that fills slopes.
2. Point at [micrograd](https://github.com/karpathy/micrograd). Quote the README's "about 100 and 50 lines" and the current counts (94 and 60).
3. Put environment setup first: Python 3.11+, `uv init`, `uv venv`, `uv add torch pytest`, and a printout of MPS and CUDA availability. Link the [PyTorch MPS notes](https://pytorch.org/docs/stable/notes/mps.html).
4. Build a `Value` type with add, mul, pow, ReLU, and a topological `backward`.
5. Run the micrograd README example and match 24.7041, 138.8338, and 645.5773.
6. Explain the local rules and why shared values add their gradients.
7. Sketch the thin neural net layer on top, and point at `demo.ipynb` without requiring it.
8. State Hao's gate: rebuild the engine without looking before the tokenizer lesson.
9. Use three progressive mermaid diagrams, not one mega diagram.

## Constraints

Keep the language simple, friendly, and literal. No quizzes, no em dashes, no invented WWDC sessions. Brand Edge FDE only. Do not paste the whole micrograd repo. The teaching engine must be original and must match the published numbers.

## Key concepts

- Autograd records a graph on the forward pass and walks it backward to compute gradients.
- micrograd does this on scalars, in about 100 lines of engine code and about 50 lines of neural net code, as its README states.
- A topological walk makes sure each node applies its local backward rule after its children.
- Addition, multiplication, power, and ReLU each have a short local gradient rule. Shared values add their gradients.
- A neuron, a layer, and a multi-layer net can sit on top of the same Value type.

## Takeaways

- Run the engine and match the README numbers: 24.7041, 138.8338, and 645.5773.
- Rebuild the engine without looking before you move to the tokenizer lesson.
- Keep the official repo nearby: [github.com/karpathy/micrograd](https://github.com/karpathy/micrograd).

## Acceptance checks

- Environment setup with `uv` and the MPS-or-CPU device check appears before the runnable engine.
- The published micrograd numbers are used as the check.
- Agent brief link is present.
- [ ] Proceed to [lesson 02 brief](02-eight-lectures-to-gpt.llms.md).
