JA EN
LearnCalculus & Optimization
·FREE·7 min read

Calculus for AI — The Gradient Is an Arrow Saying Which Way Is Better

No epsilon-delta limits, no integration by parts. Training is measuring a slope and stepping the other way. A derivative is a multiplier, a gradient is a list of slopes, the chain rule is multiplication — and Jacobians and Hessians only need to be recognised, not computed.

ModalitytextTaskmath

The goal: being able to read this equation

Every paper about training contains this line in some disguise.

θt+1=θtηθL(θt)\theta_{t+1} = \theta_t - \eta\,\nabla_{\theta}\mathcal{L}(\theta_t)
(1)

It is one line of bookkeeping, which says: from where the parameters are now, take a small step in the direction opposite to whichever way the loss increases fastest. θ\theta ("theta") is every parameter in the model, tt counts steps, η\eta ("eta") sets the step size, L\mathcal{L} is the loss, and \nabla ("nabla") is the subject of this article. Swap every symbol for an ordinary word and the line reads: new settings = current settings − (how big a step) × (the direction that makes things worse).

The amount of calculus required is startlingly small. Formal limits and integration techniques never show up. You need one intuition — a derivative is a slope — plus the vocabulary for extending it to many variables at once.

A derivative is a multiplier, and nothing more

dfdx(x)=limh0f(x+h)f(x)h\frac{df}{dx}(x) = \lim_{h \to 0}\frac{f(x+h) - f(x)}{h}

Here hh is a tiny number and limh0\lim_{h\to 0} means "where this ratio heads as hh shrinks toward zero".

Put in words: nudge the input by hh and ask by what factor the output moves. The numerator is the change in output, the denominator the change in input, and the derivative is their ratio. A slope of 0.5 means nudging the input by 0.01 moves the output by 0.005. A derivative is that local multiplier — there is no deeper content.

The sign carries the weight. Positive slope: increasing the input increases the output. Negative: increasing it decreases the output. To go down, move against the slope. That is the entirety of training.

Partial derivatives: hold everything else still, turn one knob

One knob is easy; real models have millions to billions. So you freeze every other knob and wiggle exactly one, measuring the slope along that direction. That is a partial derivative, written with \partial ("round d") instead of dd.

Lθi\frac{\partial L}{\partial \theta_i}

The symbol is really a question, which says: how much does the loss change if I nudge only knob number ii? The \partial on top is the change in the loss, the \partial underneath is the change in that one knob, and the subscript ii names which knob. It is the mixing-desk move — push one fader, listen for the difference. Nothing new has been introduced; it is a slope with a promise attached that everything else stays put.

The gradient: every slope, lined up

Compute one partial derivative per knob and stack them in order.

θL=[Lθ1,  Lθ2,  ,  Lθn]\nabla_{\theta} L = \left[\frac{\partial L}{\partial \theta_1},\; \frac{\partial L}{\partial \theta_2},\; \ldots,\; \frac{\partial L}{\partial \theta_n}\right]
(2)

Said in words: the effect of turning each knob individually, simply listed in order. The brackets are the list, each entry inside is one knob's slope, and nn is however many knobs the model has. It is not a new concept, it is an inventory of partial derivatives. Seven billion parameters means a gradient with seven billion numbers in it.

But lining them up creates properties the plain inventory did not have:

To descend, you go the other way — which is where the minus sign in equation (1) comes from. In Linear Algebra for AI a vector was a coordinate holding meaning; a gradient is an arrow living in that same space.

FIG 1The ball measures the slope and rolls against it. Raise the learning rate and it overshoots the valley, starts oscillating, and eventually blows up — that is what enlarging η in equation (1) does

The chain rule: multipliers compose by multiplying

If deep learning has one genuine trick, this is it.

dzdx=dzdydydx\frac{dz}{dx} = \frac{dz}{dy}\cdot\frac{dy}{dx}
(3)

The setup is a three-stage relationship: changing xx changes yy, and changing yy changes zz.

What it says is: multipliers compose by multiplication. Move xx by 1 and yy moves by 3; move yy by 1 and zz moves by 2; therefore moving xx by 1 moves zz by 6. Identical to gear ratios in a meshed train of gears.

A neural network is a stack of layers, which is to say a composed function — dozens of meshed gears. So the influence of an early parameter on the final output is just the product of every multiplier along the way. Executing that product from the output end backwards is backpropagation, laid out in Backpropagation From Scratch.

Being a product has consequences you can predict from the arithmetic alone. If the factors along the chain are mostly below 1, the product collapses exponentially toward zero (vanishing gradients); above 1, it explodes. Read ReLU, residual connections and normalisation layers as tools for protecting that chain of multiplications, and a lot of architectural choices line up behind a single motive.

Jacobians and Hessians: recognise them, don't compute them

Both names appear constantly in papers. You never have to evaluate one; you only have to know what table it is.

The Jacobian appears when both input and output are vectors — it is the exhaustive table of partial derivatives.

Jij=fixjJ_{ij} = \frac{\partial f_i}{\partial x_j}

In plain terms: how much output ii moves when input jj is nudged, tabulated for every pair. A gradient is the special case where the output is a single number. Backpropagation is exactly the process of multiplying such Jacobians by a vector, working from the output end inward — and implementations compute the product without ever forming the table.

The Hessian collects second derivatives, the slope of the slope.

Hij=2LθiθjH_{ij} = \frac{\partial^2 L}{\partial \theta_i \partial \theta_j}

The 2\partial^2 is a slope of a slope, and the indices ii and jj pick out a pair of knobs. Read one cell at a time, which says: how the steepness along knob ii changes when knob jj is turned. Taken as a whole the table is curvature: how sharply the valley bends. If the first derivative tells you which way is down, the second tells you how fast that downhill is turning. Knowing curvature would in principle let an optimizer pick its own step size.

So why is it not used? With nn parameters the Hessian is n×nn \times n. At a billion parameters the table alone is astronomically large. The per-parameter step sizes in optimizers like Adam are not the Hessian; they come from a running average of squared gradients, a cheap stand-in for curvature.

Convexity: why everyone wishes the loss were convex

f(λx+(1λ)y)λf(x)+(1λ)f(y)f(\lambda x + (1-\lambda)y) \le \lambda f(x) + (1-\lambda)f(y)

λ\lambda is a number between 0 and 1; the left side is the function's value at a point between xx and yy, the right side is the height of the straight line joining them.

What this says is: any chord drawn between two points on the graph stays above the graph. A bowl. One valley, no others.

Why does that help? Every local minimum is the global minimum. Reach a point where the slope is zero and you can declare it the answer. Initial values and random seeds do not change the outcome. Linear and logistic regression live here, which is exactly why they reproduce the same solution every run.

Deep learning losses are not convex. There are many valleys, along with saddle points (downhill in one direction, uphill in another) and broad plateaus where the slope nearly vanishes. That is why initialisation and data ordering change your results. It works well enough in practice anyway, and the honest criterion in production is never "did we reach the global optimum" but "is validation performance good enough" (Overfitting and Evaluation Design).

FIG 2The terrain two parameters create. The gradient points perpendicular to the contour lines, along the steepest direction. Watch the path shudder in a narrow valley — and watch momentum cancel that shudder out

Three things that matter in practice

1. Non-differentiable points do not stop training ReLU has a corner at zero where no slope is defined. Implementations return 0 or 1 there by convention. Landing exactly on zero is vanishingly unlikely and causes no trouble in practice, so "technically non-differentiable" is not a reason to hesitate.

2. Watch the gradient norm The length of the gradient is a health check for training. Spiking means explosion; sitting near zero means vanishing gradients or a plateau. Looking at the loss alone will not distinguish those two.

3. Autodiff is automated chain rule, not magic PyTorch and JAX apply the chain rule mechanically to the graph recorded during the forward pass. Read detach() and no_grad() as declarations of "cut the chain here" and you can trace why gradients stopped flowing.

Summary

Next we re-read the model's output in another language entirely — probability (Probability and Statistics for AI) — where the question of why loss functions have the shapes they do finally gets answered.

Comments

Sign in to comment