3.1.10 · D3Neural Network Fundamentals

Worked examples — Computational graphs and autograd

2,805 words13 min readBack to topic

We first draw the scenario matrix: the complete list of structural situations a computational graph can throw at you. Then every example is tagged with the cell it covers.


The scenario matrix

Every computational-graph problem is some combination of these structural situations. A "cell" is one row here.

# Cell (structural situation) Why it's tricky Covered by
A Single path input → output The plain chain rule, no summing Ex 1
B Fan-out (one node feeds ≥2 children) Must accumulate () adjoints Ex 2
C Dead branch (a node not on any path to output) Its adjoint must be 0 Ex 3
D Multiplication node with equal inputs () Both edges carry gradient, easy to halve by mistake Ex 4
E Zero / degenerate input killing a gradient A factor becomes , gradient vanishes Ex 5
F Full neuron chain (loss → sigmoid → linear) Real backprop, several primitives stacked Ex 6
G Broadcast / vector sum (one input, many outputs summed) Adjoints from every output add Ex 7
H Limiting behaviour (saturated sigmoid, ) Gradient , "vanishing gradient" Ex 8
I Word problem (real-world sensitivity) Translate story → graph → adjoint Ex 9
J Exam twist (branch that cancels: ) Adjoints from two paths cancel to Ex 10

We reuse the adjoint rule throughout:

Below, the small diagram shows the reading direction: values flow right (forward), blame flows left (backward).

forward

forward

backward

backward

input x

operations

output f


Example 1 — Single path (Cell A)

Forecast: guess first — is bigger or smaller than ? Write your guess.

  1. Forward, store values. ; ; . Why this step? Local derivatives need these numbers (e.g. needs ).
  2. Seed . Why? by definition.
  3. Edge : . Why? so the local slope is .
  4. Edge : . Why? , evaluated at the stored .
  5. Edge : . Why? is linear, slope .

Answer: .

Verify: analytically , so , at gives . ✓ (Single path = one product of slopes, no summing needed.)


Example 2 — Fan-out, must accumulate (Cell B)

Forecast: how many paths does take to reach ? Guess the count before continuing.

Figure — Computational graphs and autograd

Look at the figure: the amber node splits into two cyan edges — one to , one to . That split is the fan-out. Both branches must contribute blame.

  1. Forward. ; ; . Why? Store — the product's local derivatives are the other factor.
  2. Seed .
  3. Edge : . Why? .
  4. Edge : . Why? symmetric, .
  5. Edge : . Why? , slope . First contribution.
  6. Edge : . Why? , slope ; accumulate — this is the second path. → .

Answer: .

Verify: , so ; at : . ✓ If you had overwritten instead of accumulating you'd get — wrong. This is the classic mistake.


Example 3 — Dead branch (Cell C)

Forecast: does affect at all? Guess .

  1. Forward. ; is computed but unused by . Why? We evaluate the whole program, but only what feeds matters for blame.
  2. Seed .
  3. Edge : . Why? , at is .
  4. Node : it has no child on the path to , so . Why? Blame only flows backward from the output; received no seed and no adjoint from any child.
  5. Edge : .

Answer: .

Verify: contains no , so for all . ✓ (Autograd sets adjoints of unreachable nodes to zero automatically — they simply never get pushed.)


Example 4 — Multiply a node by itself (Cell D)

Forecast: slope of is . Will autograd give or accidentally ?

  1. Forward. Treat the two inputs of the multiply as and where . . Why? A multiply node has two input slots; both happen to be fed by — that is a fan-out.
  2. Seed .
  3. Left leg : . Why? .
  4. Right leg : . Why? .
  5. Edge : .
  6. Edge : . Accumulate..

Answer: .

Verify: , at . ✓ The factor of comes entirely from summing both legs — the derivative rule for is autograd's fan-out at work.


Example 5 — Zero input kills the gradient (Cell E)

Forecast: one of these adjoints will be zero. Which one, and why?

  1. Forward. ; ; .
  2. Seed .
  3. Edge : . Why? here.
  4. Edge : . Why? .
  5. ; .

Answer: .

Verify: and at . ✓ A zero value on one input zeroes the other input's gradient — a degenerate but perfectly valid case. Frameworks handle it with no special code.


Example 6 — Full neuron chain (Cell F)

Forecast: since and , the loss wants to decrease . Will be positive or negative?

  1. Forward. ; ; . Why store ? needs ; the linear slope needs .
  2. Seed .
  3. Edge : . Why? .
  4. Edge : . Why? , the sigmoid identity.
  5. Edge : . Why? , so .

Answer: (positive).

Verify: positive gradient means Gradient descent will decrease , lowering , lowering toward — exactly what we want. Numerically the closed form gives the same . ✓


Example 7 — Broadcast / summed outputs (Cell G)

Forecast: , so the answer is obviously — but watch how the adjoints stack.

  1. Forward. ; .
  2. Seed .
  3. Sum node: for each term. So . Why? Derivative of a sum w.r.t. each summand is .
  4. Edge : .
  5. Edge : .
  6. Edge : . → .

Answer: .

Verify: , . ✓ Broadcasting one variable into many summed uses = sum the adjoints from every use — the general form of the fan-out rule (this is exactly how a bias reused across a batch gets its gradient in PyTorch autograd / TensorFlow GradientTape).


Example 8 — Limiting behaviour: saturated sigmoid (Cell H)

Forecast: the sigmoid is nearly flat far from the origin. Guess: will be near or near ?

Figure — Computational graphs and autograd

The figure shows flattening: at the curve is almost horizontal, so a wiggle in barely moves .

  1. Forward. .
  2. Local slope . Why? Sigmoid identity again.
  3. Edge : .

Answer: — essentially zero.

Verify: as , , , so . Same as (). ✓ This is the vanishing-gradient case: saturated neurons pass almost no blame backward — a real reason deep nets stall, and why alternatives to sigmoid exist (Sigmoid and activation functions).


Example 9 — Word problem: cost sensitivity (Cell I)

Forecast: guess whether cutting the price or the count gives more cost reduction per unit nudge.

  1. Model as a graph. (multiply), (add). Why? Translate the story into add/multiply primitives.
  2. Forward. ; .
  3. Seed (we ask sensitivity of total cost).
  4. Edge : ; also . Why? Add node passes blame unchanged.
  5. Edge : . Why? .
  6. Edge : . Why? .

Answer: dollars-per-(dollar-of-price), dollars-per-part.

Verify: dropping price by $1 saves $100 (all 100 parts get cheaper); dropping count by 1 saves $3 (one part's price). Cutting price moves cost far faster. Units check: has units $/($/part)·... = dollars saved per unit price change. ✓ This is exactly a gradient in cost space.


Example 10 — Exam twist: branches that cancel (Cell J)

Forecast: always. But do the intermediate adjoints vanish, or do they cancel at the end? Guess.

  1. Forward. ; ; .
  2. Seed .
  3. Edge : . Why? .
  4. Edge : . Why? — the sign matters.
  5. Edge : .
  6. Edge : . Accumulate..

Answer: .

Verify: identically, so . ✓ Note the adjoints did not vanish individually (); they cancelled at the accumulation step. This is the subtractive twin of Example 2 — same fan-out, opposite signs — and shows why keeping edge signs correct is critical.


Recall Self-test on the cells

Which cell is ? ::: Cell D — fan-out in disguise, both legs summed to give . Why is Example 10's answer zero even though ? ::: The two path adjoints ( and ) cancel at the accumulation step. In Example 8, what physical phenomenon does represent? ::: The vanishing gradient of a saturated sigmoid. In the word problem, why is ? ::: (many parts share the price) while .