AI State of the Art
Math/Differential and Integral Calculus/11 min
Section 320

Derivatives and Gradients: The Geometric Intuition

Listen as a podcast →

The derivative: measuring change

Imagine you're driving a car. The speedometer reads 55 mph. What exactly is it measuring? The rate of change of your position with respect to time. If you accelerate, this rate increases. If you brake, it decreases. The derivative is exactly that: it measures how fast one quantity changes when you slightly modify another.

Mathematically, the derivative of ff at point xx is defined as:

f′(x)=lim⁡h→0f(x+h)−f(x)hf'(x) = \lim_{h \to 0} \frac{f(x + h) - f(x)}{h}

You take two very close points on the curve of ff, compute the slope of the line connecting them, and let the distance between those two points approach zero. What you get is the slope of the tangent line to the curve at point xx.

Curve f(x) with two points at abscissas x and x+dx, the grey line joining them, and the green tangent line to the curve. The horizontal gap dx and the vertical gap df are marked with arrows.
The grey line joins two points of the curve separated by dx, so its slope is df/dx. As dx shrinks, that line tips over onto the green tangent line, whose slope is the derivative at x.Source : Wikimedia Commons, Derivative with tangent.svg

The mountain analogy

Imagine you're on a mountainside, blindfolded. You can't see anything, but you can feel the slope under your feet. The derivative is that slope. It tells you two essential things:

  • Direction: is it going up or down?
  • Intensity: is the slope steep or gentle?

If the derivative is positive, the function is increasing. If it's negative, the function is decreasing. If it's zero, you're on a plateau, a peak, or a valley, a critical point.

Fundamental examples

Here are the derivatives you'll constantly encounter in ML:

The power function: f(x)=xnf(x) = x^n

f′(x)=n⋅xn−1f'(x) = n \cdot x^{n-1}

The simplest case: f(x)=x2f(x) = x^2 gives f′(x)=2xf'(x) = 2x. At x=3x = 3, the slope is 66, the function is climbing faster and faster. At x=0x = 0, the slope is 00, that's the minimum. This function appears everywhere in ML because the MSE loss (Mean Squared Error) is exactly an x2x^2: you measure the square of the error between prediction and reality.

The exponential: f(x)=exf(x) = e^x

f′(x)=exf'(x) = e^x

The exponential is its own derivative. It's a magical property that makes it ubiquitous in mathematics. In ML, it appears in the softmax function (which transforms scores into probabilities) and in many probability distributions.

The logarithm: f(x)=ln⁡(x)f(x) = \ln(x)

f′(x)=1xf'(x) = \frac{1}{x}

The logarithm grows more and more slowly. Its derivative decreases with xx. In ML, we very often work with the log-likelihood: we take the logarithm of probabilities to transform products into sums. The cross-entropy loss, used in nearly all classification, relies on ln⁡(p)\ln(p).

The sigmoid: σ(x)=11+e−x\sigma(x) = \frac{1}{1 + e^{-x}}

σ′(x)=σ(x)⋅(1−σ(x))\sigma'(x) = \sigma(x) \cdot (1 - \sigma(x))

The sigmoid squashes any real number into the interval [0,1][0, 1]. Its derivative is maximal at x=0x = 0 (where it equals 0.250.25) and tends toward 00 when xx is very large or very small. This phenomenon is called saturation: in those zones, the gradient becomes tiny, and learning slows down considerably. It's one of the historical problems of deep learning, known as the vanishing gradient.

Plot overlaying the sigmoid as a solid blue S-shaped curve between 0 and 1 and its derivative as a red dashed bell-shaped curve peaking at 0.25 at x equals 0 and near zero beyond plus or minus 5.
The red dashed curve is the derivative of the sigmoid. It peaks at 0.25 at zero and collapses toward zero once x goes beyond roughly plus or minus 5: that is the saturation zone where the gradient vanishes.Source : GeeksforGeeks, Derivative of the Sigmoid Function

From scalar to vector: the gradient

In the real life of a neural network, you never depend on a single variable. A model has thousands, even billions of parameters. You need a multidimensional version of the derivative: the gradient.

If ff depends on several variables x1,x2,…,xnx_1, x_2, \ldots, x_n, the gradient is the vector of all partial derivatives:

∇f=(∂f∂x1∂f∂x2⋮∂f∂xn)\nabla f = \begin{pmatrix} \frac{\partial f}{\partial x_1} \\ \frac{\partial f}{\partial x_2} \\ \vdots \\ \frac{\partial f}{\partial x_n} \end{pmatrix}

Each component says: "if I slightly move this parameter, how much does the function change?"

The key property of the gradient

The gradient points in the direction of steepest ascent of the function. This is a fundamental result of multivariate analysis. If you want to climb the mountain as fast as possible, follow the gradient. If you want to descend as fast as possible (and that's exactly what we want in ML, since we want to minimize the loss) go in the opposite direction of the gradient.

Gradient descent: the training algorithm

Training an ML model boils down to an optimization problem: finding the parameters θ\theta that minimize a loss function L(θ)\mathcal{L}(\theta). Gradient descent is the simplest algorithm to get there:

θt+1=θt−η⋅∇L(θt)\theta_{t+1} = \theta_t - \eta \cdot \nabla \mathcal{L}(\theta_t)

At each step:

  1. Compute the gradient: in which direction does the loss increase the most?
  2. Take a step in the opposite direction: update the parameters to reduce the loss
  3. Repeat until convergence

The parameter η\eta is called the learning rate. It's the size of the step you take at each iteration.

The learning rate: not too big, not too small

  • Too small: convergence is extremely slow. You're taking ant steps on the mountain.
  • Too big: you oscillate around the minimum without ever reaching it, or worse, you diverge. You're taking giant leaps and jumping over the valley.
  • Just right: you descend quickly and converge cleanly to the minimum.

In practice, choosing the learning rate is one of the most critical hyperparameters of training. Typical values range from 10−410^{-4} to 10−210^{-2}, often with a schedule that decreases the learning rate over time.

Stochastic Gradient Descent (SGD)

In theory, you should compute the gradient over all training data at each step. With datasets of millions of examples, that's impractical. The solution: SGD. Instead of computing the exact gradient, you estimate it on a small random subset of the data, a mini-batch (typically 32 to 512 examples).

The estimated gradient is noisy, but on average it points in the right direction. This noise is actually beneficial: it helps escape local minima and generalize better.

Why all of this is fundamental in AI

Every time a neural network learns, the sequence is identical:

  1. Pass data through the network (forward pass)
  2. Compute the error between prediction and truth (loss)
  3. Compute the gradient of the loss with respect to each parameter (backward pass)
  4. Update the parameters in the direction opposite to the gradient

Steps 3 and 4 rely entirely on derivatives and gradients. Understanding the derivative means understanding how a neural network learns. The gradient is the signal that tells each weight in the network: "move a little in this direction, and the error will decrease."

Without derivatives, no gradients. Without gradients, no backpropagation. Without backpropagation, no deep learning. It's truly the mathematical foundation of all modern AI.

Key takeaways

  • The derivative measures the rate of change of a function, the slope of the tangent
  • The gradient generalizes the derivative to multiple dimensions, it's a vector pointing toward the steepest ascent
  • Gradient descent consists of following the gradient in reverse to minimize the loss
  • The learning rate controls step size, too small, too slow; too big, unstable
  • SGD estimates the gradient on a mini-batch rather than the entire dataset, making large-scale training possible
  • Knowing the derivatives of x2x^2, exe^x, ln⁡(x)\ln(x), and σ(x)\sigma(x) is essential for understanding the internal mechanisms of a neural network

Where this is used. The gradient is the only compass gradient descent has, and that procedure trains every model in this course.