Deep Learning · MT2 · section
Drill 12 Two output sigmoid backpropagation
MT2 numerical practice from direct to mixed
Question
Question. Input x = [0.2, 0.4]. Each row below lists the incoming weights of one receiving neuron. W₁ is shown below; b₁ = [0.1, 0.1]; W₂ is shown below; b₂ = [0, 0.1]. Hidden and output activations are sigmoid. Target y = [1, 0]. Learning rate η = 0.2. Use half summed squared loss and update all parameters.
Answer — step by step
Given
Input x = [0.2, 0.4].
W₁ is printed with the question.
b₁ = [0.1, 0.1].
W₂ is printed with the question.
b₂ = [0, 0.1].
Hidden and output activations are sigmoid.
Target y = [1, 0].
Learning rate η = 0.2.
Use half summed squared loss and update all parameters.
To find
Find all preactivations, hidden activations and final outputs. Calculate the half summed squared loss and its output derivatives. Find the sigmoid output and, if requested, its derivative. Find the requested deltas and parameter gradients, then update the specified parameters.
Formula — which one and why
Forward propagation. Evaluate one layer completely before supplying its activated outputs to the next layer. Each layer has its own weights and biases.
x = input vector; h = hidden outputs; W₁,b₁ = hidden parameters; W₂,b₂ = output parameters; f₁,f₂ = the stated activations.
z_h=W_1x+b_1h=f_1(z_h)z_o=W_2h+b_2o=f_2(z_o)Yaad rakhne ka cue: Pehli layer ka activated output hi agli layer ka input hai.
Half summed squared loss. This loss convention takes one-half of the sum of squared prediction errors. Use it when the question specifies it, or explicitly state it as an assumption when the paper omits the loss. Differentiating cancels the factor one-half against the square derivative.
oⱼ = output j; yⱼ = target j; L = half summed squared loss under the stated or explicitly assumed convention.
L=\frac12\sum_j(o_j-y_j)^2\frac{\partial L}{\partial o_j}=o_j-y_jYaad rakhne ka cue: Half aur square ka 2 cancel: output derivative = prediction − target.
Sigmoid and its slope. Use sigmoid when the neuron is specified to squash its preactivation into (0,1). Once its output is known, its derivative needs only output × (1−output).
z = preactivation; p = sigmoid output; e is the exponential base, not a prediction error in this formula.
p=\frac{1}{1+e^{-z}}\frac{dp}{dz}=p(1-p)Yaad rakhne ka cue: Sigmoid output p; uska slope p × (1−p).
Backpropagation. The loss tells us how each output must change. Multiply by that neuron’s activation slope to obtain its delta. A weight gradient is receiving delta × sending value. Hidden deltas must include every outgoing path.
δ (delta) = derivative of loss with respect to a neuron’s preactivation; f′ = activation derivative; aᵢ = sending activation; Wⱼᵢ = connection weight. Use the exact loss scaling in the question.
\delta_{o_j}=\frac{\partial L}{\partial o_j}f_o\prime(z_{o_j})\delta_{h_i}=f_h\prime(z_{h_i})\sum_jW_{ji}^{(2)}\delta_{o_j}\frac{\partial L}{\partial W_{ji}}=\delta_ja_i\frac{\partial L}{\partial b_j}=\delta_jW_{ji,\mathrm{new}}=W_{ji}-\eta\frac{\partial L}{\partial W_{ji}}Yaad rakhne ka cue: Receiving delta × sending value = us connection ka gradient.
Solution — step by step
Formulas used before substituting numbers
For every receiving neuron, z is its weighted sum and a is its activated output. The half squared loss sums the errors from all output neurons.
z_j=\sum_iw_{ji}a_i+b_j,\quad a_j=f(z_j),\quad L=\frac12\sum_k(o_k-y_k)^2Delta means the loss gradient with respect to a neuron's raw score. First calculate output deltas, then hidden deltas using every connected output path.
\delta_{o_k}=(o_k-y_k)f'(z_{o_k})\delta_{h_j}=\left(\sum_kw_{kj}\delta_{o_k}\right)f'(z_{h_j})The gradient of a connection is its receiving delta multiplied by its sending activation. A bias receives the constant 1.
\frac{\partial L}{\partial w_{ji}}=\delta_j a_i,\qquad \frac{\partial L}{\partial b_j}=\delta_jw_{ji,\mathrm{new}}=w_{ji}-\eta\delta_j a_i,\quad b_{j,\mathrm{new}}=b_j-\eta\delta_jFor sigmoid, use f′ = a(1 − a). For ReLU, use f′ = 1 for a positive raw score and 0 for a negative raw score. Store the old weights until all gradients have been calculated.
Step 1 map the arrows. Each row of W₁ receives all inputs; each row of W₂ receives all hidden outputs. Bias is added once per receiving neuron. All gradients below use the OLD weights from this same forward pass.
Hidden neuron 1:
z_{h_1} = (0.1 \times 0.2) + (0.2 \times 0.4) + (0.1) = 0.2h_{1}=\frac{1}{1+e^{-(0.2)}}=0.549833997Hidden neuron 2:
z_{h_2} = (-0.2 \times 0.2) + (0.3 \times 0.4) + (0.1) = 0.18h_{2}=\frac{1}{1+e^{-(0.18)}}=0.544878892Output neuron 1:
z_{o_1} = (0.2 \times 0.549833997) + (-0.1 \times 0.544878892) + (0) = 0.05547891o_{1}=\frac{1}{1+e^{-(0.05547891)}}=0.513866171Output neuron 2:
z_{o_2} = (0.3 \times 0.549833997) + (0.4 \times 0.544878892) + (0.1) = 0.482901756o_{2}=\frac{1}{1+e^{-(0.482901756)}}=0.618432848Step 2 loss convention. This worked solution uses half summed squared loss, L = ½Σ(o − y)². Follow the loss specified in the question; where none is supplied, this is an explicit assumption.
Prediction minus target = [-0.486133829, 0.618432848]. Squared errors = [0.2363261, 0.382459187]. Half-sum squared loss = 0.309392643; mean squared error = 0.309392643. These are different unless the number of output components makes the scale coincide.
Output delta 1:
\delta_{o_1} = 1 \times (0.513866171-1) \times 0.249807729 = -0.121439988The derivative is o(1−o).
Output delta 2:
\delta_{o_2} = 1 \times (0.618432848-0) \times 0.235973661 = 0.145933863The derivative is o(1−o).
Step 3 output weights. Gradient = receiving delta × sending activation. An output bias gradient equals its delta.
h1 → o1:
Subtract η × gradient = −0.013354367. This weight increases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies its activation.
g_{1,1}^{(2)}=\frac{\partial L}{\partial W^{(2)}_{1,1}}=\delta_{o_1}h_1g_{1,1}^{(2)}=(-0.121439988)\times(0.549833997)\approx -0.066771834Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(2)}_{1,1,\mathrm{new}}=0.2-0.2(-0.066771834)\approx 0.213354367h2 → o1:
Subtract η × gradient = −0.013234017. This weight increases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies its activation.
g_{1,2}^{(2)}=\frac{\partial L}{\partial W^{(2)}_{1,2}}=\delta_{o_1}h_2g_{1,2}^{(2)}=(-0.121439988)\times(0.544878892)\approx -0.066170086Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(2)}_{1,2,\mathrm{new}}=-0.1-0.2(-0.066170086)\approx -0.086765983h1 → o2:
Subtract η × gradient = 0.01604788. This weight decreases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies its activation.
g_{2,1}^{(2)}=\frac{\partial L}{\partial W^{(2)}_{2,1}}=\delta_{o_2}h_1g_{2,1}^{(2)}=(0.145933863)\times(0.549833997)\approx 0.080239399Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(2)}_{2,1,\mathrm{new}}=0.3-0.2(0.080239399)\approx 0.28395212h2 → o2:
Subtract η × gradient = 0.015903256. This weight decreases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies its activation.
g_{2,2}^{(2)}=\frac{\partial L}{\partial W^{(2)}_{2,2}}=\delta_{o_2}h_2g_{2,2}^{(2)}=(0.145933863)\times(0.544878892)\approx 0.079516282Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(2)}_{2,2,\mathrm{new}}=0.4-0.2(0.079516282)\approx 0.384096744Step 4 hidden deltas. A hidden unit can influence EVERY connected output, so add those paths BEFORE multiplying by the hidden activation derivative.
Hidden 1 downstream contribution:
Calculate each outgoing path using the old outgoing weight and that output's delta.
Multiply within each path. Add the resulting contributions at the shared hidden neuron.
Contribution from output 1.
C_1=(0.2)\times(-0.121439988)\approx -0.024287998Contribution from output 2.
C_2=(0.3)\times(0.145933863)\approx 0.043780159Accumulate the contributions, then apply the hidden activation derivative below.
\text{Sum}=C_1+C_2\approx 0.019492161\delta_{h_1} = 0.019492161 \times 0.247516573 = 0.004824633Hidden 2 downstream contribution:
Calculate each outgoing path using the old outgoing weight and that output's delta.
Multiply within each path. Add the resulting contributions at the shared hidden neuron.
Contribution from output 1.
C_1=(-0.1)\times(-0.121439988)\approx 0.012143999Contribution from output 2.
C_2=(0.4)\times(0.145933863)\approx 0.058373545Accumulate the contributions, then apply the hidden activation derivative below.
\text{Sum}=C_1+C_2\approx 0.070517544\delta_{h_2} = 0.070517544 \times 0.247985885 = 0.017487356Step 5 input weights. Multiply the hidden delta by the corresponding raw input, not by the hidden output.
x1 → h1:
Subtract η × gradient = 0.000192985. This weight decreases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies the input value.
g_{1,1}^{(1)}=\frac{\partial L}{\partial W^{(1)}_{1,1}}=\delta_{h_1}x_1g_{1,1}^{(1)}=(0.004824633)\times(0.2)\approx 0.000964927Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(1)}_{1,1,\mathrm{new}}=0.1-0.2(0.000964927)\approx 0.099807015x2 → h1:
Subtract η × gradient = 0.000385971. This weight decreases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies the input value.
g_{1,2}^{(1)}=\frac{\partial L}{\partial W^{(1)}_{1,2}}=\delta_{h_1}x_2g_{1,2}^{(1)}=(0.004824633)\times(0.4)\approx 0.001929853Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(1)}_{1,2,\mathrm{new}}=0.2-0.2(0.001929853)\approx 0.199614029x1 → h2:
Subtract η × gradient = 0.000699494. This weight decreases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies the input value.
g_{2,1}^{(1)}=\frac{\partial L}{\partial W^{(1)}_{2,1}}=\delta_{h_2}x_1g_{2,1}^{(1)}=(0.017487356)\times(0.2)\approx 0.003497471Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(1)}_{2,1,\mathrm{new}}=-0.2-0.2(0.003497471)\approx -0.200699494x2 → h2:
Subtract η × gradient = 0.001398988. This weight decreases.
The sending value and receiving delta have different roles. All deltas still use the original forward-pass weights.
The receiving neuron supplies the delta; the sending neuron supplies the input value.
g_{2,2}^{(1)}=\frac{\partial L}{\partial W^{(1)}_{2,2}}=\delta_{h_2}x_2g_{2,2}^{(1)}=(0.017487356)\times(0.4)\approx 0.006994942Apply the learning rate to this gradient and subtract the result from the old weight.
W^{(1)}_{2,2,\mathrm{new}}=0.3-0.2(0.006994942)\approx 0.298601012Step 6 bias updates. A bias receives a constant input 1, so its gradient equals the corresponding delta. Use the same old forward pass and learning rate as the weight updates.
b_{h_1,\mathrm{new}}=0.1-0.2(0.004824633)=0.099035073b_{h_2,\mathrm{new}}=0.1-0.2(0.017487356)=0.096502529b_{o_1,\mathrm{new}}=0-0.2(-0.121439988)=0.024287998b_{o_2,\mathrm{new}}=0.1-0.2(0.145933863)=0.070813227These are independent per-neuron biases. If the question fixes biases, keep them fixed. If it specifies a single tied bias, add all deltas sharing that bias before updating it.