3.1.10 · D4Neural Network Fundamentals

Exercises — Computational graphs and autograd

2,187 words10 min readBack to topic

Throughout, the adjoint of a node is written and means "how much does the final scalar output change when wiggles a little." The rule we keep applying is the reverse-mode update: for each edge ,


Level 1 — Recognition

Exercise 1.1 (L1)

Here is a small computational graph for . Name the nodes, name the operations on the edges, and confirm it is a DAG (no cycles).

Figure — Computational graphs and autograd
Recall Solution

Nodes: the inputs ; the intermediate ; the output . Edge operations:

  • and : the add operation ().
  • and : the multiply operation (). DAG? Every edge points strictly "forward" (input → intermediate → output). You can never return to a node you left, so there are no cycles. Directed + acyclic = DAG. ✓

Exercise 1.2 (L1)

For , write down the two local derivatives and — the numbers autograd stores on those edges.

Recall Solution

is a product. Treat the other factor as a constant: Why these matter: in the backward pass the multiply edge sends and . The multiply node literally "swaps" the values.


Level 2 — Application

Exercise 2.1 (L2)

Function: with . Do a full forward pass (store all values) and a full backward pass to get .

Recall Solution

Forward (fill values):

Backward (blame each node):

  1. Seed .
  2. Multiply edge: , and .
  3. Add edge (, so ): , .

Answers: . Sanity check by hand: , so ✓, ✓.

Exercise 2.2 (L2)

For with , compute and the adjoint given the incoming adjoint .

Recall Solution

Why exp is special: its derivative is itself, .

  • Forward: .
  • Local derivative of the edge: .
  • Backward: .

Note the reuse: the backward step reuses the forward value . That is exactly why the forward pass stores intermediates.


Level 3 — Analysis

Exercise 3.1 (L3)

Function with a shared input: at . Notice feeds two branches. Compute and by the backward pass, and show the accumulation explicitly.

Figure — Computational graphs and autograd
Recall Solution

Break into primitives: , , . Forward:

Backward:

  1. .
  2. Multiply: , .
  3. Path through : .
  4. Path through : .
  5. Accumulate: .
  6. .

Check against the closed form : ✓.

Exercise 3.2 (L3)

Explain, using this same graph, why reverse mode gives both and in one backward pass, while forward mode would need one pass per input.

Recall Solution
  • Reverse mode seeds the single output () and propagates sensitivities backward. Every input's adjoint drops out of that one sweep — here and both. Cost number of outputs (one).
  • Forward mode seeds one input's perturbation (e.g. ) and pushes it forward to see . To also get you must re-seed () and run again. Cost number of inputs.
  • For a loss (one output, many weights), reverse mode wins massively. See Automatic differentiation (forward vs reverse mode).

Level 4 — Synthesis

Exercise 4.1 (L4) — one neuron end-to-end

, , , with . Values: . Forward + backward to get , , .

Recall Solution

Forward:

Backward (chain, each edge's local derivative):

  1. .
  2. Sigmoid edge: . .
  3. Linear edge :

Answers: . Interpretation: all negative → increasing decreases the loss, so a Gradient descent step (which subtracts the gradient) would increase them. Makes sense: , we want bigger.

Exercise 4.2 (L4)

Take one gradient-descent step on with learning rate , using from 4.1. Compute the new .

Recall Solution

Update rule: . increased, exactly the direction that pushes toward . See Loss functions and Gradient descent.


Level 5 — Mastery

Exercise 5.1 (L5) — deeper shared path

, where , at . Here feeds two children ( and ), and itself depends on both and . Get and .

Recall Solution

Primitives: ; ; ; . Forward: ; ; ; .

Backward:

  1. .
  2. Add node : , .
  3. : .
  4. : .
  5. Accumulate: .
  6. : ; .

Check : ✓.

Exercise 5.2 (L5) — design the backward pass yourself

Given , . Design and run the full backward pass; report (note appears in both the product and the exponential).

Recall Solution

Primitives: ; ; . Forward: ; ; .

Backward:

  1. .
  2. Add: , .
  3. Product : ; .
  4. Exp (local deriv ): .
  5. Accumulate: ; .

Check ✓.