Loss and derivatives
A loss function gives a numerical penalty for a prediction or decision. A derivative tells us how quickly that penalty changes near the current parameter value. For a fictional sensor that should read 3 units, let w be its adjustable prediction and define L(w) = (w − 3)². At w = 1, the loss is 4 and the derivative is −4. Increasing w a little reduces the loss. No machine learning library is required to explore this example.
A function maps inputs to outputs
A function assigns an output to each permitted input. Here the input is a real-valued parameter w, not a dataset row. The output is a squared error. At w = 3, L = 0; at w = 2 or 4, L = 1. Squaring removes the sign of the error and penalizes larger absolute errors more strongly. If w is measured in sensor units, L has squared sensor units. The derivative has loss units per sensor unit. A training objective may aggregate losses across examples and include extra penalties; it is a design choice, not proof of usefulness.
From average slope to derivative
For a nonzero change h, the average slope is (L(w+h) − L(w))/h. Expanding the square gives 2(w−3) + h. As h approaches zero this tends to 2(w−3), the derivative L′(w). At w = 1 it is −4; at w = 4 it is 2. A negative derivative means the function decreases for a sufficiently small positive change, and a positive derivative means it decreases for a sufficiently small negative change. It does not describe what happens after an arbitrarily large jump.
def loss(w): return (w - 3) ** 2
def slope(w): return 2 * (w - 3)
for w in (1, 3, 4): print(w, loss(w), slope(w))Output: 1 4 -4, 3 0 0, 4 1 2. Each row is parameter, loss, derivative. Notice that a loss can be positive even when its slope is negative. At w = 3 the derivative vanishes, and the square is nonnegative everywhere, so this is the global minimum. A local minimum is no worse than nearby points, while a global minimum is no worse than any permitted point. A zero derivative alone does not establish a minimum: −w² has a zero derivative at its maximum w = 0.
Approximate a slope numerically
The centered difference compares values on both sides of w and divides by the total distance 2h. It is exact in real arithmetic for this quadratic, but generally only an approximation. Floating-point rounding can dominate when h is extremely small. Choose a reasonable positive h, compare estimates across several h values and use a known analytic derivative for checks when possible. The following bounded example takes finite small numeric inputs.
def loss(w): return (w - 3) ** 2
def centered_slope(w, h): if h <= 0: raise ValueError("Step must be positive") return (loss(w + h) - loss(w - h)) / (2 * h)
print(round(centered_slope(1, 0.125), 6))print(round(centered_slope(4, 0.125), 6))Output: -4.0 and 2.0. The rounded estimates agree with the analytic derivatives at 1 and 4. Rounding is for display; a numerical estimate is not a proof of differentiability.
Partial derivatives and the gradient
For two parameters consider J(w,b) = (w+b−3)² + b². The partial derivative with respect to w treats b as fixed: 2(w+b−3). The partial derivative with respect to b treats w as fixed: 2(w+b−3) + 2b. The gradient is the vector of these partial derivatives in parameter order. At (1,1) it is (−2,0). For differentiable functions in ordinary Euclidean coordinates, a nonzero gradient points in the direction of steepest local increase per unit distance. The negative gradient points toward local decrease. A zero gradient can also occur at a saddle or maximum.
Practice and common mistakes
For the one-parameter loss, compute L(0) and L′(0), then predict which direction a small step should take. Answer: 9, −6, increase w. For the two-parameter objective at (2,0), both partial derivatives are −2. Do not confuse derivative with loss, or compute partial derivatives by changing both coordinates together. Next you will choose a step size and check whether the objective actually improves.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Loss and derivatives — understanding check
Apply calculus and optimization intuition to bounded synthetic examples.
At w = 1, what are L(w) and L′(w) for L(w) = (w−3)²?
For L(w) = (w − 3)², what does L′(1) = −4 imply?
Which function has a stationary maximum at w = 0?
What denominator does the centered difference use for points w−h and w+h?
What is the gradient of J(w,b) = (w+b−3)² + b² at (1,1)?