Practice / Backprop by hand
One-hidden-layer backprop, the whole backward pass
Before you start
The previous two pages computed the pieces of a backward pass: the gradient of softmax with cross-entropy, and the rule for pushing a gradient back through an affine map and an elementwise nonlinearity. These ten problems assemble them into the whole backward pass of a classifier with one hidden layer, first for one example and then for a batch, and then change the activation and the loss to see what moves. The five mistakes are the ones that survive a quick read: an outer product and a batched product transposed, the activation's derivative dropped, a bias gradient left unsummed, and a stray factor of 2 in weight decay.
- The conventions are those of the previous pages: vectors are columns, Jacobians are in numerator layout, a gradient has the shape of the variable it is taken with respect to, and for a scalar of with a function of , .
- The network: ; and , where is the logistic sigmoid applied elementwise, with ; ; ; and with one-hot.
- The two deltas are the gradients at the pre-activations: and .
- Entries: is entry of , and , , are entries of , , ; likewise one layer down.
- is the elementwise product and the all-ones vector.
- Batched: rows are examples. , , , , is the row-wise softmax of , holds the one-hot targets as rows, and is the mean of the per-example losses. and .
- The softmax page gives , and the Jacobians page gives the pattern ; this page only assembles them, so every step is a citation plus a shape check.
Builds on: Jacobians and the chain rule, The softmax Jacobian
Problems
- ·
List the shapes of and of in terms of .
- ·
Compute .
- ··
Compute and in terms of and , with shapes.
- ··
Compute .
- ··
Compute in terms of , and , without forming any Jacobian of .
- ··
Compute and .
- ··
Compute . Why would you want it?
- ···
Batched: with (rows are examples), , , , row-wise, one-hot rows and , compute , , , , , and , with shapes.
- ···
Replace by . What changes in Problems 5–7? Write and .
- ···
Add an L2 penalty: . Compute .
Answers
- , ; , ; each gradient has the shape of its variable: , , , ,
- ();
- ();
- (); (); ; (); ; ; ()
- ; ; nothing else changes
Worked solutions
Problem 1
List the shapes of and of in terms of .
- for , so is ; is added to , so and have entries.A matrix takes -vectors to -vectors only if it has columns and rows, and vectors can be added only if their shapes agree.
- has entries. is elementwise, so it keeps the shape of its input.
- , so is ; , and have entries.Step 1 one layer up, with as the input; the softmax gives one probability per class, so it keeps the shape of .
- Entry of is , and likewise for every other variable.A gradient has one entry per entry of its variable, which is why is defined.
- , ; , ; each gradient has the shape of its variable: , , , , Every answer below is checked against this list.
Problem 2
Compute .
- As a function of , ., and holds everything below fixed.
- Let be the index with . Then .Only the target term of the sum survives, and .
- . contains only when , which is ; the log-sum-exp term differentiates to . This is the softmax page's Problem 5 with .
- , the shape of . Its entries sum to : the target entry is and the others are positive.
Problem 3
Compute and in terms of and , with shapes.
- .Index form shows which entry of each entry of and builds.
- appears only in , with coefficient .It sits in row of , and row builds only ; within that sum, only term contains it.
- . depends on only through ; by step 2 only the term is nonzero.
- , with shapes .An outer product has entries : the row index comes from and the column index from , matching step 3.
- . appears only in , with coefficient : (the Jacobians page, Problem 2).
- (); and , the shapes of and from Problem 1. It is the Jacobians page's with as the incoming gradient: the gradient at the output of the affine map times its input, transposed.
Problem 4
Compute .
- , . is affine in (the Jacobians page, Problem 2).
- . depends on only through , so the chain rule for gradients applies.
- Shapes , the shape of . Entry is : hidden unit collects the output deltas weighted by its outgoing weights, column of .
Problem 5
Compute in terms of , and , without forming any Jacobian of .
- , . is elementwise (the Jacobians page, Problem 1).
- . depends on only through ; a diagonal matrix is its own transpose; is Problem 4.
- for any .A diagonal matrix acting on a vector scales entry by diagonal entry : the vector-Jacobian product of the Jacobians page, Problem 6, so the matrix is never built.
- , the shape of . For the sigmoid, , computed from the saved in the forward pass.
Problem 6
Compute and .
- has the form of , with in place of and in place of .Problem 3 used only that form and the gradient at its output, so its argument applies unchanged.
- and . appears only in , with coefficient , and only in , with coefficient (Problem 3, steps 2 to 5).
- (); and , the shapes of and from Problem 1.
Problem 7
Compute . Why would you want it?
- , . is affine in .
- , with shapes . depends on only through ; the result has the shape of .
- No parameter update uses it. is data, not a parameter, so gradient descent never changes it.
- You want it for three things. Adversarial examples: a small step along raises the loss the most, to first order, among changes of the same maximum entry size. Saliency: the size of entry says how sensitive the loss is to input feature . And if is itself the output of a layer below, is that layer's incoming gradient, from which it gets its own exactly as Problem 5 got from .
Problem 8
Batched: with (rows are examples), , , , row-wise, one-hot rows and , compute , , , , , and , with shapes.
Write for example as a column, so row of is , and likewise , , , and , for the single-example deltas of Problems 2 and 5.
- Row of is , and likewise down the network.Row of is row of times , and adds to every row. So the batch is copies of the single-example network, one per row, sharing the parameters.
- depends only on row of , , , and , so row of is .Only the -th term of the mean contains row of , and it carries the factor ; the rest is Problem 2 for example (the softmax page, Problem 7).
- , .Stack the rows of step 2.
- . is used by every row, so the loss reaches it through all copies and its gradient is the sum of their contributions; each is Problem 3 for example , scaled by .
- For matrices and with rows and , ., the entry of the sum of outer products.
- , with shapes .Step 5 with , whose rows are , and . The shape is that of .
- , with shapes . is added to every row, so its gradient is the sum over rows of Problem 3's ; sums the rows of .
- , with shapes .Row is Problem 4 for example , transposed: .
- , , with applied to every entry of .Problem 5 row by row: an elementwise product is taken entry by entry, so it does not care whether the entries are laid out as columns or rows. Row is , Problem 5 for example .
- , ; , .Steps 4 to 7 one layer down, with in place of and in place of .
- , .Step 8 one layer down: row is , Problem 7 for example scaled by .
- (); (); ; (); ; ; ()Every gradient has the shape of its variable. The enters once, in , and everything after it is linear in , so it is carried through and never applied again.
Problem 9
Replace by . What changes in Problems 5–7? Write and .
- for and for ; write for the vector whose entry is if and otherwise.ReLU is to the right of and to the left. At the one-sided slopes are and , so the derivative is undefined; it is taken as , and a pre-activation exactly at is rare with real-valued data.
- , , and keep their formulas, with .Problems 2 to 4 used only as a vector feeding ; none of them differentiated the activation. The values change, the formulas do not.
- .Problem 5 used only that the activation is elementwise, so its derivative is replaced by , which is by step 1.
- , , .Problems 6 and 7 start from and never look at the activation; the new flows into them.
- ; ; nothing else changesOnly the elementwise factor in Problem 5 changes. A hidden unit with passes back nothing: entry of is , so row of and entry of are zero for this example. A unit that is negative on every input never updates, which is a dead ReLU.
Problem 10
Add an L2 penalty: . Compute .
- .The squared Frobenius norm is the sum of the squares of the entries.
- , so the penalty's gradient is , .Only one term of the sum contains , and has derivative . This is the matrix-calculus page's , times .
- , .Problem 3; the penalty does not involve , so is unchanged.
- The gradient of a sum is the sum of the gradients, and both terms are . A gradient step becomes : the update shrinks the weights by and adds the data term, which is why this is called weight decay. The other gradients are unchanged, because the penalty contains only .
Where this goes wrong
1. Outer product the wrong way round
The weight gradient is built from two column vectors, and , and a product of two columns needs one of them transposed.
- is an outer product of () and ()Right so far: Problem 3, step 3 gives .
- “ goes into the layer and comes back out of it, so comes first.”The analogy that causes the mistake: writing the factors in the order the data flows, input then output, as if the gradient were read along the forward pass.
- It is , the transpose of 's shape, so is undefined unless , and wrong even then. The index form fixes which index is which: in the row index is the output unit and belongs to , and the column index is the input unit and belongs to , so the gradient is .
2. Passing the delta through the activation unchanged
Backprop is often summarised as “multiply the delta by the transposed weights and pass it down”, and the summary leaves out half of each layer.
- Right so far: Problem 4.
- “The delta goes back through the transposed weights, so the hidden layer's delta is .”The analogy that causes the mistake: the rule for a linear layer, applied to a layer that also has a nonlinearity, so that and are treated as the same vector.
- That is , not . The chain rule through contributes elementwise; dropping it is right only for an identity activation. For the sigmoid , so every nonzero entry comes out at least 4 times too large, and a saturated unit, whose true delta is near , keeps receiving a full-sized one.
3. Batched weight gradient with the data matrix on the wrong side
Linear regression's gradient, , puts the transposed data matrix on the left, and the batched backward pass looks like the same kind of product.
- , summed over the batchRight so far: Problem 8, step 4.
- “A sum over examples is a product with the transposed data matrix on the left, as in .”The analogy that causes the mistake: in linear regression the weights are a column vector, so the data matrix goes on the left; here the weights are a matrix and the order is fixed by 's shape.
- It is . With rows as examples the gradient is , . The entries are the same numbers transposed, , which is why it “looks right” until the update fails to broadcast; when the update runs and silently applies the transpose.
4. Bias gradient that is still a matrix
For one example the bias gradient is the delta itself, , and in the batch the delta is a matrix.
- , Right so far: Problem 8, step 3.
- “The bias gradient equals the delta, as in Problem 3.”The analogy that causes the mistake: a single-example identity carried to the batch without asking how the bias enters. copies one bias into every row.
- It is , and has entries. The bias is shared across the batch, so its gradient sums the rows: . In array code may broadcast without complaint and turn the bias into an matrix, one bias per example.
5. Doubling the weight-decay gradient
The matrix-calculus page's is short enough to memorise, and the 2 in it is easy to carry across without the it is meant to meet.
- , and Right so far: both are true.
- “The gradient of a squared Frobenius norm is twice the matrix, so the penalty contributes .”The analogy that causes the mistake: applied to instead of , as if the prefactor were .
- The is there to cancel the 2: (Problem 10). The slip survives because training still works: the model is simply regularised as if were twice what was set, so a tuned with this code must be doubled in any correct implementation.
Print this set: one-hidden-layer-backprop.pdf (problems, answers, and worked solutions on separate pages).