Backpropagation with chain rule
Quick answer
Backpropagation computes derivatives of a scalar loss with respect to each trainable parameter by applying the chain rule from output toward input. Gradient descent then uses those derivatives to update parameters; backpropagation itself does not update them. Static examples verified locally with Python 3.13.5 and NumPy 2.3.5 where imported. ByteMind execution is unverified.
Scalar chain rule
Expected output: 7 6 3. The half-squared loss derivative is residual times input for weight and residual for bias.
How it works
With X (N,D), A=tanh(X@W1+b1) (N,H), logits Z=A@W2+b2 (N,1), Y (N,1), mean BCE gives E=(sigmoid(Z)-Y)/N. The derivative of softplus is sigmoid. Then dW2=A.T@E (H,1), db2=sum(E,axis=0) (1,), dA=E@W2.T (N,H), dZ1=dA*(1-A**2), dW1=X.T@dZ1 (D,H), db1=sum(dZ1,axis=0) (H,). Derive by summing sample contributions; the activation derivative acts elementwise. A scalar analogy: z=wx+b, L=(z-y)^2/2 gives dz=z-y, dw=dzx, db=dz. For a shared variable used along multiple paths, sum contributions. Compute all gradients using the same original parameters, then update simultaneously. For mean BCE do not divide by N again.
Two-layer scalar gradient
from math import tanhx,w1,b1,w2,b2,y=2,.3,.1,.4,-.2,1a=tanh(x*w1+b1); z=a*w2+b2e=z-yd1=e*w2*(1-a*a)print([round(v,5) for v in [d1*x,d1,e*a,e]])Expected output: [-0.48659, -0.2433, -0.57914, -0.95825]. Both layers receive gradients through the same forward pass; hidden gradient includes tanh slope.
Common mistakes
Do not update W2 before using it to compute dA. Do not confuse matrix products with elementwise activation derivatives. Sum bias derivatives over rows, not features. The sign of gradient descent is subtraction.
Practice and next steps
On paper derive dW1 and dW2 for a one-row, two-layer scalar network, then compare with a central-difference check in the next lesson.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Backpropagation with chain rule — knowledge check
Apply backpropagation with chain rule to small examples and debugging scenarios.
Which operation moves from dA to dZ1 for tanh?
Why sum bias gradients across rows?
When should W2 be updated?
What is dw for x=2,w=3,b=1,y=4 with half-squared loss?
How many batch divisions for mean BCE?