3.3.2 · D1Deep Learning Frameworks

Foundations — Autograd and computational graphs in PyTorch

3,164 words14 min readBack to topic

Before you can read a single line of the parent note the Autograd note, you need a small toolbox of ideas. This page builds each one from nothing. Nothing here is assumed. If a symbol appears, it was defined a line earlier and pinned to a picture.


0. The absolute starting point: a number that "remembers"

Normally a number is just a value: 3.0. It sits there. It has no history.

The whole trick of autograd is to use a number that carries a history — it remembers where it came from and what was done to it. In PyTorch that "number-with-history" is a tensor (a container for one or more numbers) that you flag with requires_grad=True.

Figure — Autograd and computational graphs in PyTorch

Look at the picture. A plain number (left) is a lone dot — no arrows in, no arrows out. A tracked tensor (right) has an arrow pointing back to whatever operation produced it. That backward-pointing arrow is the seed of the whole computational graph.


1. Variable, function, and output — , , , and

Before derivatives we need the language of changing quantities.

The picture for a function is an input arrow → box → output arrow. This is exactly the node picture from Section 0 — a function is an operation, and its output is a new tracked number. This is why the topic calls the whole thing a graph of operations.


2. The symbols , , — operations that become nodes

The parent note writes z = x * y + x**2. Reading the left side with Section 1: is the output of feeding inputs and through three operations:

  • — multiply (in code: x * y)
  • — square, meaning (in code: x**2)
  • — add

So in symbols the output is .

Each operation is a little machine (a function of Section 1). So is really three machines feeding into each other, and is the final number that drops out the end.


3. What "a tiny nudge" means — the derivative

This is the single most important tool the topic uses, so we build it slowly.

Why do we need a rate and not just the output value? Because training a network means adjusting inputs (the weights) to lower a loss. To adjust wisely you must know which direction lowers the loss and by how much per step. That is precisely a derivative.

Figure — Autograd and computational graphs in PyTorch

Look at the curve in the figure. Pick the point at . Draw a tiny step of width to the right (the pale-yellow segment). The output climbs by the pink segment. The slope of the straight line connecting the two points is . As shrinks toward zero, that line becomes the blue tangent — the true instantaneous rate. That limiting slope is the derivative.

Let us earn the parent note's result by hand:

What we did: expanded , cancelled , cancelled one top-and-bottom, then let so the leftover vanished. Why: this is the only honest definition of "instantaneous rate," and it is exactly the rule autograd applies internally for every squaring operation.


4. Many inputs at once — the partial derivative

The output has two inputs, and . "Nudge the input" is now ambiguous — nudge which one?

The picture: on a hilly surface with two floor-directions and , is the steepness you feel if you walk purely in the -direction, ignoring .

Because is frozen, treat it like a plain constant:


5. Chaining machines — the chain rule

Section 2 said operations feed into each other. Suppose the output is built in two stages: an inner machine turns into an in-between value we also call , then an outer machine turns that into the final output . In symbols , where names both the inner machine and the number it produces. How does a nudge in reach ?

Figure — Autograd and computational graphs in PyTorch

Why the topic lives on this: the backward pass is nothing but the chain rule applied step by step through the graph, multiplying the little rate stored at each node.

When reaches by two different paths (in , the value flows into both the node and the node), the two ripples add:


6. Direction of travel — the DAG and "reverse order"

The parent calls the graph a DAG. Two new words:


7. The vector / matrix symbols — , , and

The parent's Example 1 uses . Heads-up on naming: here is the output of the layer — it plays the role played in Sections 2–5. We keep the parent's letter , but remember is now "the number the machine produces," not an input. Unpack the rest of the alphabet:

Now the weight gradient. Let be the row of rates of the loss with respect to the layer's output (Section 8 defines ). Since , matrix calculus gives Shape check: is and is , so their product is — exactly the shape of . That shape agreement is how you know the formula is put together correctly. (The parent note wrote this with for the pre-activation output; it is the same output-role letter, just renamed.)

You will meet these live when you build layers: 3.4.01-Building-neural-networks-with-torch.nn.Module.


8. The loss and the summary symbol

Now the subtle point the parent raises. A batch of samples has a batch loss. A common choice is the average:


9. The autograd nouns you'll type

Now the code words in the parent are just labels on the ideas above:

Symbol / word Plain meaning Which idea it is
requires_grad=True "track this number's history" the remembering tensor (§0)
grad_fn "the operation that made me" the backward arrow / node (§0, §2)
.backward() "run the chain rule backwards now" §5 + §6
.grad "here is (me)" a partial derivative (§4)
.zero_() "wipe the accumulated sum" reset the raw .grad sum (§8)

These reappear the moment you touch 4.2.03-Batch-normalization, 5.3.01-Transfer-learning, and memory tricks like 6.4.02-Gradient-checkpointing — so lock them in.


Prerequisite map

The picture below shows how each foundation feeds the next, ending at the autograd machinery.

Tensor a box of numbers

Tracked tensor remembers history

Function a machine

Operations become graph nodes

Derivative rate of change

Partial derivative one input at a time

Chain rule multiply along a path

Computational graph a DAG

Vectors and matrices

Layer output y equals Wx plus b

Backward pass gradients

Loss a single score

Autograd in PyTorch

Read it top-to-bottom: raw tensors and functions become graph nodes; derivatives and the chain rule power the backward walk; vectors and matrices supply the layer shapes; a scalar loss seeds the whole thing — and all of it lands in Autograd in PyTorch.


Equipment checklist

Cover the right side and answer out loud. If any stalls you, reread that section before opening the parent note.

What does requires_grad=True turn a plain number into?
A tensor that remembers its history — it carries a backward arrow to the operation that made it.
In this page, what role do and play versus ?
are inputs; is the number an operation outputs. (In Section 7 the layer's output is renamed .)
In one sentence, what is a derivative?
The rate at which the output moves per tiny nudge of the input — .
Why do we use the curly instead of ?
The function has several inputs; means "nudge only this one, freeze the rest."
Compute for .
.
Along a chain of machines, do rates multiply or add? And across two separate paths into a node?
Multiply along one path; add across separate paths.
What does DAG stand for and why does "acyclic" matter?
Directed Acyclic Graph; no loops guarantees a clean back-to-front order for backpropagation.
Why must .backward() be called on a scalar loss?
The chain rule needs one final number to seed the ripples; a single loss gives that seed.
Where does the in an averaged batch loss actually come from?
From the loss expression you write (e.g. .mean()); PyTorch does not add it during .backward().
What does .grad accumulation do, and what must you do because of it?
Each .backward() adds into .grad (no averaging); you must call .zero_() between iterations.
Why does have the right shape?
is and is , giving a result — the shape of .