Derivatives and slopes
Quick answer
A derivative measures the instantaneous rate at which a scalar function changes with its input. In machine learning, a derivative can tell us how a loss changes when one parameter moves. For a smooth loss L(w), a positive derivative means a sufficiently small increase in w raises the loss locally; it does not guarantee the same behavior far away.
From secant to tangent
A secant slope compares two inputs: (L(w+h) − L(w))/h. The derivative is the limit of this ratio as nonzero h approaches zero, if that limit exists. For L(w) = (w−3)², expanding gives 2(w−3)+h. At w=5 the limiting slope is 4. The example uses three positive steps; the formula also holds for negative steps.
def loss(w): return (w - 3)**2for h in [1, 0.5, 0.125]: print((loss(5+h)-loss(5))/h)Expected output:
5.04.54.125The average slope approaches 4 as the interval shrinks. A derivative is not the ratio at h=0, which would divide by zero.
Use the derivative to interpret a loss
The power rule gives d(w²)/dw = 2w; the shift by 3 gives L′(w)=2(w−3). A derivative has units of output per unit input. For this loss, negative slope to the left of 3 and positive slope to the right indicate a minimum at 3.
for w in [1,3,5]: print(w, (w-3)**2, 2*(w-3))Expected output:
1 4 -43 0 05 4 4At w=1, a small increase reduces loss; at w=5, a small decrease reduces it. Zero slope at 3 does not imply that every zero slope of every function is a minimum.
Common mistakes
Do not confuse the value L(5)=4 with its derivative L′(5)=4: their equality here is coincidental. A horizontal tangent alone does not prove a minimum; f(w)=w³ has derivative zero at w=0 but increases through it. A limit can fail to exist, as at the corner of |w|.
Practice and next steps
For L(w)=(w−4)², calculate the derivative at w=1 and w=6. Explain which direction a small adjustment should take. Next, learn how to handle multiple parameters.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Derivatives and slopes — knowledge check
Apply calculus and differentiation to small original examples.
What does the derivative measure?
What is the slope of (w−3)² at w=1?
What is the difference quotient at h=0?
Which statement about a zero derivative is valid?
If a smooth loss has positive slope at w, which sufficiently small change reduces loss?