Contents
11Interview questions and model answers
Q1What is the difference between L1 and L2 regularization?
Both add a penalty on the size of the weights to the training loss to reduce overfitting. L2 adds \lambda\sum_jw_j^2; its gradient 2\lambda w shrinks every weight in proportion to its size, so weights become small but not zero. L1 adds \lambda\sum_j|w_j|; its gradient has constant size \lambda, so weak weights are driven exactly to zero and L1 also performs feature selection. L2 has a closed form for linear regression and handles correlated features by sharing weight; L1 picks one of a correlated group.
Q2Why does L1 produce sparse solutions and L2 does not?
Three equivalent answers. Forces: the L1 push does not weaken near zero, and at zero the penalty can absorb any loss gradient smaller than \lambda, so a weak feature stays at exactly zero; the L2 pull vanishes at zero. Geometry: the L1 ball is a diamond with corners on the axes, and the expanding loss contours usually touch it first at a corner, where some weights are zero; the L2 ball is smooth. Optimality: the lasso sets w_j=0 whenever |2x_{\cdot j}^\top(y-Xw)|\le\lambda. In the orthonormal case the lasso soft-thresholds, \operatorname{sign}(\hat w)\,(|\hat w|-\lambda/2)_+, while ridge scales, \hat w/(1+\lambda).
Q3Derive the ridge regression solution.
Minimize \|y-Xw\|^2+\lambda\|w\|^2. The gradient is -2X^\top y+2X^\top Xw+2\lambda w; setting it to zero gives \hat w=(X^\top X+\lambda I)^{-1}X^\top y. The matrix is positive definite for \lambda>0, so the solution is unique and exists even when X^\top X is singular. In the eigenbasis of X^\top X, ridge multiplies the least-squares solution by d_j/(d_j+\lambda) in each direction.
Q4How do you choose \lambda?
By K-fold cross-validation over a logarithmic grid, picking the value with the lowest average validation error, or the largest value within one standard error of it for a simpler model. Features should be standardized first. The test set is not used. For ridge, generalized cross-validation gives a closed-form alternative.
Q5What happens to the lasso with highly correlated features? What would you use instead?
It tends to select one feature of the group and set the others to zero, and which one can change with small perturbations of the data. Ridge would share the weight among them. The elastic net, \lambda_1\|w\|_1+\lambda_2\|w\|^2, keeps the sparsity of L1 and adds the grouping effect of L2, so correlated features enter or leave together.
Q6What is the Bayesian interpretation of L1 and L2?
With Gaussian noise, the regularized least-squares solution is the maximum a posteriori estimate under a prior on the weights. A Gaussian prior \mathcal N(0,\tau^2) gives ridge with \lambda=\sigma^2/\tau^2; a Laplace prior with scale b gives the lasso with \lambda=2\sigma^2/b. The Laplace prior’s sharp peak at zero is the source of sparsity. Only the posterior mode is sparse; the posterior mean is not.
Q7Why is early stopping a form of regularization?
Gradient descent starts from small weights and moves toward the training optimum, fastest along high-curvature directions. Stopping after \tau steps keeps the weights small and suppresses the flat, noise-dominated directions, like ridge. For a quadratic loss the filter factors 1-(1-\eta h)^\tau and h/(h+2\lambda) match when \lambda\approx1/(2\eta\tau). In practice one monitors the validation loss, uses a patience window and restores the best weights.
Q8Is weight decay the same as L2 regularization?
For plain SGD yes, up to a rescaling by the learning rate: the L2 gradient 2\lambda w makes each step multiply the weights by 1-2\eta\lambda. For adaptive optimizers such as Adam no: the L2 gradient is divided by the adaptive scale, so weights with large gradients are decayed less. AdamW decouples the decay, w\leftarrow w-\eta\big(\hat m/(\sqrt{\hat v}+\epsilon)+\lambda w\big), and generally generalizes better.
Q9Why should you standardize features and not penalize the intercept?
The penalty depends on the scale of each weight, and a weight’s scale depends on its feature’s units, so without standardization the penalty is arbitrary and the lasso’s selection depends on the units. The intercept only shifts the predictions; penalizing it would bias all predictions toward zero and make the fit depend on where the origin of y is.
Q10Besides L1 and L2, how else can you prevent overfitting?
Get more data; use data augmentation (label-preserving transformations); add noise to the inputs, which for linear regression is exactly ridge with \lambda=ns^2; dropout, an adaptive L2-like penalty; early stopping; simpler models or feature selection; ensembles such as bagging; and cross-validation to choose all of these. The right choice depends on whether the problem is variance (overfitting) or bias (underfitting).