← Tensor by Tensor
§ Math Fundamentals for AI
Tensor by Tensor · Complete Guide

Gradients

Fourteen lessons that connect high-school mathematics to AI. Start with the plain-language explanation and a small example. Then read the full formulas, derivations, and Python code at your own pace.

How to study a lesson

Read the opening example. Try the question before revealing its answer. Then work through the detailed notes below it. You do not need to understand every proof on the first reading.

Symbols: a subscript such as xi identifies one entry. Σ means add a collection of terms. ∈ means “belongs to”. The symbol ≈ means “approximately equal”, not exactly equal. A parameter is an adjustable number in a model.

01.04 · Gradients

Introduction

A loss depends on more than one parameter, so one slope is not enough. At the point 3, negative 1, the gradient gives one sensitivity per direction and guides a step that lowers loss from 5 to 1.25.

Learning goal

Calculate a two-variable gradient and use it to perform and interpret one complete descent update.

Before you start

Single-variable derivatives, vectors, coordinate points, and substitution into simple algebraic expressions.

Lesson plan

  1. Hold other inputs fixed to calculate one partial derivative at a time.
  2. Collect partial derivatives into a gradient vector and read its direction.
  3. Apply a learning-rate-scaled descent step and explore the resulting vector field.

Use a loss instead of a hill. For L(w,b)=(w−1)²+(b+2)², the gradient at (3,−1) is [4,2]. Predict the descent direction: both coordinates should decrease, so we step opposite the gradient.

The central concept

The gradient is the workhorse of machine learning. It tells you the direction and rate of steepest increase of a scalar function. Gradient descent — stepping opposite the gradient — is how neural networks learn.

Partial derivatives

When a function depends on multiple variables, we can ask how it changes as one variable moves while the others stay fixed. The partial derivative with respect to $x_i$ is:

$$\frac{\partial f}{\partial x_i}(x) = \lim_{h \to 0} \frac{f(x_1, \ldots, x_i+h, \ldots, x_n) - f(x)}{h}$$
Example

Let $L(w, b) = (w-1)^2 + (b+2)^2$. Then:

  • $\frac{\partial L}{\partial w} = 2(w-1)$ (treat $b$ as constant)
  • $\frac{\partial L}{\partial b} = 2(b+2)$ (treat $w$ as constant)

At $(w,b) = (3, -1)$: $\partial L/\partial w = 4$ and $\partial L/\partial b = 2$.

The gradient

The gradient collects all partial derivatives into a single vector:

$$\nabla f(x_1, \ldots, x_n) = \begin{bmatrix} \frac{\partial f}{\partial x_1} \\ \vdots \\ \frac{\partial f}{\partial x_n} \end{bmatrix}$$

For our example: $\nabla L(w,b) = [2(w-1),\; 2(b+2)]$. At $(3,-1)$: $\nabla L = [4, 2]$.

Theorem · Steepest ascent

The gradient $\nabla f(x)$ points in the direction of steepest increase of $f$ at $x$. Its magnitude $\|\nabla f(x)\|$ equals the rate of increase in that direction.

Proof sketch: The directional derivative in direction $\mathbf{u}$ (unit vector) is $D_\mathbf{u} f = \nabla f \cdot \mathbf{u} = \|\nabla f\| \cos\theta$. This is maximized when $\theta = 0$, i.e., $\mathbf{u}$ points along $\nabla f$.

Gradient descent

To minimize a loss $L$, step opposite the gradient:

$$\theta_{k+1} = \theta_k - \eta \nabla L(\theta_k)$$

where $\eta > 0$ is the learning rate (step size).

Example · One step

Starting at $(w,b) = (3, -1)$ with $\eta = 0.25$:

$$\begin{bmatrix} w \\ b \end{bmatrix}_{\text{new}} = \begin{bmatrix} 3 \\ -1 \end{bmatrix} - 0.25 \begin{bmatrix} 4 \\ 2 \end{bmatrix} = \begin{bmatrix} 2 \\ -1.5 \end{bmatrix}$$

The new loss is $(2-1)^2 + (-1.5+2)^2 = 1 + 0.25 = 1.25$, down from 5.

Learning rate matters

For a quadratic $L(w) = (w-1)^2$ starting at $w_0 = 3$:

  • $\eta = 0.1$: slow, steady convergence ✓
  • $\eta = 0.5$: converges in one step ✓
  • $\eta = 1.2$: overshoots, loss increases
Stability bound

For a convex quadratic with Hessian eigenvalues $\lambda_i$, gradient descent converges iff $0 < \eta < 2/\lambda_{\max}$. Larger $\eta$ causes divergence.

The gradient as a vector field

The gradient $\nabla f$ is a vector field: at each point in the input space, it assigns a vector. Plotting these vectors over level curves of $f$ reveals the geometry:

  • The gradient is orthogonal to level curves (contour lines)
  • It points uphill (steepest ascent)
  • Its magnitude $\|\nabla f\|$ tells you how steep the slope is