§ Gradients · Interactive Graduate Primer
Tensor by Tensor · expanded

Why gradients?

Learn how changing several inputs changes one output. Begin with partial derivatives, then explore gradient descent, backpropagation, and optimizers. The later sections include proofs and deeper mathematical detail.
A first reading path

Start with sections 2–4, then section 7. Return to the directional-derivative proof after you understand a gradient-descent update. An optimizer is a rule for updating the model’s adjustable numbers.

For L(a, b) = a² + 2b², the gradient at (1, 2) is [2, 8]. The first entry measures change along a while b stays fixed. The second measures change along b while a stays fixed. With step size 0.1, subtract [0.2, 0.8] to reach (0.8, 1.2). The loss falls from 9 to 3.52.

Check: why subtract the gradient?

The gradient points toward the steepest local increase under the usual Euclidean length measure. Subtracting it moves toward a local decrease when the step is small enough. A large step can still overshoot and increase the loss.

1. Motivation

Introduction

Training repeatedly asks one practical question: which small parameter change should reduce the current loss? The gradient supplies a local direction, and the model measures, updates, and checks again rather than searching every possibility.

Learning goal

Explain why training uses local gradient information and preview the complete update cycle in this guide.

Before you start

Functions, coordinates, basic derivatives, and the idea of a model parameter and loss.

Lesson plan

  1. Frame learning as changing parameters to reduce a measured loss.
  2. See how one local slope replaces an impractical search over all changes.
  3. Preview the measure, differentiate, update, and recheck cycle used throughout training.

The problem: a model has many adjustable numbers, but one loss value. We need a reliable direction for changing every number. This guide builds that direction from one-variable slopes, then proves what the gradient means.

A neural network is a parameterised function $f_\theta : \mathcal{X} \to \mathcal{Y}$ with $\theta \in \mathbb{R}^n$, where $n$ ranges from $10^5$ in a small CNN to $10^{12}$ in a frontier LLM. Training means choosing $\theta$ to minimise

$$L(\theta) = \tfrac{1}{N}\sum_{i=1}^{N}\ell\bigl(f_\theta(x_i),\,y_i\bigr).$$

Because $\theta$ is high-dimensional and $L$ non-convex, we cannot solve $\nabla L(\theta)=0$ in closed form. We iterate: evaluate $L$ and its gradient, step, repeat. Every practical optimiser — SGD, Adam, Muon — is a recipe for using the gradient wisely.