concept

Loss and backpropagation intuition

For scalar squared error, multiply the residual by twice the input to obtain the weight gradient. Backpropagate using unchanged parameters before the optimizer step.

Formal Syntax & Structure

artificial-intelligence syntaxartificial-intelligence
L = (w*x-y)**2; dL/dw = 2*(w*x-y)*x

Working Example

Example Snippetartificial-intelligence
def loss_and_gradient(x, y, w):    residual = w * x - y    return residual ** 2, 2 * residual * x
loss, grad = loss_and_gradient(2, 6, 1)new_w = 1 - 0.125 * gradprint(loss, grad)print(new_w, loss_and_gradient(2, 6, new_w)[0])