Binary cross-entropy loss
L=−[y^ln(p)+(1−y^)ln(1−p)] The loss L compares our prediction p with the ground truth y^∈{0,1}. We want to find how changing each weight changes the loss.
Start with one output weight
z=w11(4)a1(3)+w12(4)a2(3)+b1(4),p=σ(z) Follow w11(4)→z→p→L. The chain rule gives:
∂w11(4)∂L=∂p∂L⋅∂z∂p⋅∂w11(4)∂z Calculate each derivative
∂p∂L=−py^+1−p1−y^=p(1−p)p−y^ p=1+e−z1⟹∂z∂p=(1+e−z)2e−z=p(1−p) ∂w11(4)∂z=a1(3) The incoming activations, other output weight, and bias are held fixed when differentiating with respect to this weight.
Put them together
∂w11(4)∂L=(−py^+1−p1−y^)⋅p(1−p)⋅a1(3)=[−y^(1−p)+(1−y^)p]a1(3)=(p−y^)a1(3) One layer back: the first weight in hidden layer 3
z1(3)a1(3)=w11(3)a1(2)+w12(3)a2(2)+w13(3)a3(2)+b1(3)=f(z1(3)) Follow w11(3)→z1(3)→a1(3)→z→p→L.
∂w11(3)∂L=∂p∂L⋅∂z∂p⋅∂a1(3)∂z⋅∂z1(3)∂a1(3)⋅∂w11(3)∂z1(3) The three new derivatives
∂a1(3)∂z∂z1(3)∂a1(3)∂w11(3)∂z1(3)=w11(4)=f′(z1(3))=a1(2) f′(z1(3)) is the derivative of the activation function at this neuron's pre-activation.
Put them together
∂w11(3)∂L=p(1−p)p−y^⋅p(1−p)⋅w11(4)⋅f′(z1(3))⋅a1(2)=(p−y^)w11(4)f′(z1(3))a1(2) One more layer back: the first weight in hidden layer 2
G₁ follows the upper path; G₂ follows the lower path. Both share the steps before and after the branch. Other inputs and biases are omitted.Multiply the derivatives along each path, then add. G1 and G2 are the two contributions to the same weight's gradient.
Path 1 · through neuron 1
G1=∂p∂L⋅∂z∂p⋅∂a1(3)∂z⋅∂z1(3)∂a1(3)⋅∂a1(2)∂z1(3)⋅∂z1(2)∂a1(2)⋅∂w11(2)∂z1(2)=p(1−p)p−y^⋅p(1−p)⋅w11(4)⋅f′(z1(3))⋅w11(3)⋅f′(z1(2))⋅a1(1)=(p−y^)w11(4)f′(z1(3))w11(3)f′(z1(2))a1(1) Path 2 · through neuron 2
G2=∂p∂L⋅∂z∂p⋅∂a2(3)∂z⋅∂z2(3)∂a2(3)⋅∂a1(2)∂z2(3)⋅∂z1(2)∂a1(2)⋅∂w11(2)∂z1(2)=p(1−p)p−y^⋅p(1−p)⋅w12(4)⋅f′(z2(3))⋅w21(3)⋅f′(z1(2))⋅a1(1)=(p−y^)w12(4)f′(z2(3))w21(3)f′(z1(2))a1(1) Add both contributions
∂w11(2)∂L=G1+G2