Gradient Descent and Numerical Optimization › Formula
Machine Learning from ScratchAll chapters Gradient Descent and Numerical Optimization › FormulaSection view · Sections in this chapter4 / 10 ↓
Reading tools Definitions, links & notes
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Let be the parameter vector and let be the real-valued objective to minimize. We begin with a local approximation to , then use quadratic examples to determine exactly when repeated updates converge.
The entries of are the quantities we fit. In the regression example they will be an intercept and slopes; the objective is half the mean squared prediction error. Unless stated otherwise, the derivations concern differentiable objectives on an unconstrained parameter space.
The first thing we need is a compact summary of how responds to a nudge in any of the coordinate directions. That summary is the gradient, which collects all first partial derivatives into one vector:
Read the vector one entry at a time and each entry asks the same question in a different coordinate: if we nudge only by a tiny amount and hold the other parameters fixed, how fast does change, and in which direction? Stacking those answers into a column vector turns separate one-dimensional sensitivities into a single object that encodes both the steepest direction and the steepness itself. Two facts about that object carry everything that follows:
- For a nonzero gradient, its negative gives the steepest directional decrease among directions of unit Euclidean length. This is a first-order statement about a small displacement, not the best destination at an arbitrary finite distance.
- The gradient norm measures the first-order residual in the chosen units. A differentiable unconstrained local minimum has zero gradient, but a small norm alone does not measure distance to a minimizer or certify that a point is a minimum.
A direction alone does not tell us how far to move. The learning rate supplies that scale.
Gradients and parameter updates
Let be a direction vector and a scalar step multiplier. A first-order Taylor expansion around the current point says
The left side is the objective after moving by . The right side adds a first-order prediction of that change to the current objective. The dot product is the directional derivative along . When both vectors are nonzero, it is negative if their angle exceeds ninety degrees. Among unit Euclidean directions, Cauchy-Schwarz gives the smallest dot product at , provided the gradient is nonzero. Absorbing the step length into a positive scalar gives the gradient-descent update
where:
- : the parameter vector at iteration
- : the updated parameter vector after one step
- : the learning rate, controlling how far we travel along the negative gradient
- : the gradient of the objective evaluated at the current parameters, pointing in the direction of steepest increase
- : the iteration counter
Reading it plainly: the new parameters are the old parameters, shifted a small distance against the gradient. In scalar form, component obeys , where is the -th component after the update, is the -th component before the update, is the learning rate, and is the partial derivative of the objective with respect to the -th parameter. This component form is often easier to reason about when we implement it.
The displacement has Euclidean length : the learning rate multiplies the gradient rather than directly specifying a distance. For example, when fitting a likelihood model we minimize its negative log-likelihood and differentiate that objective. A local linear approximation need not remain accurate over a large displacement, so the step length requires a separate choice.
Gradients and parameter updates on a quadratic
For the scalar quadratic with , the gradient is . Substituting it into the update gives
where:
- : the parameter at iteration
- : the curvature, the second derivative of with respect to
- : the learning rate
- : the signed multiplier of the scalar parameter at each update
The quantity multiplies the parameter anew at every iteration, which means after steps
where is the starting value at iteration zero.
For every starting value, convergence to zero occurs when , equivalently . At , a single update reaches zero. For a nonzero starting value, produces constant-amplitude sign changes, and produces divergence. A start already at zero stays there for any finite learning rate. These exact recursions use the constant curvature of this quadratic; a general smooth objective needs a separate local or global analysis.
Learning rates, curvature, and feature scaling
The relevant step scale is reciprocal curvature. For , larger curvature narrows the stable interval . This explains why a rate that works on one quadratic may be unstable on another. It does not imply that every small rate is equally efficient.
For a quadratic with a constant symmetric positive-definite Hessian , all eigenvalues satisfy . Write the error relative to its unique minimizer in the orthonormal eigenvector basis of . Each error coordinate is multiplied by , so the Euclidean error contracts by at most . Convergence from every start requires . Balancing the two extreme multipliers gives the optimal fixed rate and its worst-case parameter-error contraction factor
where:
- : the condition number of the Hessian, defined as , which measures how differently the objective curves across directions
- : the smallest and largest eigenvalues of the Hessian , the curvatures along the shallowest and steepest directions respectively
- : the worst-case per-iteration error contraction factor for the optimal fixed learning rate
When , this quadratic has equal curvature in every direction and the optimal fixed rate reaches the minimizer in one update. At , the factor is ; the worst-case parameter error needs 116 updates for a tenfold reduction because . This comparison uses relative parameter error and the optimal fixed rate. Objective gaps along an individual eigenmode contract by the square of that mode's parameter multiplier.
For , differentiation gives gradient and Hessian . Here has rows and columns, has entries, and has entries. If has full column rank, the Hessian is positive definite and its spectral condition number equals the square of the design matrix's spectral condition number. Rescaling feature columns changes this geometry. Standardization often helps when unequal feature scales cause poor conditioning, but it does not remove collinearity or guarantee fast convergence. With rank deficiency, the Hessian is singular and the positive-condition-number formulas above do not apply.
Batch, stochastic, and mini-batch descent
When is a finite mean of differentiable example losses, differentiation gives . The full-batch method computes this complete average before an update. Its work is times the cost of one example gradient; for the dense linear model here that is .
A single-example update uses one loss gradient. If a fresh index is sampled uniformly and independently of previous updates, its gradient at the current fixed parameters has conditional expectation equal to the full gradient. The update is
A single-example gradient can vary around the full gradient, so individual updates need not decrease the full objective. Persistent noise near an optimum can leave fluctuations when the step stays constant. Positive schedules satisfying , , and are used in convergence analyses together with assumptions on the objective and noise. A smooth strongly convex objective, conditionally unbiased bounded-variance gradients, and stable iterates are one such setting; the schedule alone is not a guarantee for an arbitrary objective. Noise can also vanish at the solution, in which case convergence with a constant step is possible. Our implementation uses random reshuffling, whose adaptive updates are not automatically covered by the fresh-index unbiasedness calculation.
Mini-batch descent sits between the extremes, using a batch of size :
At fixed parameters, averaging independent example gradients divides their covariance by . Sampling a subset without replacement has a different finite-population factor and gives zero sampling variance when the batch contains all examples. Batch size also changes the number of updates per pass and the available vectorization. We use 32 as one illustrative mini-batch size below; it is not a universal optimum.
Newton steps, stopping rules, and numerical checks
If curvature is the problem, we can use it. Rather than guessing a step size that survives the worst direction, expand the objective to second order around the current point:
The Hessian is the matrix of second partial derivatives. The new quadratic term models how the slope changes over the displacement . When this Hessian is positive definite, the quadratic approximation has a unique minimizer. Differentiating that approximation with respect to and setting its gradient to zero gives
So the Newton update is
The displayed rule is the full Newton step, with step multiplier one. In practice we solve rather than explicitly forming an inverse. On a positive-definite quadratic this reaches the minimizer in one update in exact arithmetic. Local quadratic convergence for a nonquadratic objective is guaranteed under a sufficiently close start, a nonsingular Hessian at the solution, and a Hessian that is locally Lipschitz, meaning its changes are bounded proportionally to parameter displacement nearby. These sufficient conditions are stronger than smoothness alone.
A dense direct solve typically costs arithmetic and stores a matrix; forming the Hessian has an additional cost. Newton-type methods can also use iterative solves or Hessian-vector products, so explicit dense storage is not universal. An indefinite Hessian can produce an uphill direction. A shift must be large enough to make the modified Hessian positive definite; at a nonzero gradient that provides a descent direction. A line search can then shorten a descent step to achieve sufficient decrease. Shortening an uphill direction alone does not repair its sign.
For stopping, we need honest criteria rather than a fixed iteration count. Three are standard, and they answer different questions:
- Absolute gradient norm checks an approximate first-order stationarity condition in the current units.
- Relative objective change asks whether progress has stalled.
- Relative parameter change asks whether the iterates have settled.
Each can misfire alone. A tiny objective change may reflect a flat but nonoptimal plateau, and a small gradient norm may occur at a saddle. Report more than one diagnostic alongside the objective value at the returned point. A finite-difference estimate provides a separate check on a hand-derived gradient: it can expose a sign, normalization, or indexing mistake before we interpret the training curve.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!