Backpropagation in a Two-Layer Neural Network: Trace Every Gradient Step by Step

Follow every derivative in a 2 -> 2 -> 1 sigmoid network, then update all nine parameters together. Two sanity checks expose sign and stale-value errors.

KnowledgeGate Team

Exam prep & CS education

Updated 24 Nov 20266 min read
Backpropagation in a Two-Layer Neural Network: Trace Every Gradient Step by Step

Training code can call backward() in one line, but that does not explain why a weight receives a positive, negative, large, or small gradient. A complete scalar pass through a 2 -> 2 -> 1 sigmoid network contains forward values, local derivatives, all nine parameter gradients, one simultaneous update, and two error checks. The chain rule connects the loss to every parameter without requiring a memorised backpropagation formula.

Backpropagation is the chain rule on a computation graph

This network has two trainable affine layers: hidden and output. The input vector is not a trainable layer. Its inputs are x1 = 1 and x2 = 2.

  • Hidden unit h1: w11 = 0.1, w12 = 0.2, b1 = 0.1

  • Hidden unit h2: w21 = -0.1, w22 = 0.1, b2 = 0

  • Output unit: v1 = 0.7, v2 = -0.3, bo = 0.2

Every unit uses sigma(z) = 1/(1 + e^(-z)). The target is y = 1, and L = 1/2(y_hat - y)^2. A bias is simply a weight connected to an implicit constant input of 1.

The reusable rule is: a parameter gradient equals the upstream derivative multiplied by the local derivatives along every path from that parameter to the loss. Store delta_o = dL/dzo, delta_h1 = dL/dz1, and delta_h2 = dL/dz2, then reuse them instead of expanding the whole chain each time.

For broader ANN foundations, Artificial Neural Networks: Worked Examples for GATE connects activation functions, perceptron learning and parameter counting in one overview. Backpropagation is the narrower reverse-pass problem: all nine parameter gradients and one simultaneous update for a two-layer sigmoid network. Skill Development Courses collect the wider applied-learning route.

Two-layer neural network forward pass: compute and cache every value

Start with the hidden units:

z1 = 0.1(1) + 0.2(2) + 0.1 = 0.600000

a1 = sigma(0.6) = 0.645656

z2 = -0.1(1) + 0.1(2) + 0 = 0.100000

a2 = sigma(0.1) = 0.524979

Now move to the output:

zo = 0.7(0.645656306) - 0.3(0.524979187) + 0.2 = 0.494466

y_hat = sigma(zo) = 0.621158

L = 1/2(0.621157862 - 1)^2 = 0.071761

Node

Pre-activation z

Activation a

Role in backward pass

h1

0.600000

0.645656

Gives sigma'(z1) and feeds the output

h2

0.100000

0.524979

Gives sigma'(z2) and feeds the output

Output

0.494466

0.621158

Gives dL/dy_hat and sigma'(zo)

Cache z1, a1, z2, a2, zo, and y_hat from this original pass. Since sigma'(z) = sigma(z)(1 - sigma(z)), each cached activation is enough to calculate its sigmoid derivative during the reverse pass.

Left-to-right sigmoid network showing inputs, hidden units, output, target, and loss.

Output-layer gradients: move one edge at a time

At the loss, dL/dy_hat = y_hat - y = -0.378842. At the output sigmoid, dy_hat/dzo = y_hat(1 - y_hat) = 0.235321. Therefore:

delta_o = dL/dzo = (-0.378842)(0.235321) = -0.089149

For one weight, write the full chain before using the delta shortcut:

dL/dv1 = (dL/dy_hat)(dy_hat/dzo)(dzo/dv1)

= (-0.378842)(0.235321)(0.645656) = -0.057560

The remaining output gradients follow from zo = v1a1 + v2a2 + bo:

  • dL/dv1 = delta_o a1 = -0.057560

  • dL/dv2 = delta_o a2 = -0.046802

  • dL/dbo = delta_o = -0.089149

Both hidden activations are positive, so increasing either output weight increases zo. The negative delta says the prediction is below the target and the loss falls when zo rises.

Hidden-layer gradients: use the original output weights

Send the upstream signal through the original v1 = 0.7 and v2 = -0.3:

delta_h1 = delta_o v1 a1(1-a1) = (-0.089149)(0.7)(0.228784) = -0.014277

delta_h2 = delta_o v2 a2(1-a2) = (-0.089149)(-0.3)(0.249376) = 0.006670

The incoming gradients are:

  • For h1: dL/dw11 = -0.014277, dL/dw12 = -0.028554, dL/db1 = -0.014277

  • For h2: dL/dw21 = 0.006670, dL/dw22 = 0.013339, dL/db2 = 0.006670

The x2 = 2 factor makes each x2-edge gradient twice its corresponding x1-edge gradient. The negative v2 reverses the signal's sign, so delta_h2 is positive even though delta_o is negative.

Right-to-left backpropagation diagram showing output and hidden deltas with parameter gradients.

Gradient descent update: change all nine parameters together

Use theta_new = theta_old - eta(dL/dtheta) with eta = 0.1. Every new value below is calculated from the same original network.

Parameter

Old

Gradient

New

w11

0.100000

-0.014277

0.101428

w12

0.200000

-0.028554

0.202855

b1

0.100000

-0.014277

0.101428

w21

-0.100000

0.006670

-0.100667

w22

0.100000

0.013339

0.098666

b2

0.000000

0.006670

-0.000667

v1

0.700000

-0.057560

0.705756

v2

-0.300000

-0.046802

-0.295320

bo

0.200000

-0.089149

0.208915

Re-run the forward pass only after all nine updates: z1_new = 0.608566, a1_new = 0.647614, z2_new = 0.095998, a2_new = 0.523981, zo_new = 0.511230, y_hat_new = 0.625095, and L_new = 0.070277.

The loss fell from 0.071761 to 0.070277. That is a sanity check for this step, not a promise for every learning rate. Notice that subtracting a negative gradient increases a parameter, so v1 rises, while the positive gradient on w22 makes it fall.

Backpropagation checks and traps that expose a wrong trace

Mistake

What breaks

Repair

Forgetting the output sigmoid derivative

delta_o is too large

Multiply by y_hat(1-y_hat)

Using updated v1 or v2 for hidden deltas

The reverse pass mixes parameter states

Use original downstream weights

Omitting bias gradients

The update is incomplete

A bias gradient equals its unit's delta

Using x2 = 1

Second-input gradients lose their factor of two

Multiply by the cached input 2

Writing z(1-z)

The sigmoid derivative is wrong

Use a(1-a)

Reversing the update sign

The step moves uphill

Use old value minus learning rate times gradient

Using the sigmoid plus binary-cross-entropy shortcut

The stated loss and derivative disagree

Differentiate the specified half squared error

Three invariants catch many mistakes: dL/dw12 = 2(dL/dw11), dL/dw22 = 2(dL/dw21), and each bias gradient equals its unit's delta. Also, delta_h2 must be positive because negative delta_o times negative v2 times a positive sigmoid derivative is positive.

For a numerical check, hold every original parameter except v1 fixed and use epsilon = 0.00001:

dL/dv1 approximately [L(v1 + epsilon) - L(v1 - epsilon)]/(2epsilon) = -0.057560

This central finite difference agrees with backpropagation to six decimal places. It is a useful debugging oracle for a tiny network, not a practical training method for a large one.

How backpropagation questions test the chain rule

Representative questions ask you to compute a missing forward value, find dL/dv1 or dL/dw12, determine a sign, identify a required cached value, compare magnitudes when an input doubles, trace a sign flip through a negative downstream weight, or perform one update.

Try these local checks:

  • If x2 changes from 2 to 0 while the same hidden delta is given, what is its incoming weight gradient? Answer:0.

  • If v2 is +0.3 while all stated forward derivatives are held fixed for a sign check, what is the sign of delta_h2? Answer: Negative.

  • If eta = 0.1 and dL/dtheta = -0.04, what is the update? Answer:theta_new = theta_old + 0.004.

For follow-up practice on wider neural-network concepts, use Artificial Neural Networks MCQs.

Backpropagation through two layers: the short version and next step

  1. Run and cache the forward pass.

  2. Start with dL/dy_hat.

  3. Multiply by the output activation derivative.

  4. Form the output parameter gradients.

  5. Send each delta through the original downstream weight and local activation derivative.

  6. Update every parameter together.

The checkpoints are y_hat = 0.621158, L = 0.071761; delta_o = -0.089149, delta_h1 = -0.014277, delta_h2 = 0.006670; and L_new = 0.070277 after the eta = 0.1 step. Now reproduce dL/dw12 = -0.028554 from the full chain and explain its sign in words.

Redraw the graph, cover the gradient labels, and derive all nine values before checking the table. If you want to place neural networks inside a broader AI and machine-learning programme, continue with AI & ML for Placements.