Calculus for Machine Learning
From Derivatives to Gradients · 15 min
A derivative measures how fast a function's output changes as its input changes by an infinitesimal amount — geometrically, the slope of the line tangent to the function's graph at a point (MIT OCW 18.01, Single Variable Calculus). For the function f(x) = x², the derivative is f′(x) = 2x, so at x = 3 the function is increasing at an instantaneous rate of f′(3) = 6 units of output per unit of input; a small increase in x near 3 produces roughly six times as large an increase in f(x), while near x = 0 the same function would barely change at all, since f′(0) = 0. This single-variable idea — rate of change — is the atomic building block for everything that follows in machine learning, because every loss function an AI model minimizes is, at its core, a function whose local behavior near the current parameters is described exactly by its derivative, telling the training process whether increasing or decreasing a parameter will make the model's predictions better or worse, and by roughly how much for a given size of adjustment.
Most functions an AI system actually optimizes depend on many variables at once — a linear regression model might have dozens of weights, and a deep network can have billions. To differentiate such a function, MIT's 18.02 Multivariable Calculus introduces the partial derivative: the rate of change of the function with respect to one variable while holding all others fixed, treated as if the other inputs were momentarily constant numbers. Collecting all of a function's partial derivatives into a single vector produces the gradient, written ∇f. For f(x, y) = x² + 3y², the partial derivative with respect to x is 2x and with respect to y is 6y, so at the point (1, 2) the gradient is ∇f(1, 2) = (2, 12) — a vector that simultaneously reports how the function changes along each of its two input directions, with the larger second component showing the function is currently more sensitive to changes in y than in x at that point.
The gradient is not just descriptive; it is directional. A foundational fact used throughout AI is that the gradient ∇f points in the direction in which f increases fastest, and its negative, −∇f, points in the direction of steepest decrease (Parr & Howard's Matrix Calculus You Need for Deep Learning explains this in the context of neural network training). That single fact is why gradient descent — the workhorse algorithm behind training everything from logistic regression to large language models — repeatedly computes the gradient of a loss function with respect to the model's parameters and steps in the negative-gradient direction, shrinking the loss a little at a time until the model's predictions improve; without this directional property of the gradient, there would be no principled way to know which of millions of parameters to adjust, or which way to adjust each one.
Jacobians, Hessians, and the Chain Rule in Neural Networks · 15 min
When a function produces multiple outputs from multiple inputs — as every layer of a neural network does, mapping a vector of activations to another vector of activations — a single gradient vector is not enough to describe it. Instead, calculus stacks the gradient of each output into rows of a matrix called the Jacobian (explained.ai's Matrix Calculus You Need for Deep Learning walks through this construction in detail, building on the partial-derivative machinery of MIT 18.02). A layer that maps 3 inputs to 2 outputs has a 2×3 Jacobian: each of its two rows is the gradient of one output with respect to all three inputs, so reading across a row shows how sensitive one particular output is to each input, while reading down a column shows how much every output responds to one particular input. The Jacobian is the natural generalization of the single-variable derivative to vector-valued, vector-input functions, and it is exactly the object that deep learning frameworks like PyTorch and TensorFlow compute automatically during training, layer after layer.
Where the gradient captures a function's slope, the Hessian matrix captures its curvature — the matrix of all second-order partial derivatives of a scalar function. For a function of two variables, the Hessian is a 2×2 matrix whose entries describe how each partial derivative itself changes as the inputs move; its eigenvalues reveal whether a point is a local minimum (all positive), a local maximum (all negative), or a saddle point (mixed signs) — a distinction MIT 18.02's treatment of optimization with several variables makes explicit. In AI, the Hessian matters because loss surfaces of deep networks are riddled with saddle points, and second-order methods like Newton's method use Hessian information to take smarter, curvature-aware steps than plain gradient descent, correcting for the fact that a function can curve much more sharply in some directions than others, at the cost of being far more expensive to compute for high-dimensional models with millions of parameters.
The multivariable chain rule is what makes training deep networks tractable at all: it says that the derivative of a composition of functions equals the product of the derivatives of each function in the chain. Concretely, if a tiny two-layer network computes h = w₁x and y = w₂h, then the derivative of y with respect to w₁ is ∂y/∂w₁ = ∂y/∂h · ∂h/∂w₁ = w₂ · x — the chain rule lets us compute how the final output depends on an early parameter by multiplying local derivatives together, layer by layer, without ever needing to write out the whole composed function explicitly. Backpropagation, the algorithm that trains virtually every modern neural network, is precisely this chain rule applied systematically from the loss backward through every layer, reusing each layer's local Jacobian (explained.ai's guide develops this vector chain rule explicitly for deep learning; MIT 18.02 covers the underlying multivariable chain rule that makes the whole procedure valid).
Calculus for Gradient-Based Learning
The gradient points uphill along the tangent's slope; an optimizer steps in the opposite direction to descend toward the minimum.
- The gradient of a function always points toward its steepest increase, so gradient descent moves in the negative-gradient direction to decrease a loss.
- The Hessian's curvature tells an optimizer whether it is near a true minimum, a maximum, or a saddle point where training can stall.
- Backpropagation is the multivariable chain rule applied mechanically, layer by layer, from the loss back to every weight in a network.
Recall Practice
Glossary
- Derivative
- The instantaneous rate of change of a function with respect to one input variable.
- Partial derivative
- The rate of change of a multivariable function with respect to one variable, holding all others fixed.
- Gradient
- The vector of all partial derivatives of a function, pointing in the direction of steepest increase.
- Jacobian matrix
- The matrix of all partial derivatives of a vector-valued function, stacking one gradient per output.
- Hessian matrix
- The matrix of second-order partial derivatives of a function, describing its local curvature.
- Chain rule
- The calculus rule stating that the derivative of a composition of functions is the product of the derivatives of each piece, used throughout backpropagation.
Manual Gradient Descent Step
This is a virtual, hand-computed exercise using only the numbers supplied here — no real dataset or software involved. Given the function f(x, y) = x² + 3y², a starting point (1, 2), and a learning rate of 0.1, compute the gradient ∇f(1, 2) by hand, then apply one gradient descent update (x, y) ← (x, y) − 0.1·∇f(x, y) and verify the new point lands at (0.8, 0.8).
Ready to test yourself?
5 questions on this module.