Practice / Minibatches

Batched backprop: dense layers on a minibatch

Ten problems on backprop with a minibatch: the dense layer XW + 1bᵀ and its gradients XᵀdY, dYᵀ1 and dY Wᵀ, why a sum over the batch becomes a matrix product, the 1/N of a mean loss, activations and masked softmax cross-entropy on a batch, micro-batch accumulation and sequence batches, with worked solutions and the mistakes that transpose, double-count or drop a factor.

Before you start

Every earlier page did its derivations one example at a time and then stacked the results. Real training runs a whole minibatch through each layer as one matrix, and the backward pass is written the same way. These ten problems take a dense layer in the layout most array code uses, XW+1b⊤XW + \mathbf{1}b^\top, derive its three gradients from index form, show where the sum over the batch hides inside a matrix product, and then follow the batch dimension through a mean loss, an activation, a masked loss, gradient accumulation and sequences. The five mistakes at the end are the ones that pass a quick look: a weight gradient in the other layout, a 1/N1/N applied twice, WW where W⊤W^\top belongs, an elementwise derivative used as a matrix, and micro-batch gradients summed instead of averaged.

  • The conventions are those of the previous pages: a gradient has the shape of the variable it is taken with respect to, ⊙\odot is the elementwise product, 1\mathbf{1} is the all-ones vector, and rows are examples.
  • The dense layer: input X∈RN×dX \in \mathbb{R}^{N \times d} with row nn equal to x(n)⊤x^{(n)\top}, weights W∈Rd×mW \in \mathbb{R}^{d \times m}, bias b∈Rmb \in \mathbb{R}^m, and output Y=XW+1b⊤∈RN×mY = XW + \mathbf{1}b^\top \in \mathbb{R}^{N \times m}, with row nn written y(n)⊤y^{(n)\top}. This is the layout of most array code, which stores WW as inputs by outputs; the one-hidden-layer page used the other layout, XW1⊤+1b1⊤XW_1^\top + \mathbf{1}b_1^\top with W1W_1 outputs by inputs.
  • LL is a scalar loss computed from YY by the layers above. For any array AA in the network, dAdA is the code-style name for ∇AL\nabla_A L: the array of ∂L/∂A…\partial L/\partial A_{\dots}, with the shape of AA. Row nn of dYdY is written dy(n)⊤dy^{(n)\top}.
  • The backward pass of a layer receives dYdY from above and returns dWdW, dbdb, and dXdX for the layer below.
  • A mean loss over the batch is L=1N∑nℓnL = \tfrac1N\sum_n \ell_n, where ℓn\ell_n depends only on example nn. Targets are written TT to keep YY for the layer's output.

Builds on: Jacobians and the chain rule, One-hidden-layer backprop, the whole backward pass

Problems

  1. ·

    Give the shapes of XX, WW, bb, YY and of dYdY, dWdW, dbdb, dXdX. Show that row nn of YY is the layer applied to example nn alone.

  2. ··

    Compute dWdW from index form.

  3. ··

    Compute dbdb.

  4. ··

    Compute dXdX. Does it involve a sum over the batch?

  5. ···

    Write dWdW as a sum of single-example gradients and show it is X⊤dYX^\top dY. How many entries does the Jacobian of YY with respect to WW have, and how many are nonzero?

  6. ··

    Mean loss: L=12N∥Y−T∥F2L = \tfrac1{2N}\|Y - T\|_F^2 with targets T∈RN×mT \in \mathbb{R}^{N \times m}. Compute dYdY, dWdW and dbdb. Where does the 1/N1/N enter?

  7. ··

    Let H=tanh⁡(Z)H = \tanh(Z) elementwise, with Z∈RN×mZ \in \mathbb{R}^{N \times m}. Given dHdH, compute dZdZ without forming any Jacobian.

  8. ···

    Masked softmax cross-entropy: logits Z∈RN×kZ \in \mathbb{R}^{N\times k}, row-wise softmax SS, one-hot target rows TT, and μ∈{0,1}N\mu \in \{0, 1\}^N marking the real rows (padding has μn=0\mu_n = 0; at least one μn=1\mu_n = 1). With ℓn=−∑iTnilog⁡Sni\ell_n = -\sum_i T_{ni}\log S_{ni}, the loss is L=∑nμnℓn/∑nμnL = \sum_n \mu_n\ell_n / \sum_n \mu_n. Compute dZdZ, and check the case where every μn=1\mu_n = 1.

  9. ···

    Gradient accumulation: split NN rows into KK micro-batches of B=N/KB = N/K rows, with L(j)L^{(j)} the mean loss over micro-batch jj. Show that the full-batch mean loss is L=1K∑jL(j)L = \tfrac1K\sum_j L^{(j)} and give ∇WL\nabla_W L. What changes if the sizes differ?

  10. ···

    Sequences: X∈RN×T×dX \in \mathbb{R}^{N\times T\times d} holds NN sequences of TT positions, and the layer acts at every position: y(n,t)=W⊤x(n,t)+by^{(n,t)} = W^\top x^{(n,t)} + b, with x(n,t)x^{(n,t)} position tt of sequence nn. Compute dWdW and dbdb as matrix products.

Worked solutions

Problem 1

Give the shapes of XX, WW, bb, YY and of dYdY, dWdW, dbdb, dXdX. Show that row nn of YY is the layer applied to example nn alone.

  1. XWXW is (N×d)(d×m)=N×m(N \times d)(d \times m) = N \times m.A product needs the inner dimensions to agree, so WW has dd rows, one per input feature, and its mm columns are the output features.
  2. 1b⊤\mathbf{1}b^\top is (N×1)(1×m)=N×m(N \times 1)(1 \times m) = N \times m, and every row of it is b⊤b^\top.(1b⊤)nj=1⋅bj(\mathbf{1}b^\top)_{nj} = 1 \cdot b_j. This is the matrix form of adding bb to every row, which array code does by broadcasting.
  3. Row nn of XWXW is x(n)⊤Wx^{(n)\top}W.Row nn of a product is row nn of the left factor times the right factor.
  4. dAdA has the shape of AA for every AA.It has one entry ∂L/∂A…\partial L/\partial A_{\dots} per entry of AA, so that A−η dAA - \eta\,dA is defined.
  5. X:N×dX: N\times d, W:d×mW: d\times m, b:mb: m, Y:N×mY: N\times m, and each dAdA has the shape of AA; row nn of YY is x(n)⊤W+b⊤x^{(n)\top}W + b^\topRow nn uses only row nn of XX and the shared WW and bb: the batch is NN copies of the single-example layer y=W⊤x+by = W^\top x + b, one per row, sharing the parameters.

Problem 2

Compute dWdW from index form.

  1. Ynj=∑iXniWij+bjY_{nj} = \sum_i X_{ni}W_{ij} + b_j.Index form of Y=XW+1b⊤Y = XW + \mathbf{1}b^\top.
  2. WijW_{ij} appears in YnjY_{nj} for every nn, with coefficient XniX_{ni}, and in no entry outside column jj.Column jj of WW builds only column jj of YY, and every example uses the same WW.
  3. ∂L∂Wij=∑n∂L∂Ynj Xni=∑nXni dYnj\dfrac{\partial L}{\partial W_{ij}} = \sum_n \dfrac{\partial L}{\partial Y_{nj}}\,X_{ni} = \sum_n X_{ni}\,dY_{nj}.The chain rule sums over every entry of YY that contains WijW_{ij}; by step 2 that is one entry per example.
  4. ∑nXni dYnj=(X⊤dY)ij\sum_n X_{ni}\,dY_{nj} = (X^\top dY)_{ij}.(X⊤)in=Xni(X^\top)_{in} = X_{ni}, and a sum over the shared index nn is the definition of the matrix product.
  5. dW=X⊤dYdW = X^\top dY (d×md\times m)(d×N)(N×m)=d×m(d \times N)(N \times m) = d \times m, the shape of WW. The batch index is the one summed over, which is why NN is the inner dimension of the product.

Problem 3

Compute dbdb.

  1. bjb_j appears in YnjY_{nj} for every nn, with coefficient 11.1b⊤\mathbf{1}b^\top copies b⊤b^\top into every row (Problem 1, step 2).
  2. ∂L/∂bj=∑ndYnj\partial L/\partial b_j = \sum_n dY_{nj}.The chain rule sums over the NN entries that contain bjb_j, each with derivative 11.
  3. db=dY⊤1db = dY^\top\mathbf{1} (mm)(m×N)(N×1)=m×1(m \times N)(N \times 1) = m \times 1, the shape of bb; multiplying by 1\mathbf{1} sums each column of dYdY, which array code writes as a sum over axis 00. A bias added to every row has its gradient summed over every row.

Problem 4

Compute dXdX. Does it involve a sum over the batch?

  1. XniX_{ni} appears in YnjY_{nj} for every jj, with coefficient WijW_{ij}, and in no other row of YY.Example nn feeds only row nn: in a dense layer the examples do not interact.
  2. ∂L∂Xni=∑jdYnj Wij=(dY W⊤)ni\dfrac{\partial L}{\partial X_{ni}} = \sum_j dY_{nj}\,W_{ij} = (dY\,W^\top)_{ni}.The chain rule sums over the entries of row nn that contain XniX_{ni}, one per output feature; (W⊤)ji=Wij(W^\top)_{ji} = W_{ij}.
  3. dX=dY W⊤dX = dY\,W^\top (N×dN\times d)(N×m)(m×d)=N×d(N \times m)(m \times d) = N \times d, the shape of XX. The sum is over output features, not over the batch: each example gets its own input gradient, and row nn is (W dy(n))⊤(W\,dy^{(n)})^\top, the single-example rule for y=W⊤x+by = W^\top x + b.

Problem 5

Write dWdW as a sum of single-example gradients and show it is X⊤dYX^\top dY. How many entries does the Jacobian of YY with respect to WW have, and how many are nonzero?

  1. For one example, y=W⊤x+by = W^\top x + b has yj=∑ixiWij+bjy_j = \sum_i x_i W_{ij} + b_j, so its gradient with respect to WW has entries xi dyjx_i\,dy_j: it is x dy⊤x\,dy^\top, d×md \times m.WijW_{ij} appears only in yjy_j, with coefficient xix_i; an outer product ab⊤ab^\top has entries aibja_i b_j.
  2. dW=∑nx(n) dy(n)⊤dW = \sum_n x^{(n)}\,dy^{(n)\top}.Every example uses the same WW, so the loss reaches WW through all NN rows and the contributions add.
  3. For matrices AA and CC with rows a(n)⊤a^{(n)\top} and c(n)⊤c^{(n)\top}, ∑na(n)c(n)⊤=A⊤C\sum_n a^{(n)}c^{(n)\top} = A^\top C.(A⊤C)ij=∑nAniCnj=∑nai(n)cj(n)(A^\top C)_{ij} = \sum_n A_{ni}C_{nj} = \sum_n a^{(n)}_i c^{(n)}_j, the (i,j)(i,j) entry of the sum of outer products.
  4. Flatten YY to NmNm entries and WW to dmdm; the Jacobian is Nm×dmNm \times dm, and entry (Ynj,Wij′)(Y_{nj}, W_{ij'}) is XniX_{ni} if j′=jj' = j and 00 otherwise.YnjY_{nj} depends only on column jj of WW (Problem 2, step 2), so for each of the NmNm outputs only dd of the dmdm weights give a nonzero entry: Nm⋅dNm \cdot d in total.
  5. ∑nx(n) dy(n)⊤=X⊤dY\sum_n x^{(n)}\,dy^{(n)\top} = X^\top dY; the Jacobian of YY with respect to WW has Nm×dmNm \times dm entries, only NdmNdm of them nonzero, and the matrix product never forms itStep 3 with A=XA = X, C=dYC = dY. X⊤dYX^\top dY costs NdmNdm multiply-adds, the same as the forward product XWXW, while the full Jacobian has Ndm2Ndm^2 entries: at N=512N = 512 and d=m=4096d = m = 4096, about 3.5×10133.5 \times 10^{13}. The matrix product is the vector-Jacobian product of the Jacobians page, done for the whole batch at once.

Problem 6

Mean loss: L=12N∥Y−T∥F2L = \tfrac1{2N}\|Y - T\|_F^2 with targets T∈RN×mT \in \mathbb{R}^{N \times m}. Compute dYdY, dWdW and dbdb. Where does the 1/N1/N enter?

  1. L=1N∑nℓnL = \tfrac1N\sum_n \ell_n with ℓn=12∥y(n)−t(n)∥2\ell_n = \tfrac12\|y^{(n)} - t^{(n)}\|^2, where t(n)⊤t^{(n)\top} is row nn of TT.The squared Frobenius norm is the sum of the squared entries, which is the sum of the squared row norms.
  2. ∂L/∂Ynj=1N(Ynj−Tnj)\partial L/\partial Y_{nj} = \tfrac1N(Y_{nj} - T_{nj}).Only ℓn\ell_n contains row nn of YY, it carries the factor 1N\tfrac1N, and 12u2\tfrac12u^2 differentiates to uu.
  3. dY=1N(Y−T)dY = \tfrac1N(Y - T), N×mN \times m.Step 2 for every entry.
  4. dW=X⊤dYdW = X^\top dY and db=dY⊤1db = dY^\top\mathbf{1}.Problems 2 and 3 hold for any dYdY; they never used where it came from.
  5. dY=1N(Y−T)dY = \tfrac1N(Y - T); dW=1NX⊤(Y−T)dW = \tfrac1N X^\top(Y - T); db=1N(Y−T)⊤1db = \tfrac1N(Y - T)^\top\mathbf{1}The 1N\tfrac1N enters once, in dYdY, and every later gradient is linear in dYdY, so it carries the factor without applying it again. dWdW is the average of the single-example gradients; with a summed loss it would be NN times larger.

Problem 7

Let H=tanh⁡(Z)H = \tanh(Z) elementwise, with Z∈RN×mZ \in \mathbb{R}^{N \times m}. Given dHdH, compute dZdZ without forming any Jacobian.

  1. HnjH_{nj} depends only on ZnjZ_{nj}.tanh⁡\tanh is applied to each entry on its own, in every row.
  2. ∂L/∂Znj=dHnj tanh⁡′(Znj)\partial L/\partial Z_{nj} = dH_{nj}\,\tanh'(Z_{nj}).ZnjZ_{nj} reaches LL only through HnjH_{nj}, so the chain rule has one term.
  3. tanh⁡′(u)=1−tanh⁡2(u)\tanh'(u) = 1 - \tanh^2(u), so tanh⁡′(Znj)=1−Hnj2\tanh'(Z_{nj}) = 1 - H_{nj}^2.Differentiate tanh⁡=sinh⁡/cosh⁡\tanh = \sinh/\cosh with the quotient rule and use cosh⁡2−sinh⁡2=1\cosh^2 - \sinh^2 = 1; the forward pass already saved HH.
  4. dZ=(1−H⊙H)⊙dHdZ = (1 - H\odot H)\odot dH (N×mN\times m)1−H⊙H1 - H\odot H means 11 minus each entry of H⊙HH\odot H. The Jacobian of HH with respect to ZZ is Nm×NmNm \times Nm and diagonal, with these entries on the diagonal, so multiplying by it is an elementwise product: the batch changes the shape, not the rule.

Problem 8

Masked softmax cross-entropy: logits Z∈RN×kZ \in \mathbb{R}^{N\times k}, row-wise softmax SS, one-hot target rows TT, and μ∈{0,1}N\mu \in \{0, 1\}^N marking the real rows (padding has μn=0\mu_n = 0; at least one μn=1\mu_n = 1). With ℓn=−∑iTnilog⁡Sni\ell_n = -\sum_i T_{ni}\log S_{ni}, the loss is L=∑nμnℓn/∑nμnL = \sum_n \mu_n\ell_n / \sum_n \mu_n. Compute dZdZ, and check the case where every μn=1\mu_n = 1.

  1. Let M=∑nμn=1⊤μM = \sum_n \mu_n = \mathbf{1}^\top\mu, the number of real rows, so L=1M∑nμnℓnL = \tfrac1M\sum_n \mu_n\ell_n.M≥1M \ge 1 by assumption, and it does not depend on ZZ, so it is a constant for the derivative.
  2. Row nn of ZZ feeds only ℓn\ell_n, and ∇z(n)ℓn=s(n)−t(n)\nabla_{z^{(n)}}\ell_n = s^{(n)} - t^{(n)}, with z(n)⊤z^{(n)\top}, s(n)⊤s^{(n)\top}, t(n)⊤t^{(n)\top} row nn of ZZ, SS, TT.The softmax is taken row by row; the gradient is the softmax page's Problem 5 for example nn.
  3. Row nn of dZdZ is μnM (s(n)−t(n))⊤\tfrac{\mu_n}{M}\,(s^{(n)} - t^{(n)})^\top.ℓn\ell_n enters LL with coefficient μn/M\mu_n/M.
  4. Multiplying row nn of a matrix by μn\mu_n, for every nn, is multiplying it on the left by diag⁡(μ)\operatorname{diag}(\mu).(diag⁡(μ)A)ni=μnAni(\operatorname{diag}(\mu)A)_{ni} = \mu_n A_{ni}.
  5. dZ=11⊤μdiag⁡(μ)(S−T)dZ = \tfrac{1}{\mathbf{1}^\top\mu}\operatorname{diag}(\mu)(S - T); with every μn=1\mu_n = 1 it is 1N(S−T)\tfrac1N(S - T)With every row real, diag⁡(μ)=I\operatorname{diag}(\mu) = I and 1⊤μ=N\mathbf{1}^\top\mu = N: the softmax page's batched result. Padding rows get a zero gradient whatever their logits, and dividing by the number of real rows, not by NN, keeps the loss on the same scale however much padding a batch carries. In code diag⁡(μ)\operatorname{diag}(\mu) is never built: the mask multiplies each row.

Problem 9

Gradient accumulation: split NN rows into KK micro-batches of B=N/KB = N/K rows, with L(j)L^{(j)} the mean loss over micro-batch jj. Show that the full-batch mean loss is L=1K∑jL(j)L = \tfrac1K\sum_j L^{(j)} and give ∇WL\nabla_W L. What changes if the sizes differ?

  1. L=1N∑nℓn=1N∑j∑n∈batch jℓnL = \tfrac1N\sum_n \ell_n = \tfrac1N\sum_j \sum_{n \in \text{batch } j}\ell_n.Group the sum by micro-batch; each example is in exactly one.
  2. ∑n∈batch jℓn=B L(j)\sum_{n \in \text{batch } j}\ell_n = B\,L^{(j)}.L(j)L^{(j)} is the mean of the BB losses in micro-batch jj.
  3. L=BN∑jL(j)=1K∑jL(j)L = \tfrac BN\sum_j L^{(j)} = \tfrac1K\sum_j L^{(j)}.N=KBN = KB.
  4. ∇WL=1K∑j∇WL(j)\nabla_W L = \tfrac1K\sum_j \nabla_W L^{(j)}.The gradient of a sum is the sum of the gradients, and 1K\tfrac1K is a constant.
  5. L=1K∑jL(j)L = \tfrac1K\sum_j L^{(j)}, so ∇WL=1K∑j∇WL(j)\nabla_W L = \tfrac1K\sum_j \nabla_W L^{(j)}Accumulating the KK micro-batch gradients and dividing once by KK reproduces the full-batch gradient, up to rounding. This needs the examples not to interact, which holds for every layer on this page; batch normalisation, which mixes examples, is the exception. With sizes BjB_j the same steps give weights Bj/NB_j/N in place of 1K\tfrac1K.

Problem 10

Sequences: X∈RN×T×dX \in \mathbb{R}^{N\times T\times d} holds NN sequences of TT positions, and the layer acts at every position: y(n,t)=W⊤x(n,t)+by^{(n,t)} = W^\top x^{(n,t)} + b, with x(n,t)x^{(n,t)} position tt of sequence nn. Compute dWdW and dbdb as matrix products.

  1. dW=∑n,tx(n,t) dy(n,t)⊤dW = \sum_{n,t} x^{(n,t)}\,dy^{(n,t)\top} and db=∑n,tdy(n,t)db = \sum_{n,t} dy^{(n,t)}.Every position of every sequence uses the same WW and bb, so each is an example in the sense of Problems 3 and 5, and their contributions add.
  2. Let X~∈RNT×d\tilde X \in \mathbb{R}^{NT\times d} have one row x(n,t)⊤x^{(n,t)\top} per pair (n,t)(n,t), and dY~∈RNT×md\tilde Y \in \mathbb{R}^{NT\times m} the rows dy(n,t)⊤dy^{(n,t)\top} in the same order.Stacking the pairs as rows turns the double sum into a single sum over the NTNT rows; any order works if both use the same one.
  3. dW=X~⊤dY~dW = \tilde X^\top d\tilde Y and db=dY~⊤1db = d\tilde Y^\top\mathbf{1}, where X~\tilde X (NT×dNT\times d) and dY~d\tilde Y (NT×mNT\times m) stack every position of every sequence as a rowProblem 5, step 3 and Problem 3 applied to the NTNT rows: (d×NT)(NT×m)=d×m(d \times NT)(NT \times m) = d \times m. A position-wise layer treats positions exactly like examples, so a transformer's feed-forward and projection layers see a batch of NTNT rows. If the loss is a mean over all positions, the factor in dY~d\tilde Y is 1NT\tfrac1{NT}, not 1N\tfrac1N.

Where this goes wrong

1. Weight gradient in the outputs-by-inputs layout

The one-hidden-layer page computed ∇W2L=Δ2⊤H\nabla_{W_2} L = \Delta_2^\top H, a product with the transposed delta on the left, and that pattern is easy to carry from one layer to the next.

  1. Y=XW+1b⊤Y = XW + \mathbf{1}b^\top, WW is d×md \times m, dYdY is N×mN \times mRight so far: the layer and shapes of Problem 1.
  2. “The weight gradient is the upstream gradient, transposed, times the input.”The habit that causes the mistake: the rule for Y=XW⊤+1b⊤Y = XW^\top + \mathbf{1}b^\top, where WW is outputs by inputs, applied to a layer stored inputs by outputs.
  3. dW=dY⊤XdW = dY^\top XIt is (m×N)(N×d)=m×d(m \times N)(N \times d) = m \times d, the transpose of WW's shape. In Y=XWY = XW, WijW_{ij} multiplies XniX_{ni} into YnjY_{nj}, so dW=X⊤dYdW = X^\top dY (Problem 2). When d=md = m the update runs and silently applies the transpose.

2. Dividing by N twice

The bias gradient is often described as “the average of the deltas over the batch”, and with a mean loss that description is true of the result, not of the formula.

  1. dY=1N(Y−T)dY = \tfrac1N(Y - T)Right so far: Problem 6, step 3. The 1N\tfrac1N of the mean loss is already inside dYdY.
  2. “The bias gradient is the average of the upstream gradient over the batch.”The shortcut that causes the mistake: averaging rows of a dYdY that already carries the 1N\tfrac1N.
  3. db=1N dY⊤1db = \tfrac1N\,dY^\top\mathbf{1}That is 1N2(Y−T)⊤1\tfrac1{N^2}(Y - T)^\top\mathbf{1}, NN times too small. The rule is db=dY⊤1db = dY^\top\mathbf{1}, a plain sum (Problem 3), because the factor was applied once at the loss (Problem 6). The bias then learns NN times more slowly than the weights, and the gap grows with the batch size.

3. Multiplying the upstream gradient by W instead of Wᵀ

The forward pass multiplies by WW, and it is tempting to think the backward pass simply multiplies by it again.

  1. Y=XW+1b⊤Y = XW + \mathbf{1}b^\top, and dXdX should have the shape of XX, N×dN \times dRight so far: Problem 1.
  2. “Backward goes through the same weights, so multiply by WW.”The analogy that causes the mistake: the backward pass uses the same weights, but it runs the map in the opposite direction, from mm output features back to dd input features, which is what W⊤W^\top does.
  3. dX=dY WdX = dY\,W(N×m)(d×m)(N \times m)(d \times m) is undefined unless m=dm = d. The rule is dX=dY W⊤dX = dY\,W^\top (Problem 4): ∂L/∂Xni=∑jdYnjWij\partial L/\partial X_{ni} = \sum_j dY_{nj}W_{ij} sums over WW's second index. For a square WW the product runs and sends every gradient back along the wrong weights.

4. Elementwise derivative applied as a matrix product

The chain rule is a product of Jacobians, and in a batch every factor looks like a matrix.

  1. H=tanh⁡(Z)H = \tanh(Z), and 1−H⊙H1 - H\odot H is N×mN \times mRight so far: Problem 7, step 3 gives the derivative at every entry.
  2. “The chain rule multiplies the upstream gradient by the local derivative, so multiply the matrices.”The analogy that causes the mistake: the rule ∇in=J⊤∇out\nabla_{\text{in}} = J^\top\nabla_{\text{out}} for vectors, applied to two N×mN \times m arrays as if the derivative array were the Jacobian.
  3. dZ=dH (1−H⊙H)⊤dZ = dH\,(1 - H\odot H)^\topIt is (N×m)(m×N)=N×N(N \times m)(m \times N) = N \times N, a matrix over pairs of examples. The Jacobian of an elementwise map is diagonal, so multiplying by it is an elementwise product: dZ=(1−H⊙H)⊙dHdZ = (1 - H\odot H)\odot dH (Problem 7).

5. Summing micro-batch means without dividing by K

Gradient accumulation is usually written as a running sum of gradients, and the division at the end is easy to leave out.

  1. L=1K∑jL(j)L = \tfrac1K\sum_j L^{(j)} with L(j)L^{(j)} the mean over micro-batch jjRight so far: Problem 9, step 3.
  2. “Accumulate the gradients of the micro-batches; the result is the gradient of the full batch.”The shortcut that causes the mistake: true when each L(j)L^{(j)} is a sum, or already divided by the full NN, but here each one is a mean over BB rows.
  3. ∇WL=∑j∇WL(j)\nabla_W L = \sum_j \nabla_W L^{(j)}It is KK times the full-batch gradient: each L(j)L^{(j)} is already a mean, so the sum of KK of them must be divided by KK (Problem 9). Switching from one batch of NN to KK micro-batches then multiplies the effective learning rate by KK, which looks like a training instability rather than an arithmetic slip.

Print this set: batched-backprop.pdf (problems, answers, and worked solutions on separate pages).