Reliable and efficient learning requires control of model complexity, economical use of large datasets, and accurate derivative calculations. We study regularization through ridge regression and sparsity, followed by stochastic gradient methods that reduce the cost of individual updates. The final section derives automatic differentiation on computational graphs and applies it to feedforward networks, recurrent models, and optimization procedures.
For simplicity, consider regularized least-squares regression:
x∈Rpminfλ(x):=21∣∣Ax−y∣∣2+λR(x)(15.1)
where R(x) is the regularizer and λ⩾0 the regularization parameter.
The regularizer expresses prior structure of the weight vector x, such as small magnitude or sparsity. The strength λ can be selected by cross-validation.
We assume that R is finite, nonnegative, lower semicontinuous, and coercive, i.e. R(x)→+∞ as ∣∣x∣∣→+∞.
The following proposition shows that as λ↓0, regularization selects among the exact solutions when Ax=y is feasible. This is particularly useful when ker(A)={0} and there are infinitely many exact solutions.
Thus xλ depends linearly on y and can be computed by solving a linear system. Two equivalent expressions are available.
The systems can be solved directly, for example by Cholesky factorization, or iteratively, for example by conjugate gradients. The latter exploits the quadratic structure more effectively than ordinary gradient descent.
For n>p, use the smaller system (15.4); for n<p, use (15.5).
Pseudo-inverse.
As λ↓0, xλ converges to the minimum-norm least-squares solution A+y. If Ax=y is feasible, this is, by (15.2)
Ax=yargmin∣∣x∣∣.
If A has full column rank, so ker(A)={0} and n⩾p, A⊤A∈Rp×p is an invertible matrix, and (A⊤A+λIdp)−1→(A⊤A)−1, so that
x0=A+ywhereA+:=(A⊤A)−1A⊤.
If A has full row rank, so ker(A⊤)={0} and n⩽p, the alternative formula is
x0=A+ywhereA+:=A⊤(AA⊤)−1.
When n=p and A is invertible, both formulas reduce to A+=A−1. For any rank, the singular value decomposition (SVD) gives the Moore–Penrose pseudoinverse by inverting the nonzero singular values and leaving the zero ones unchanged.
Figure 15.1.ℓq balls {x;∑k∣xk∣q⩽1} for varying q.
The penalty promotes sparsity in the solutions xλ of
xλ∈x∈Rpargminfλ(x)=21∣∣Ax−y∣∣Rn2+λ∣∣x∣∣1
The solutions xλ may be nonunique, and typically have many zero coefficients.
Figure 15.1 illustrates the geometry of ℓq balls. They concentrate around the coordinate axes as q→0, favoring sparse solutions, but become nonconvex for q<1.
Sparsity can encode a prior on the unknown, as in imaging, or select a small subset of predictive features. Feature selection can make a model easier to interpret and cheaper to evaluate.
Whether Lasso or ridge regression performs better depends on sparsity, correlations, noise, and the prediction task.
The objective fλ is convex, but the ℓ1 penalty is nonsmooth, so ordinary gradient descent does not apply at all points.
Section 15.1.4 develops an appropriate modification of gradient descent.
A closed-form solution xλ is generally unavailable, but the case of an orthogonal design matrix A reduces to coordinatewise thresholding.
Figure 15.2. Effect of λ on the penalized scalar objective F(x):=21(x−y)2+λ∣x∣.
Figure 15.2. Effect of λ on the penalized scalar objective F(x):=21(x−y)2+λ∣x∣.
The term K(x,x′) is nonnegative when λmax(A⊤A)⩽1/τ, equivalently τ⩽1/∣∣A∣∣op2, where ∣∣A∣∣op=σmax(A) is the operator norm.
Thus fτ(x,x′) is a valid surrogate:
f(x)⩽fτ(x,x′),fτ(x,x)=f(x),andf(⋅)−fτ(⋅,x′) is smooth.
The surrogate is strongly convex in its first argument, so its minimizer is unique.
Define the iteration
xk+1:=xargminfτ(x,xk)(15.6)
which ensures by construction that
f(xk+1)⩽f(xk).
Equation (15.7) defines iterative soft thresholding. For A=0, the surrogate argument applies when 0<τ⩽1/∣∣A∣∣op2. Convergence to a Lasso solution holds for a fixed step in the larger range 0<τ<2/∣∣A∣∣op2. If A=0, every positive step is admissible.
A finite average is an expectation under the uniform measure π=n1∑i=1nδi, with f(x,i)=fi(x). Conversely, drawing (zi)i i.i.d. from π and setting fi(x)=f(x,zi) gives an empirical approximation of (15.9). Under integrability, the empirical objective converges pointwise by the law of large numbers. Convergence of minima or minimizers requires additional assumptions.
For example, empirical risk minimization with linear predictors uses
fi(x)=ℓ(⟨ai,x⟩,yi)andf(x,z)=ℓ(⟨a,x⟩,y)(15.10)
for z=(a,y)∈Z=(A=Rp)×Y (typically Y=R or Y={−1,+1} for regression and classification), where ℓ is some loss function.
We illustrate the methods with binary logistic classification, using
L(s,y):=log(1+exp(−sy)).(15.11)
The same computational approach extends to general parametric models, including deep neural networks.
The stochastic methods below apply both to finite sums (15.8) and to expectations (15.9), under the corresponding unbiasedness and moment assumptions. We present them for finite sums. When n is large, the aim is to make each iteration inexpensive without evaluating every summand.
When the summands fi(x) are similar, a single gradient can approximate the average well: ∇fi≈∇f. Evaluating ∇fi is typically n times cheaper than forming the full gradient.
Stochastic optimization is nevertheless not always faster than batch gradient descent.
For moderate n, a deterministic method may use the available computation more effectively.
When n is too large for a full pass to be practical, stochastic methods divide the work into small updates and begin improving the model immediately. Mini-batches also permit parallel computation.
Batch gradient descent (BGD), analyzed in Section 14.4, iterates
xk+1=xk−τk∇f(xk)
For a convex objective with an L-Lipschitz gradient and a minimizer, the condition 0<τmin⩽τk⩽τmax<2/L ensures convergence. Strong convexity strengthens this to a linear rate.
For a finite-sum objective, the gradient is
∇f(x)=n1i=1∑n∇fi(x)(15.12)
Its cost is typically O(np) when each ∇fi can be evaluated in time linear in p.
For an ERM summand of the form (15.10), Taylor expansion gives
Figure 15.4. Unbiased stochastic gradient estimate (panel 1) and a schematic SGD trajectory (panel 2).
For very large n, computing the full gradient ∇f as in (15.12) is prohibitive.
SGD replaces the full gradient by the gradient of one summand fi, with i sampled uniformly. This gives an unbiased estimate:
Ei∇fi(x)=∇f(x)(15.14)
where i is a random variable distributed uniformly in {1,…,n}.
Starting from x0, stochastic gradient descent (SGD) iterates
xk+1=xk−τk∇fi(k)(xk)
where, for each iteration index k, i(k) is drawn independently of previous draws, uniformly from {1,…,n}.
The iterates xk+1 are random. We therefore study convergence to a minimizer of f in a probabilistic sense, such as mean square or probability, together with the corresponding rates.
A batch-gradient step costs O(np), whereas an SGD step costs O(p). This makes SGD attractive when n is large and repeated passes through the data are costly. Redundancy between observations can sometimes yield accurate predictions after only k≪n updates, before all observations have been used.
The step-size schedule τk must balance two requirements: it usually needs to decrease to suppress stochastic noise, but not so quickly that the method stops making progress before reaching a minimizer.
A common schedule has asymptotic behavior τk∼k−1 as k→+∞. One such schedule is
τk:=1+k/k0τ0(15.15)
where τ0>0 sets the initial step and k0>0 controls how long the steps remain approximately constant.
Figure 15.5 illustrates minimization of f1(x)+f2(x) for x∈R, with f1(x)=(x−1)2 and f2(x)=(x+1)2. The distribution of xk concentrates around the minimizer x⋆=0. The initial iterate x0 is uniform on [−1/2,1/2].
Figure 15.5. Repeated SGD runs for k↦xk∈R. Left: the distribution of iterates at each iteration. Right: individual trajectories.
The following theorem gives a mean-square convergence rate. The second-moment assumption is imposed along the iterates, not uniformly on all of Rp: a globally strongly convex function cannot have a globally bounded gradient.
The preceding SGD bound, and the averaging scheme below, give sublinear rates even when f is strongly convex. By contrast, batch gradient descent has a linear rate under the assumptions of Theorem 14.16.
Figure 15.6 compares the objective values f(xk). The black dashed curve shows batch gradient descent, with the iteration axis rescaled to reflect the n-fold cost of a batch update.
Figure 15.6. Objective-error evolution for SGD in logistic classification; the dashed curve shows batch gradient descent.
15.2.4 Stochastic Gradient Descent with Averaging (SGA)¶
Diminishing steps can slow SGD as τk approaches zero.
Averaging reduces fluctuations in the estimate. Run SGD on auxiliary iterates (x~k)k,
x~k+1=x~k−τk∇fi(k)(x~k)
and return their Cesàro average:
xk:=k1ℓ=1∑kx~ℓ.
This gives stochastic gradient descent with averaging (SGA), also called averaged SGD.
The average can be maintained without storing the full trajectory, using the recursive update
xk+1=k+11x~k+1+k+1kxk.
A typical step-size schedule is
τk:=1+k/k0τ0.
These steps decrease more slowly, at rate k−1/2.
Averaging can reduce sensitivity to the tuning parameters (k0,τ0), although their choice still affects performance.
Bach proves that for logistic classification, averaging can adapt to local curvature under suitable regularity and step-size assumptions. This is a problem-dependent result, rather than a universal improvement of every constant over SGD.
For a finite dataset with n samples of total size n×p, one can trade additional memory for reduced gradient noise by storing past gradient evaluations. This yields the stochastic averaged gradient (SAG) method.
Store the latest gradient of each summand in a table (Gi)i=1n, requiring O(np) memory. Maintain its average g as an approximation to the full gradient.
Initialize Gi=0 for all i and g=0, so that g=n−1∑iGi is the stored average. At each iteration, update the table and then x:
Because most entries were evaluated at earlier iterates, the table average is generally biased conditional on the current history. Its convergence analysis therefore differs from the unbiased-SGD proof above. SAG uses a fixed step size τ. As for batch gradient descent, choose τ on the scale of 1/L, where L bounds the Lipschitz constants of the individual gradients ∇fi; the admissible numerical constant depends on the convergence theorem.
For smooth convex finite sums with a minimizer and a suitable step size, SAG admits an O(1/k) expected objective bound for averaged iterates. Furthermore, in the presence of strong convexity (for instance after adding a positive quadratic penalty to logistic regression), it has a linear convergence rate, i.e.
E(f(xk))−f(x⋆)=O(ρk),
for some 0<ρ<1.
These improvements exploit the finite-sum structure: SAG stores information about each of the n summands. SGD and averaged SGD also extend beyond finite n to general expectations (15.9).
Figure 15.7 shows a comparison of SGD, SGA and SAG.
Figure 15.7. Objective gaps for SGD, averaged SGD (SGA), and SAG, displayed on logarithmic axes.
Gradient evaluation ∇f(x) is the main operation in batch and stochastic gradient methods. For linear models and shallow networks, explicit formulas usually reduce it to matrix-vector products. For deeper or more elaborate computations, expanded derivative formulas become unwieldy and can duplicate work. Automatic differentiation organizes the chain rule recursively to reuse intermediate results.
For f:Rp→R, we want to evaluate ∇f:Rp↦Rp. A finite-difference approximation with a small parameter ε>0 is
ε1(f(x+εδ1)−f(x),…,f(x+εδp)−f(x))⊤≈∇f(x)
This requires p+1 evaluations of f. Here δk=(0,…,0,1,0,…,0) is the coordinate vector with its 1 at position k.
For large p, repeated evaluations are expensive. Finite differences also balance truncation error against floating-point cancellation as ε decreases. Reverse-mode automatic differentiation computes the full gradient at a cost typically proportional to one evaluation of f.
Like symbolic differentiation, automatic differentiation applies exact derivative rules, subject to floating-point error. It organizes those rules around the operations of the program that evaluates the function, preserving reuse of intermediate computations.
Let x=(x1,…,xs) be the inputs to a program evaluating f. Number its intermediate variables xs+1,…,xt, with output xt=f(x), and let xk∈Rnk. We seek the Jacobian blocks ∂xk∂f(x)∈Rnt×nk for k=1,…,s. The scalar case nk=1 is easiest to follow; the formulas also apply to vector variables if the matrix products retain the displayed order. The total input dimension is p=∑k=1snk.
Figure 15.8. A computational graph.
Represent the computation as a sequence of elementary maps:
∀k=s+1,…,t,xk=fk(x1,…,xk−1)
Each fk depends only on earlier variables; see Figure 15.8. The directed acyclic graph (DAG) connects each argument used by fk to its output xk. The indexing is a topological order: every edge points from a smaller to a larger index.
Evaluating f(x) is a forward traversal of the graph.
Automatic differentiation differentiates the supplied computational graph. It does not by itself choose an efficient implementation of the original formula; the derivative computation benefits from the efficiency and reuse already present in the forward program.
Forward mode propagates derivatives of each intermediate variable with respect to a chosen input block, here x1. For scalar inputs, repeating the traversal for all p coordinates gives the full derivative. The formulas below also propagate a block Jacobian when n1>1.
Figure 15.9. Derivative dependencies in forward mode (left) and reverse mode (right).
The notation “parent(k)” denotes the nodes ℓ<k of the graph that are connected to k, see Figure 15.9, left.
The stored quantities are the derivatives ∂x1∂xℓ. The symbol × denotes matrix multiplication in the vector-valued case.
The brackets […] indicate a local derivative: ∂xℓ∂xk is the derivative of the elementary map fk with respect to that argument. It can be evaluated when needed, assuming the local derivative is available in closed form.
Suppose the local derivative operations ∂xℓ∂fk have comparable costs, as when the variable dimensions nk are uniformly bounded, and each node has a bounded number of parents. Computing the full Jacobian coordinate by coordinate requires p traversals, each with cost comparable to a forward evaluation. This becomes expensive for large p.
Simple example.
We consider the function
f(x,y)=ylog(x)+ylog(x)(15.19)
on the open domain x>0, ylogx>0, where all elementary derivatives exist. Its computational graph is displayed in Figure 15.10. The iterations of the forward mode to compute the derivative with respect to x read
Figure 15.10. Example of a simple computational graph.
Dual numbers.
Dual numbers provide a convenient implementation of the forward pass. They form a real algebra whose elements have the form x+εx′ where ε is a symbol obeying the rule that ε2=0. Here (x,x′)∈R2 and x′ is intended to store a derivative with respect to some input variable. Multiplication and, when x=0, inversion are given by
Reverse mode computes the derivatives ∂xk∂xt of the output with respect to the intermediate variables. For a scalar output and many input coordinates, this avoids the repeated forward traversals required to compute each ∂xi∂xk separately.
Initialize the derivative of the output node by
∂xt∂xt=Idnt×nt,
and apply the following recursion from the last node to the first:
Here (∂xk∂fm(x1,…,xm−1))⊤∈Rnk×nm is the adjoint Jacobian of fm. This adjoint recursion is called backpropagation and is the standard form used in machine learning.
For a scalar output (nt=1), reverse mode is usually the preferred way to compute the full gradient. It requires access to the intermediate variables (xk)k=s+1t, which can be costly to store for a large graph. Checkpointing trades additional computation for reduced storage.
Simple example.
For the two-input example f(x,y) in (15.19), reverse mode gives
Figure 15.11. Matrix multiplication costs for forward mode (left) and reverse mode (right) along a chain.
We note that the computation of the product A×B of A∈Rn×p with B∈Rp×q requires O(npq) arithmetic operations with the standard dense algorithm.
As shown on Figure 15.11, the arithmetic costs of the two parenthesizations are proportional to
n0k=1∑t−1nknk+1andntk=0∑t−2nknk+1
For roughly comparable intermediate widths, these costs favor reverse mode when nt≪n0. A scalar learning objective typically has nt=1, making reverse mode a natural choice; the exact cost comparison depends on all the intermediate dimensions.
A parameterized feedforward architecture, such as a neural network, has the form
∀k=1,…,t,xk=fk(xk−1,θk−1)(15.21)
where θk−1 is the parameter vector for a layer and the input x0∈Rn0 is fixed. The objective is
f(θ):=L(xt)(15.22)
Here L:Rnt→R is a loss, such as squared prediction error or logistic loss, and θ=(θk)k=0t−1 collects the parameters. Figure 15.12 shows the computational graph.
Figure 15.12. Computational graph for a feedforward architecture.
Reverse mode computes the gradient of f by propagating derivatives through the variables (xk,θk). Initialize
where θk−1∈Rnk×nk−1 contains the weights and ρ acts coordinatewise; see Figure 15.13. The derivative formulas below apply where ρ is differentiable. At a ReLU kink, a chosen derivative convention defines the computed update, but the classical derivative need not exist.
A residual form of (15.21) can be interpreted as a time discretization of an ordinary differential equation.
Keep the state dimension fixed, nk=n, and interpret xk as an approximation of a continuous trajectory x(t) at time t=kτ. The formal continuum limit takes τ↓0 with kτ approaching a fixed time.
Impose the residual structure
fk(xk−1,θk−1)=xk−1+τu(xk−1,θk−1,kτ)(15.25)
with a parameterized vector field u(x,θ,t)∈Rn. Formally, as τ→0, this approaches the nonlinear ODE
x˙(t)=u(x(t),θ(t),t)(15.26)
with x(t=0)=x0.
Writing z(t)=∇x(t)f(θ) for the adjoint, the discrete relations (15.23) become a linear backward ODE:
The adjoint has terminal condition z(T)=∇L(x(T)). The parameter derivative is interpreted as the L2 functional gradient with respect to the time-dependent control. Its discrete normalization is τ1∇θk−1f→∇θ(t)f(θ).
as illustrated in Figure 15.15. Here θ=(W1,W2)∈(Rq×n)2 contains the weights and ρ is a pointwise activation. Increasing the hidden width q enlarges the class of representable residual maps.
When W2=−τW1 and ρ=ψ′, this update is a gradient step for E(x,θ)=∑iψ((W1x)i). Repeating it gives an iterative implementation of the argmin layer (15.31), provided the iterations converge to a minimizer.
Backpropagation requires access to the iterates (xk)k=0t, whose storage can dominate memory use. Checkpointing stores selected states and reruns parts of the forward computation to reconstruct missing values. Recursive schedules can use O(logt) stored states with an O(logt) computational overhead.
If the forward computation is invertible, stored states can sometimes be reconstructed instead. Assume maps gk satisfy
xk=gk(xk+1,…,xt).
These inverse updates may amplify numerical errors, so algebraic invertibility alone does not ensure stable reconstruction. In practice, the inverse can also depend on a few extra variables, in particular on the input values (x0,…,xs).
For example, split the continuous state as x(t)=(r(t),s(t)) and suppose the vector field in (15.26) has the separated form u((r,s),θ,t)=(F(s,θ,t),G(r,θ,t)). An invertible staggered update is
If h is continuous and the iterates converge to x⋆(θ), their limit is a fixed point:
x⋆(θ)=h(x⋆(θ),θ).
Backpropagation differentiates the finite computation f(θ)=L(xt,θ). For the limiting objective f⋆(θ)=L(x⋆(θ),θ), the implicit function theorem gives an alternative:
Assume h is continuously differentiable and Id−∂xh is invertible at the fixed point. The implicit function theorem then gives
∂x⋆(θ)=(Id−∂xh(x⋆(θ),θ))−1∂θh(x⋆(θ),θ).
In practice, one replaces in these formulas x⋆(θ) by xt, which approximates the derivative of the converged objective f⋆, rather than the derivative of the finite unrolling f.
This approach replaces storage of the iteration history by a linear solve at the approximate equilibrium.
Argmin layers
An argmin layer maps a parameter θ to a minimizer x(θ) of a parameterized objective. Assume a unique minimizer, or specify a branch when several exist:
x(θ)=xargminE(x,θ).
One way to compute the layer is gradient descent, initialized here at x0=0:
xk+1=xk−τ∇E(xk,θ).(15.31)
This has the form (15.25) when using the vector field u(x,θ)=−∇xE(x,θ).
Assume E is twice continuously differentiable near the selected minimizer and its Hessian with respect to x is invertible. The implicit function theorem gives a locally differentiable minimizer branch; applying (15.30) to its stationarity equation yields
If f(θ) is the minimized value itself, f(θ)=E(x⋆(θ),θ), then L=E. Formula (15.30) simplifies because ∇xL(x⋆(θ),θ)=0, giving
∇f⋆(θ)=∇θL(x⋆(θ),θ).(15.32)
This is the envelope theorem, also associated with Danskin’s theorem.
Sinkhorn’s algorithm
Let a∈Rn and b∈Rm be strictly positive probability vectors, let C∈Rn×m be a cost matrix, and let ε>0. Entropic optimal transport minimizes
ij∑CijPij+εij∑Pij(logPij−1)
over P⩾0 with row sums a and column sums b. Set Kij=e−Cij/ε>0. Sinkhorn’s alternating updates, initialized with positive scaling vectors, are
uk+1=Kvka,vk+1=K⊤uk+1b.
All divisions are componentwise. The second update uses the newly computed uk+1; this sequential dependence is part of Sinkhorn’s alternating scheme.
The regularized transport value is the maximum of the dual objective
E(u,v;a,b)=ε(⟨a,logu⟩+⟨b,logv⟩−⟨u,Kv⟩).
Sinkhorn performs alternating maximization of this objective. A finite number of iterations defines a differentiable recurrent map on positive inputs. Its Jacobian products can be computed by chaining the two scaling steps. For example, for u=a/(Kv),
∂vu=−diag((Kv)2a)K,∂au=diag(Kv1),
and analogous formulas apply to the v update, with the dependence through the newly computed u included.
At convergence, the envelope theorem gives marginal derivatives represented by the dual potentials
∇af⋆=εlogu⋆,∇bf⋆=εlogv⋆.
The scalings have the invariance (u,v)↦(cu,v/c), so these potentials are defined up to opposite additive constants. On the probability simplices, derivatives act on zero-sum perturbations and are therefore independent of this gauge. An implicit differentiation formula must fix the gauge before inverting a Jacobian; the unmodified fixed-point Jacobian is singular in that direction.