PythiloLesson 6 of 15 · progress kept in this browser

The chain rule

A network is a chain of functions: a product, then an activation, then a loss. The chain rule says the derivative of a chain is the product of the derivatives of its links, each taken at the value that flowed into it.

For L = (s - y) ** 2 with s = sigmoid(z) and z = w * x, the links are dL/ds = 2 (s - y), ds/dz = s (1 - s) and dz/dw = x. Multiply them.

import math

def sigmoid(z):
    return 1 / (1 + math.exp(-z))

w, x, y = 0.5, 2.0, 1.0
z = w * x
s = sigmoid(z)
dL_ds = 2 * (s - y)
ds_dz = s * (1 - s)
dz_dw = x
print(dL_ds * ds_dz * dz_dw)

Compute forward first and keep every intermediate value, then multiply the local derivatives on the way back. That order, forward then backward, is exactly what backpropagation does at scale.

Your task

Write sigmoid(z) and loss_and_grad(w, x, y) returning the pair (L, dL_dw) as floats for L = (sigmoid(w * x) - y) ** 2, using the chain rule.