Derivatives in differential calculus, explained for people who survived mathematics.
Fundamentals 13 min Intermediate May 11, 2026
When a neural network trains, it adjusts millions of weights — but how does it know WHICH direction to adjust them? The answer: It computes the derivative of the loss function. The derivative measures how much the error changes when a weight changes by a tiny amount.
Positive derivative: error increases — decrease the weight. Negative derivative: error decreases — increase the weight. This simple sign-check, repeated billions of times, is how AI learns.
Slope: From Average to Instantaneous
Derivative
AnalogyDefinition
Your car drives through mountains. The average speed over 100 km (100 km / 2 h = 50 km/h) is the average slope of the distance-time curve — the secant. The speedometer shows the speed RIGHT NOW — the tangent slope, the derivative.
Example
A real speedometer measures over a small but finite time window (milliseconds). The mathematical derivative idealizes this: h goes to exactly zero — physically impossible, but mathematically precise.
Analogy:
Your car drives through mountains. The average speed over 100 km (100 km / 2 h = 50 km/h) is the average slope of the distance-time curve — the secant. The speedometer shows the speed RIGHT NOW — the tangent slope, the derivative.
Example
A real speedometer measures over a small but finite time window (milliseconds). The mathematical derivative idealizes this: h goes to exactly zero — physically impossible, but mathematically precise.
Definition:
The derivative f'(x) = lim(h\u21920) [(f(x+h) - f(x)) / h] measures the instantaneous rate of change of a function at a point. It is the slope of the tangent line to the graph.
Let us see how the limit works. For f(x) = x\u00B2 at x = 3: the exact derivative is f'(3) = 6. Watch how the approximation converges:
h \u2192 0 Limit = 6.0 — the exact derivative f'(3) = 2\u00B73 = 6
def f(x):
return x ** 2
x = 3
for h in [1, 0.1, 0.01, 0.001, 0.0001]:
slope = (f(x + h) - f(x)) / h
print(f"h = {h:8.4f} -> slope = {slope:.4f}")
# h = 1.0000 -> slope = 7.0000
# h = 0.1000 -> slope = 6.1000
# h = 0.0100 -> slope = 6.0100
# h = 0.0010 -> slope = 6.0010
# h = 0.0001 -> slope = 6.0001 (converges to 6)
Misconception: The Derivative IS the Slope
Almost: The derivative is a FUNCTION f'(x) that gives the slope at every point. At a specific point x = 3, f'(3) = 6 is the slope there. The derivative maps every input to its corresponding slope.
The loss function L depends on weights w. The derivative dL/dw tells you: "If I increase this weight by a tiny amount, does the error go up or down — and by how much?" This is exactly the information gradient descent uses.
Differentiation Rules: The Toolkit
Instead of computing the limit by hand every time, four rules cover almost all AI-relevant derivatives. Like recipes: you grab ready-made flour instead of grinding grain.
The derivative has many notations: f'(x) (Lagrange), dy/dx (Leibniz), df/dx (explicit), \u2202f/\u2202x (partial derivative). All mean the same thing for functions of one variable. You will need the \u2202 notation starting with the next article (Partial Derivatives), when functions depend on multiple variables.
Deep Dive: Autograd — Why You Don't Compute Derivatives by Hand
PyTorch and TensorFlow compute all derivatives automatically via Autograd. You define a function, call .backward(), and the framework delivers the gradients. Yet understanding the concept is essential: when training does not converge, you need to know WHY the gradient is too large, too small, or zero.
import torch
# Weight with gradient tracking
w = torch.tensor([5.0], requires_grad=True)
# Compute loss
loss = (w - 3) ** 2 + 1 # L(5) = 5
# Compute gradient automatically
loss.backward()
print(w.grad) # tensor([4.]) -> L'(5) = 4
Interactive: Compare Function Growth
The power rule reveals: the higher the exponent, the faster the function grows. Move the slider and observe how f(x) = x² and f(x) = x³ pull away from f(x) = x as x increases. The derivative measures exactly this difference in growth — f'(x) = 2x for x² grows twice as fast as f'(x) = 1 for x.
150
f(x) = 11
f(x) = x100
f(x) = x²10.000
f(x) = x³1.000.000
Moderate Input
At n=100, the difference becomes visible: O(n²) requires 10.000 operations, while O(n) needs only 100. O(log n) needs just 6.6 — that's 15x less than O(n).
Ratio to O(n)
Complexity
Operations
Factor vs. O(n)
f(x) = 1
1
100x faster
f(x) = x
100
1x (Reference)
f(x) = x²
10.000
100x slower
f(x) = x³
1.000.000
10000x slower
Finding Minima: Why Derivatives Power AI
You now know how to compute derivatives and what their sign means. The decisive question: where is the error smallest?
Function Minimum
AnalogyDefinition
You stand on a hill in dense fog and want to reach the valley (minimum). You cannot see the valley, but you can feel which direction the ground slopes downward under your feet (= derivative). You walk in the direction of steepest descent. Step by step, you approach the valley.
Example
A real hill has maybe 3 valleys. A neural network's loss landscape has millions of dimensions with countless valleys of varying depth — unimaginably complex.
Analogy:
You stand on a hill in dense fog and want to reach the valley (minimum). You cannot see the valley, but you can feel which direction the ground slopes downward under your feet (= derivative). You walk in the direction of steepest descent. Step by step, you approach the valley.
Example
A real hill has maybe 3 valleys. A neural network's loss landscape has millions of dimensions with countless valleys of varying depth — unimaginably complex.
Definition:
A local minimum exists where f'(x) = 0 (tangent is horizontal) and the curve bends upward at that point (bowl shape). f'(x) = 0 alone is not sufficient — it could also be a maximum (hilltop) or a saddle point.
Misconception: f'(x) = 0 Always Means Minimum
Not necessarily. f'(x) = 0 only means the tangent is horizontal — the terrain is flat here. It could be a valley (minimum), a hilltop (maximum), or a saddle point. In deep neural networks, saddle points are more common than true local minima — they can slow down or stall training.
Preview: Gradient Descent
The simplest form of training: start at a random weight, compute the derivative, take a small step in the opposite direction. Repeat.
Gradient Descent Rule
w_new = w_old - learning_rate × L'(w_old)
# Simplest Gradient Descent
def L(w):
return (w - 3) ** 2 + 1
def L_prime(w):
return 2 * w - 6
w = 0.0
lr = 0.1 # learning rate
for step in range(20):
grad = L_prime(w)
w = w - lr * grad
if step % 5 == 0:
print(f"Step {step:2d}: w={w:.4f} L={L(w):.4f} L'={grad:+.4f}")
# Step 0: w=0.6000 L=6.7600 L'=-6.0000
# Step 5: w=2.8024 L=1.0390 L'=-0.3277
# Step 10: w=2.9893 L=1.0001 L'=-0.0178
# Step 15: w=2.9994 L=1.0000 L'=-0.0010
# -> w converges to 3.0 (the true minimum)
Training a neural network means: minimizing the loss function by adjusting weights. The derivative of loss with respect to each weight points toward the minimum. Gradient descent formalizes this: w_new = w_old - learning_rate \u00D7 gradient. The next articles (Gradient, Chain Rule) complete the picture for real networks.
Key Takeaways
The derivative f'(x) gives the instantaneous rate of change — how steeply the function rises or falls at one specific point. The limit definition (h \u2192 0) turns average slope into exact slope.
Four simple rules (power, constant, sum, scaling) are enough to differentiate any polynomial. In AI, the derivative of the loss function tells you whether to increase or decrease a weight.
Setting f'(x) = 0 finds candidates for extrema — but not every zero is a minimum. It could also be a maximum or a saddle point. Real neural network loss landscapes have many local minima and saddle points — not just one clean valley.
Learning Goals
Given f(x) = 3x\u00B2 + 2, what is f'(4)?
For L(w) = w\u00B2 - 4w + 5, where is the minimum?
Why doesn't f'(x) = 0 guarantee a minimum?
Quiz: Derivatives
Question 1 / 4
Not completed
What does the sign of the derivative L'(w) tell you about the loss function?
1. What does the sign of the derivative L'(w) tell you about the loss function?
☐ A) Whether the function has a minimum
☐ B) Whether the error increases or decreases when w increases
☐ C) The exact value of the minimum
☐ D) How many parameters the network has
2. You are training a network. At the current weight w, L'(w) = -3. What should gradient descent do?
☐ A) Keep w unchanged
☐ B) Decrease w (follow the negative sign)
☐ C) Increase w (negative derivative means loss is falling, so keep going in this direction)
☐ D) Set w to 3
3. Given f(x) = x\u00B2 - 6x + 10. Apply the power rule and sum rule. What is f'(x)?
☐ A) x\u00B2 - 6
☐ B) 2x - 6
☐ C) 2x + 10
☐ D) x - 6
4. Gradient descent lands at a point where L'(w) = 0, but the loss is not the smallest possible value. What happened?
☐ A) The learning rate was too high
☐ B) The algorithm found a local minimum or saddle point instead of the global minimum