~/teaching · machine learning · session 3, thursday 15 october 2026
Convex optimisation and regularisation: gradient descent, step size, ridge and lasso
How a convex loss is minimised, and what a penalty on the coefficients buys you.
Abstract
This deck is the third session of the Machine Learning course at Centrale Méditerranée (option DDEFI, 2026-2027). The previous session wrote down what we minimise, least squares and the log-likelihood of the logistic regression. This one asks how the minimum is found, why the method works, and what happens when we add a price on the size of the coefficients.
The first half builds gradient descent from convexity: why a zero gradient settles the problem, which step size keeps the descent stable, why an elongated valley is slow, and why everybody standardises. Stochastic gradient, momentum and Adam follow. The second half covers ridge and lasso three times over: as a formula, as a geometry and as a Bayesian prior, then shows how to pick the strength of the penalty without touching the test set.
Each idea is computed by hand on numbers small enough to check with a pencil: a one-variable parabola, least squares with four observations, and a toy set of two orthogonal columns. After the session you can say whether a problem is convex, diagnose a descent that diverges or crawls, explain ridge as a direction-by-direction shrinkage and the lasso as soft thresholding, and choose the penalty by cross-validation.
Plan of the deck
- Convexity and the zero-gradient theorem. Chord above the curve, curve above its tangents, and why a flat tangent marks the global minimum.
- Two curvature constants, L and μ. The steepest and the flattest curvature of the bowl, and their ratio, the condition number κ.
- Gradient descent and the step size. The update rule, the descent lemma, and the condition η < 2/L computed on a parabola.
- Convergence rate and standardisation. The gap shrinks like (1 − 1/κ) per step, so the number of steps grows with κ; scaling the columns fixes it.
- Stochastic gradient, momentum, Adam. One gradient per observation or per mini-batch, the heavy ball, and per-coordinate step sizes.
- Ridge and its reading in the SVD basis. Closed form with λ on the diagonal, then a shrinkage factor for each singular direction.
- Lasso, geometry and the Bayesian reading. Soft thresholding and exact zeros, the corners of the L1 ball, Gaussian and Laplace priors.
- Choosing λ and reading the path. Cross-validation on the training set, the one-standard-error rule, and the regularisation path.
- Lab. Ridge coded through the SVD and checked against scikit-learn, then the lasso path of a credit-default dataset.
Convexity: why a zero gradient is enough
A convex function is a bowl with a single bottom, so the point where the slope vanishes is the best point there is.
A function is convex when the segment joining any two points of its graph never dips below the graph. Least squares and the logistic log-loss are convex; the loss of a neural network is not, and can trap a descent in a local dip. Everything in this session assumes the convex case.
Subtract , divide by and let tend to zero: the chord inequality becomes the tangent inequality. The curve lies above every one of its tangents. This is the form used all morning.
If the gradient is zero at , the linear term disappears and for every . A stationary point of a convex function is therefore a global minimum. That is why the normal equations of the previous session were enough. The least-squares Hessian is , and , so linear regression is convex.
The running example of the deck is the bowl . Its slope is negative left of 3, zero at 3, positive to the right, and the height is smallest exactly where the slope vanishes. Two constants then describe the shape of any bowl: , the largest curvature, and , the smallest. For a quadratic they are the extreme eigenvalues of the Hessian, and their ratio is the condition number met with the SVD.
Gradient descent and the step size η
Feel the slope, step downhill, repeat: the only real decision is how long each step is, and a step too long makes things worse.
The gradient points uphill, so we walk the other way. Among all directions of a given length, the Cauchy-Schwarz inequality shows that decreases fastest to first order. The step length is the learning rate .
If the curvature never exceeds , the function stays under a parabola of curvature (the descent lemma, admitted in the deck). Plugging the step into it gives a guaranteed decrease as long as .
On a quadratic the picture is exact. In the eigenbasis of the Hessian, each coordinate of the gap to the minimum is multiplied by at each step. Every factor must have modulus below 1, and the largest eigenvalue gives the tightest constraint: . The step removes the steepest direction in one move.
| Step k | w_k | slope 2(w_k − 3) | w_{k+1} | f(w_{k+1}) |
|---|---|---|---|---|
| 0 | 0 | −6 | 1.5 | 3.25 |
| 1 | 1.5 | −3 | 2.25 | 1.5625 |
| 2 | 2.25 | −1.5 | 2.625 | 1.1406 |
| 3 | 2.625 | −0.75 | 2.8125 | 1.0352 |
| 4 | 2.8125 | −0.375 | 2.9063 | 1.0088 |
Same bowl, same start, four step sizes (here 1/L = 0.5 and 2/L = 1)
- η = 0.25. Factor 0.5: the iterates 1.5, 2.25, 2.625 approach 3 from one side.
- η = 0.5. Factor 0: the first step lands on 3 and stays there.
- η = 0.75. Factor −0.5: 4.5, 2.25, 3.375, the descent overshoots and comes back, but still converges.
- η = 1.2. Factor −1.4, beyond 2/L: 7.2, −2.88, 11.23, each step jumps further away. This is the training run whose loss turns into NaN.
Why the condition number decides the speed
In a long narrow valley the step must be small for the steep side, so progress along the flat floor is slow; rescaling the variables rounds the valley.
With the steep direction is settled at once and the flat direction sets the pace: its factor is . Dividing the gap by ten therefore costs about iterations.
The deck checks this on least squares with four observations and two correlated columns. The Hessian of the mean loss has eigenvalues 1 and 4, so , and . From the second iteration on, each gap to the solution is exactly 0.75 times the previous one, and iterations divide it by ten.
The consequence is practical. A column in euros next to a column in years makes enormous: on the credit-default data of the course, the deck reports a condition number of about for the raw columns of the logistic Hessian, against about 300 after centring and scaling, computed on its 18,000-row training split. Standardising is the cheapest acceleration there is. Its mean and standard deviation come from the training set only, otherwise the validation and test sets leak into the model.
Remark (Three habits). Standardise first. Plot the training loss at every step, on a log scale: a rising curve means the step is too large, a straight slow decline means it is too small. Start with a small step and multiply it by three while the loss keeps going down.
Stochastic gradient, momentum and Adam
Using the slope of a few random observations instead of all of them makes each step far cheaper, at the price of some noise.
The loss is an average of individual losses, so its gradient is the average of individual gradients. Stochastic gradient descent (SGD) uses one of them, drawn at random: on average it is the right direction, and one step costs times less. A mini-batch of observations keeps the estimate unbiased and divides its variance by .
With a fixed step the noise never dies and the iterates keep circling around the minimum. Robbins and Monro (1951) gave the conditions on a decreasing step: so that the walk can go anywhere, so that the noise fades. On the deck's two-variable example the four individual gradients at the origin differ, but their mean is the full gradient , with a spread .
Two refinements close the first half. Momentum (Polyak, 1964) accumulates past gradients: in the steep direction they alternate in sign and cancel, along the floor they add up. Adam (Kingma and Ba, 2015) adds a step per coordinate, scaled by a running average of squared gradients, which acts as an automatic standardisation. It is the default for neural networks, but it carries no convergence guarantee in the convex case; for a logistic regression, plain descent or L-BFGS is enough.
Ridge regression: shrinkage, direction by direction
Ridge adds a cost on large coefficients, which pulls them towards zero and tames the directions that the data barely determine.
Nearly collinear columns make almost singular, and the least-squares coefficients become huge with unstable signs. Ridge (Hoerl and Kennard, 1970, also known as Tikhonov regularisation) adds to the criterion. The gradient stays linear, so the solution is the normal equations with on the diagonal, invertible for every . The intercept is not penalised: centre and the columns first.
On a toy set of four rows with two orthogonal columns of , and . Least squares give , and ridge divides by instead of 4, so both coefficients are multiplied by , that is 0.8 at , 0.5 at , 0.1 at . They shrink; they never reach zero.
The SVD gives the exact mechanism. Least squares divide each projection by the singular value , and a tiny is where the variance blows up. Ridge multiplies each direction by a factor between 0 and 1.
| Direction | d_j² | OLS coordinate | factor d_j²/(d_j² + 2) | ridge coordinate |
|---|---|---|---|---|
| (1, 1)/√2 | 8 | 1.414 | 0.8 | 1.131 |
| (1, −1)/√2 | 2 | 0.707 | 0.5 | 0.354 |
Recomposed in the original basis, this gives against for least squares, the same as the direct formula. The sum of the factors, 1.3 here instead of 2, is the effective number of parameters the model really uses.
Lasso: soft thresholding, corners and priors
The lasso subtracts a fixed amount from each coefficient and sets to exactly zero those that would cross zero, so it selects variables.
The lasso (Tibshirani, 1996) replaces the squared norm by the sum of absolute values. With orthonormal columns (, the scikit-learn convention with in front of the squared error), the criterion separates into one problem per coordinate. Each has a corner at zero, handled with the subgradient of , and the solution is soft thresholding of the least-squares coefficient .
| λ | β̂₁ | β̂₂ | Variables kept |
|---|---|---|---|
| 0 | 2.4 | 1.2 | 2 |
| 0.5 | 1.9 | 0.7 | 2 |
| 1 | 1.4 | 0.2 | 2 |
| 1.2 | 1.2 | 0 | 1 |
| 2.4 | 0 | 0 | 0 |
A check from the deck: at the criterion is 2.605 at the thresholded point , 2.625 if the second coefficient is forced to zero too early, and 3.605 at the least-squares point. Without orthonormal columns there is no closed form, and solvers iterate a gradient step followed by a thresholding (ISTA) or run coordinate descent, as scikit-learn does.
Geometry. Penalising is equivalent to minimising the loss inside a budget . The L2 budget is a disc, the L1 budget a diamond with corners on the axes; the elliptical contours of the loss usually first touch the diamond at a corner, where some coordinates are exactly zero.
Bayesian reading. Adding a prior on turns the maximum a posteriori into loss plus penalty. A Gaussian prior gives ridge with , and a Laplace prior gives the lasso. Choosing a large is stating that you expect small coefficients compared with the noise, and with the lasso that you expect many of them to be zero.
Choosing λ by cross-validation, never on the test set
The strength of the penalty is a setting chosen on held-out folds of the training data; the test set is opened once, at the very end.
is a hyperparameter. Fitted on the training data it would always fall to zero, and tuned on the test set it would leave nothing honest to report. The answer is the cross-validation of the previous session: a grid of on a log scale starting from , where every lasso coefficient is zero, five folds of the training set, and the mean validation error per value with its standard error. Standardisation is refitted inside each fold.
Two choices are defensible: , the minimum of the curve, or the one-standard-error rule (Breiman and co-authors, 1984), the largest whose error stays within one standard error of the minimum. The second gives a simpler model that the data cannot tell apart from the best, and it is the one usually deployed. In scikit-learn the logistic regression is parametrised by , the inverse of the penalty strength.
The regularisation path plots every coefficient against . For the lasso it is piecewise linear: all coefficients start at zero and the variables enter one by one as the penalty relaxes, the most informative first. The deck adds a deployment test: refit the lasso on many resamples of the training set and count how often each variable is kept (stability selection, Meinshausen and Bühlmann, 2010). A short, stable list of variables is what a credit committee can read, explain and maintain, which is the subject of the next session on thresholds and reproducible delivery.
Key takeaways
- For a convex function, a point where the gradient vanishes is a global minimum: chord above the curve, curve above its tangents.
- Gradient descent converges for a step η < 2/L, where L is the largest curvature; beyond that the loss rises at every step.
- The number of iterations grows with the condition number κ = L/μ, which is why columns are standardised, with statistics computed on the training set only.
- SGD and mini-batches trade an unbiased but noisy gradient for a far cheaper step; the noise falls like 1/B, and the step must decrease to settle.
- Ridge shrinks each singular direction by d²/(d² + λ) and never sets a coefficient to zero; the lasso subtracts λ by soft thresholding and produces exact zeros.
- Ridge corresponds to a Gaussian prior and the lasso to a Laplace prior: λ compares the expected noise with the expected size of the coefficients.
- λ is chosen by cross-validation on the training folds, often with the one-standard-error rule; the test set stays closed until the final report.
Glossary
- Convex function
- A function whose chords lie above its graph; any point with zero gradient is a global minimum.
- Gradient descent
- Iterative method that moves against the gradient, by a step proportional to the learning rate.
- Learning rate (η)
- Length of a gradient step per unit of slope; it must stay below 2/L for the descent to converge.
- Condition number (κ)
- Ratio L/μ of the largest to the smallest curvature; it sets the number of iterations gradient descent needs.
- Stochastic gradient descent (SGD)
- Gradient descent that uses one observation or a mini-batch per step, unbiased but noisy.
- Momentum
- Accumulation of past gradients in a velocity, which damps oscillations and speeds up flat directions.
- Ridge
- Least squares with an L2 penalty λ‖β‖²; shrinks coefficients without setting them to zero.
- Shrinkage
- Pulling estimated coefficients towards zero to reduce their variance, at the cost of some bias.
- Lasso
- Least squares with an L1 penalty λ‖β‖₁; produces exact zeros and so selects variables.
- Soft thresholding
- The map z ↦ sign(z)(|z| − λ)₊, the lasso solution coordinate by coordinate when the columns are orthonormal.
- Regularisation path
- The fitted coefficients plotted as a function of λ, from all-zero to the unpenalised fit.
- Prior
- Distribution on the parameters before seeing the data; a Gaussian prior gives ridge, a Laplace prior the lasso.
- Cross-validation
- Rotating split of the training set into folds, used to choose hyperparameters such as λ without the test set.
The other notes of the course
One public summary per deck, in the order of the course.
Previous: Maximum likelihood and logistic regressionNext: Decision threshold and reproducible ML
- IntroductionAI winters, jobs and method
- ToolsKaggle, GitHub, Python: the tools
- Session 1Project framing and least squares
- Session 2Maximum likelihood and logistic regression
- Session 3Gradient descent, ridge and lasso
- Session 4Decision threshold and reproducible ML
- HomeworkA defensible baseline model
- Session 5Kernels, SVM and decision trees
- Session 6Forests, boosting, honest comparison
- Session 7SHAP, fairness, PCA and clustering
- HomeworkA documented champion model
- Session 8Neural networks, drift, conformal prediction
- HomeworkA delivered machine learning project
- Session 9Defending a machine learning project
- ReferenceCredit default scoring project
- ReferenceAsset returns: XGBoost and sentiment
- ReferencePython code for every ML model
- ReferenceDeep learning essentials
- ReferenceOpen data ML projects by sector
- ReferenceData and AI engineer handbook
- ReferenceData engineering, SRE and cloud
- ReferencePanorama of machine learning models
About these notes
Written by the lecturer from the slides of the deck, as a public summary: the key ideas, the formulas and one worked example. The exercises, their solutions, the tests and the grading stay in the deck, for the students of the course.
Revision: .