Understanding derivatives is the key to understanding how neural networks learn. Every time a model improves, calculus is working behind the scenes. This guide covers exactly what you need to understand AI optimizationβexplained intuitively with real examples.
Machine learning is fundamentally about optimizationβfinding the best possible parameters for a model. Imagine you're blindfolded on a hilly landscape, trying to find the lowest valley. How do you know which direction to walk?
Calculus answers this question. Specifically, derivatives tell us the slope of the ground beneath our feet. If we're on a slope going up to the right, we should walk left. If it's going up to the left, we walk right. This simple ideaβ"follow the downhill slope"βis the core of how neural networks learn.
Every neural network has millions (or billions!) of parameters. During training, the model computes how changing each parameter would affect its errors. This computation uses derivatives. The famous "backpropagation" algorithm is essentially clever application of the chain rule to compute all these derivatives efficiently.
Think of training a neural network as adjusting millions of knobs on a massive control panel. Each knob affects the output. Calculus tells us: "Turn this knob clockwise to reduce the error" or "Turn that knob counterclockwise." Without calculus, we'd be randomly guessing which way to turn each knob.
A derivative measures how fast something changes. If you have a function f(x), the derivative f'(x) tells you the rate of change at any point x.
In plain English: take a tiny step h, see how much f changes, divide by the step size. As h gets infinitely small, you get the instantaneous rate of change.
Imagine driving a car. Your position changes over time. The derivative of position is velocity (how fast position changes). The derivative of velocity is acceleration (how fast velocity changes). Derivatives describe "how fast something is changing."
f(x)
β β± β Derivative positive (increasing)
β β±
β β±
β β’ β Derivative = 0 (peak or valley)
β β±
β β± β Derivative negative (decreasing)
β β±
βββββββββββββββββ x
The derivative describes the slope at each point on the curve
For the function f(x) = xΒ², let's compute f'(2):
f(x) = xΒ²
f(2) = 4
f(2.001) = 4.004001
Approximate derivative:
f'(2) β (4.004001 - 4) / 0.001 = 4.001
Exact answer: f'(x) = 2x, so f'(2) = 4
At x = 2, the function xΒ² is increasing at a rate of 4 units of output per 1 unit of input.
Rather than computing limits every time, mathematicians have derived rules for common functions. These rules let you quickly compute derivatives of any combination of basic functions.
d/dx[xΒ³] = 3xΒ²
d/dx[xΒ²] = 2x
d/dx[x] = 1 Β· xβ° = 1
d/dx[xβ»ΒΉ] = -1 Β· xβ»Β² = -1/xΒ²
d/dx[βx] = d/dx[x^Β½] = Β½ Β· x^(-Β½) = 1/(2βx)
This makes senseβa constant doesn't change, so its rate of change is zero.
d/dx[xΒ³ + 2xΒ² - 5x + 7]
= d/dx[xΒ³] + d/dx[2xΒ²] - d/dx[5x] + d/dx[7]
= 3xΒ² + 4x - 5 + 0
= 3xΒ² + 4x - 5
Think of it as: "Keep one function the same, differentiate the other, then switch roles and add."
d/dx[xΒ² Β· sin(x)]
= (2x) Β· sin(x) + xΒ² Β· cos(x)
= 2xΒ·sin(x) + xΒ²Β·cos(x)
d/dx[e^x] = e^x
d/dx[ln(x)] = 1/x
d/dx[sin(x)] = cos(x)
d/dx[cos(x)] = -sin(x)
d/dx[sigmoid(x)] = Ο(x)Β·(1-Ο(x))
d/dx[ReLU(x)] = 1 if x>0, else 0
d/dx[tanh(x)] = 1 - tanhΒ²(x)
d/dx[softplus(x)] = sigmoid(x)
Activation functions like ReLU, sigmoid, and tanh are used in every neural network. Their derivatives determine how gradients flow during training. ReLU is popular partly because its derivative is simple (0 or 1), making training efficient. Sigmoid's derivative can get very small (near 0 or 1), causing "vanishing gradients"βa real problem in deep networks.
So far, we've looked at functions with one input. But neural networks have millions of parameters! Partial derivatives let us handle functions with multiple inputs by looking at how the output changes with respect to one variable at a time, while treating others as constants.
To find βf/βx: treat all other variables as constants and differentiate normally with respect to x.
f(x, y) = xΒ² + 3xy + yΒ²
To find βf/βx:
- Treat y as a constant
- d/dx[xΒ²] = 2x
- d/dx[3xy] = 3y (y is constant, so 3xy is "3y times x")
- d/dx[yΒ²] = 0 (yΒ² is a constant w.r.t. x)
- βf/βx = 2x + 3y
To find βf/βy:
- Treat x as a constant
- d/dy[xΒ²] = 0
- d/dy[3xy] = 3x
- d/dy[yΒ²] = 2y
- βf/βy = 3x + 2y
The gradient bundles all partial derivatives into a single vector. It points in the direction of steepest increase.
For f(x, y) = xΒ² + 3xy + yΒ²:
βf = [βf/βx, βf/βy] = [2x + 3y, 3x + 2y]
At point (1, 2):
βf(1,2) = [2(1) + 3(2), 3(1) + 2(2)]
= [8, 7]
This vector [8, 7] points in the direction of
steepest increase from point (1, 2).
Gradient descent is the core algorithm for training neural networks. The idea is simple: to minimize a function, move opposite to the gradient (downhill).
Initialize your model's weights (often randomly).
Calculate βL with respect to all parameters, where L is the loss function.
Move in the opposite direction: w_new = w_old - Ξ· Β· βL, where Ξ· is the learning rate.
Continue until the loss stops decreasing (convergence).
Loss Surface (2D visualization)
High Loss
β±β²
β± β²
β± β²
β± β²
β± β’ ββββββ Starting point
β± β
β± β ββββββ Gradient points uphill,
β± β’ we move OPPOSITE (downhill)
β± β
β± β
β± β’ ββββββ Getting closer to minimum
β± β
β± β
ββββββ Minimum (goal!)
ββββββββββββββββββββ
Parameters (weights)
Gradient descent follows the downhill direction to find the minimum loss
In a neural network with 1 billion parameters, we compute 1 billion partial derivativesβone for each parameter. This tells us how to adjust each weight to reduce the error. Modern frameworks like PyTorch and TensorFlow do this automatically using "autograd" (automatic differentiation).
Learning rate too high: Steps are too big, you might "jump over" the minimum.
Learning rate too low: Training takes forever, might get stuck.
Local minima: You might find a valley that's not the deepest (though this is
less problematic in high dimensions than you might think).
The chain rule is arguably the most important calculus concept for AI. It tells us how to compute derivatives of composed functionsβfunctions inside functionsβwhich is exactly what a neural network is!
If x affects y, and y affects z, then to find how
x affects z, multiply the effects together:
(rate z changes per y) Γ (rate y changes per x) = (rate z changes per x)
Find the derivative of h(x) = (3x + 1)Β²
Step 1: Identify inner and outer functions
Inner: g(x) = 3x + 1
Outer: f(u) = uΒ²
Step 2: Find their derivatives
g'(x) = 3
f'(u) = 2u
Step 3: Apply chain rule
h'(x) = f'(g(x)) Β· g'(x)
= 2(3x + 1) Β· 3
= 6(3x + 1)
= 18x + 6
When functions have multiple inputs and outputs, the chain rule extends naturally. This is exactly what happens in neural networks.
A neural network is just a composition of functions: input β layer 1 β layer 2 β ... β output β loss. Backpropagation computes derivatives by applying the chain rule from output back to input.
Let's trace through backprop with real numbers before introducing symbols:
Simple network: y = 2x, then square it β output = (2x)Β²
Input: x = 3
FORWARD PASS (calculate step by step):
Step 1: z = 2 Γ 3 = 6
Step 2: output = 6Β² = 36
BACKWARD PASS (how does changing x affect the output?):
If x increases by 1 β z increases by 2 (because z = 2x)
If z increases by 1 β output increases by 12 (derivative of zΒ² at z=6 is 2Γ6=12)
Combined: xβ1 β zβ2 β outputβ24
So: changing x by 1 changes output by 24. That's the gradient!
Check: d/dx[(2x)Β²] = d/dx[4xΒ²] = 8x = 8Γ3 = 24 β
That's all backpropagation is: multiply how much each step affects the next, backwards through the chain.
FORWARD PASS (compute output):
βββββββββββββββββββββββββββββββββββββββββββββββββ
Input Layer 1 Layer 2 Output Loss
x β zβ β zβ β Ε· β L
Wβ Wβ Wβ
BACKWARD PASS (compute gradients via chain rule):
βββββββββββββββββββββββββββββββββββββββββββββββββ
βL/βWβ = βL/βΕ· Β· βΕ·/βzβ Β· βzβ/βzβ Β· βzβ/βWβ
βββββ ββββββ ββββββ ββββββββ
β β β β
βββββββββ΄βββββββββ΄ββββββββββ
Multiply these "local gradients"
Backpropagation applies the chain rule layer by layer
Compute the output by feeding input through all layers. Store intermediate values.
Compare output to target, compute loss (e.g., squared error, cross-entropy).
Starting from the loss, compute gradients layer by layer using the chain rule.
Use gradient descent to adjust all weights in the direction that reduces loss.
Network: x β [Linear] β [ReLU] β [Linear] β Ε·
wβ wβ
Forward:
zβ = wβ Β· x (linear layer 1)
aβ = ReLU(zβ) (activation)
Ε· = wβ Β· aβ (linear layer 2)
L = (Ε· - y)Β² (squared error loss)
Backward (chain rule):
βL/βΕ· = 2(Ε· - y)
βΕ·/βwβ = aβ
βΕ·/βaβ = wβ
βaβ/βzβ = 1 if zβ > 0, else 0 (ReLU derivative)
βzβ/βwβ = x
Final gradients:
βL/βwβ = βL/βΕ· Β· βΕ·/βwβ = 2(Ε·-y) Β· aβ
βL/βwβ = βL/βΕ· Β· βΕ·/βaβ Β· βaβ/βzβ Β· βzβ/βwβ
= 2(Ε·-y) Β· wβ Β· ReLU'(zβ) Β· x
Computing gradients naively would require O(nΒ²) operations for n parameters. Backpropagation is clever: it reuses intermediate computations from the forward pass, achieving O(n) complexity. This efficiency is why we can train billion-parameter models. Modern frameworks (PyTorch, TensorFlow, JAX) implement this automaticallyβyou just define the forward pass, and gradients are computed for free!
Test your understanding with these problems. Try to solve them before revealing the solutions!
d/dx[3xβ΄ - 2xΒ² + 5x - 7]
= 3Β·4xΒ³ - 2Β·2x + 5Β·1 - 0
= 12xΒ³ - 4x + 5
Using chain rule with:
Outer: f(u) = sin(u), f'(u) = cos(u)
Inner: g(x) = xΒ², g'(x) = 2x
d/dx[sin(xΒ²)] = cos(xΒ²) Β· 2x = 2xΒ·cos(xΒ²)
βf/βx: treat y as constant
β/βx[xΒ²y] = 2xy
β/βx[3xyΒ²] = 3yΒ²
βf/βx = 2xy + 3yΒ²
βf/βy: treat x as constant
β/βy[xΒ²y] = xΒ²
β/βy[3xyΒ²] = 6xy
βf/βy = xΒ² + 6xy
βf/βx = 2x
βf/βy = 2y
βf = [2x, 2y]
βf(3,4) = [6, 8]
This points away from the origin (uphill).
To minimize f, move in direction [-6, -8].
With these concepts, you have the calculus foundation needed for the AI course. You'll encounter these ideas throughout: