Skip to main content
Open In Colab Now that we have introduced somewhat more formally the learning problem and its notation lets us study a simple but instructive regression problem that is known in the statistics literature as shrinkage. Suppose that we are given the training set X={x(1),,x(m)}\mathbb{X} = \{x^{(1)}, \ldots, x^{(m)}\} together with their labels {y(1),,y(m)}\{y^{(1)}, \ldots, y^{(m)}\}. We need to construct a model such that a suitably chosen loss function is minimized for a different set of input data, the so-called test set. The ability to correctly predict when observing the test set, is called generalization. Since the output yy is a continuous variable, this supervised problem is a regression problem (otherwise it is a classification problem). To make it concrete, read the input xx as normalized time over a single tidal cycle (x=0x = 0 at one high tide, x=1x = 1 at the next) and the output yy as the water level relative to mean sea level, in meters. A tide gauge records the level at a sequence of times, and those readings are your training points. The water climbs to a high, falls to a low, and rises again: one smooth cycle. We draw the readings from sin(2πx)+ϵ\sin(2 \pi x) + \epsilon, with measurement noise ϵN(μ=0,σ=0.25)\epsilon \sim N(\mu=0,\, \sigma=0.25). That underlying tidal curve is completely unknown to you; you see only the noisy gauge readings, and your hypothesis has to recover the smooth tide beneath them.
Output from cell 3 Let us now pick the hypothesis set that corresponds in general to the following sum, g(x;θ)=θ0+j=1M1θjϕj(x)=θϕ(x)g(x;\, \boldsymbol{\theta}) = \theta_0 + \sum_{j=1}^{M-1} \theta_j\, \phi_j(x) = \boldsymbol{\theta}^\top \boldsymbol{\phi}(x) where ϕj(x)\phi_j(x) are known as basis functions. Evaluating the basis at every training input and stacking the results row by row gives the design (featurization) matrix Φ\boldsymbol{\Phi}, whose ii-th row is ϕ(x(i))\boldsymbol{\phi}(x^{(i)})^\top. For the polynomial basis ϕj(x)=xj\phi_j(x) = x^j it is the Vandermonde matrix Φ=[ϕ(x(1))ϕ(x(2))ϕ(x(m))]=[1x(1)(x(1))2(x(1))M1x(2)(x(2))2(x(2))M1x(m)(x(m))2(x(m))M]Rm×(M+1),\boldsymbol{\Phi} = \begin{bmatrix} \boldsymbol{\phi}(x^{(1)})^\top \\ \boldsymbol{\phi}(x^{(2)})^\top \\ \vdots \\ \boldsymbol{\phi}(x^{(m)})^\top \end{bmatrix} = \begin{bmatrix} 1 & x^{(1)} & (x^{(1)})^2 & \cdots & (x^{(1)})^M \\ 1 & x^{(2)} & (x^{(2)})^2 & \cdots & (x^{(2)})^M \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ 1 & x^{(m)} & (x^{(m)})^2 & \cdots & (x^{(m)})^M \end{bmatrix} \in \mathbb{R}^{m \times (M+1)}, so that all mm predictions collapse into a single matrix product y^=Φθ\hat{\boldsymbol{y}} = \boldsymbol{\Phi}\boldsymbol{\theta}. With this notation the ridge objective is Φθy2+λθ2\lVert \boldsymbol{\Phi}\boldsymbol{\theta} - \boldsymbol{y} \rVert^2 + \lambda \lVert \boldsymbol{\theta} \rVert^2, whose minimizer solves the normal equations (ΦΦ+λI)θ=Φy(\boldsymbol{\Phi}^\top \boldsymbol{\Phi} + \lambda \boldsymbol{I})\,\boldsymbol{\theta} = \boldsymbol{\Phi}^\top \boldsymbol{y}. The fitting code below builds exactly this Φ\boldsymbol{\Phi} with np.vander, then standardizes its columns for numerical stability. A set of Polynomial, Gaussian and Sigmoidal basis functions are plotted below.
Output from cell 4 Using the polynomial basis, we fit candidate hypotheses g(x;θ)g(x;\, \boldsymbol{\theta}) of increasing order MM to the training data. The figure below overlays the fits for M=0,1,2,3,9M = 0, 1, 2, 3, 9 on a single plot. Low-order models underfit, while M=9M=9 wiggles through every training point.
Output from cell 5 Our job is to find θ\boldsymbol{\theta} such that the polynomial expansion above fits the data we are given - as we will see there are multiple hypothesis that can satisfy this requirement. Consistent with the block diagram we need to define a metric, an figure of merit or loss function in fact, that is also a common metric in regression problems of this nature. This is the Mean Squared Error (MSE) function, which here plays the role of the empirical risk on the training set: Remp(θ)=1mi=1m(g(x(i);θ)y(i))2R_{\text{emp}}(\boldsymbol{\theta}) = \frac{1}{m} \sum_{i=1}^m \big(g(x^{(i)};\, \boldsymbol{\theta}) - y^{(i)}\big)^2 We can visualize this loss for a specific hypothesis. The green segments below are the displacements (residuals) between each training point and the order-M=2M=2 hypothesis gg; the MSE is the average of their squares.
Output from cell 6 The loss function chosen for this regression problem corresponds to the sum of the squares of the displacements of each data point and our hypothesis. The sum of squares in the case of Gaussian errors gives rise to an (unbiased) Maximum Likelihood estimate of the model parameters. Contrast this to the sum of absolute differences. Now our job has become to choose two things: the parameter vector θ\boldsymbol{\theta}^\ast and MM the order of the polynomial. Both define our hypothesis. If you think about it, the order MM defines the model complexity in the sense that the larger MM becomes the more the number of parameters we need to estimate and store. Obviously this is a trivial example and storage is not a concern here but treat this example as instructive for that it applies in far for complicated settings. Sweeping the model order MM and recording the MSE on the training set and on a held-out test set exposes the tension between fitting and generalization: the training error keeps decreasing with MM, but the test error eventually rises, the signature of overfitting.
Output from cell 7 Obviously you can reduce the training error to almost zero by selecting a model that is complicated enough (M=9) to perfectly fit the training data (if m is small). But this is not what you want to do. Because when met with test data, the model will perform far worse than a less complicated model that is closer to the true model (e.g. M=3). This is a central observation in statistical learning called overfitting. In addition, you may not have the time to iterate over M (very important in online learning settings).
Output from cell 8 To avoid overfitting we have multiple strategies. One straightforward one is evident by observing the wild oscillations of the θ\boldsymbol{\theta} elements as the model complexity increases. We can penalize such oscillations by introducing the 2\ell_2 norm of θ\boldsymbol{\theta} in our loss function. Remp(θ)=1mi=1m(g(x(i);θ)y(i))2+λθ2R_{\text{emp}}(\boldsymbol{\theta}) = \frac{1}{m} \sum_{i=1}^m \big(g(x^{(i)};\, \boldsymbol{\theta}) - y^{(i)}\big)^2 + \lambda \|\boldsymbol{\theta}\|^2 This type of solution is called regularization and because we effectively shrink the parameter dynamic range it is also called in statistics shrinkage or ridge regression. We have introduced a new parameter λ\lambda that regulates the relative importance of the penalty term as compared to the MSE. This parameter together with the polynomial order is what we call hyperparameters and we need to optimize them as both are needed for the determination of our final hypothesis gg. The graph below show the results of each search iteration on the λ\lambda hyperparameter. Sweeping the regularization strength λ\lambda for the M=9M=9 model and recording the MSE on the training set and on a held-out test set reproduces Bishop’s λ\lambda-optimization curve. At very small λ\lambda the model overfits (low training error, high test error); too large a λ\lambda underfits; the test error is minimized at an intermediate λ\lambda^\ast.
Output from cell 9
Output from cell 10 The table below lists the fitted coefficient coordinates for M=3,6,9M = 3, 6, 9, without regularization and with ridge regularization (λ=0.1\lambda = 0.1). Without regularization the polynomial coefficients grow explosively with the model order, reaching the order of 10610^{6} at M=9M = 9 (the wild oscillation plotted above). Ridge penalizes the whole coefficient vector (typically excluding the intercept), so it reduces the overall coefficient norm; in this polynomial example the poorly constrained high-order behavior is what is most visibly suppressed. Individual coordinates, especially with correlated or standardized polynomial features, should not be read as each moving monotonically toward zero. Ridge also does not set individual high-order terms exactly to zero; that kind of sparsity is associated with L1 / Lasso.
From shrinkage to pruning in real models. Ridge only shrinks the coefficients toward zero; it keeps all of them. In a large neural network the analogous, more aggressive step is pruning: the weights that regularization has driven close to zero are set to exactly zero, so they can be dropped to compress and speed up the model (this is closer in spirit to the L1 / Lasso sparsity mentioned above than to ridge). This is a standard step in production: NVIDIA’s TensorRT Model Optimizer prunes, imposes 2:4 structured sparsity (half the weights in every group zeroed), and quantizes (INT8 / FP8), and the TensorRT runtime then executes the resulting sparse, low-precision network on the GPU’s Sparse Tensor Cores. So “drop the coefficients close to zero” is not only a textbook idea, it is a core step in deploying real models.
Key references: (, n.d.; , n.d.; , n.d.)

References

  • (n.d.). Pattern Recognition and Machine Learning.
  • (n.d.). PRML: PRML algorithms implemented in Python.
  • (n.d.). Elements of Statistical Learning.