Hyperparameters: learning rate, momentum and regularisation
Hyperparameters — learning rate, momentum and regularisation — control optimization step sizes, acceleration and model complexity to ensure stable training and good generalisation in deep learning.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Engineers tune hyperparameters to make training stable, fast and to avoid poor generalisation. The learning rate controls how big each optimisation step is; momentum accelerates convergence and helps escape shallow local minima; regularisation methods reduce overfitting so models perform well on new (test) data.
Key ideas
Gradient — the vector of partial derivatives ∇L(θ); it points in direction of greatest local increase of L. Gradient descent moves parameters opposite to the gradient to reduce loss.
Learning rate (η) — a non-negative scalar that multiplies the gradient during parameter updates. It controls step size in parameter space. Definition (Gradient descent update): the basic update for parameters θ at step t is θ_{t+1} = θ_t − η · ∇L(θ_t). Assumption: differentiable loss. If η is too large, updates can diverge; if η is too small, convergence is slow.
Draw: for exams draw a 2D plot with horizontal axis 'parameter θ' and vertical axis 'loss L(θ)'. Sketch a convex bowl (quadratic). Mark a starting point θ0 on the right and draw arrows leftwards showing steps of size proportional to η. Label arrows with step number t and show how larger η gives larger arrows; if η too large, next arrow should overshoot and bounce to left of minimum.
Stability and convergence for quadratic losses: for a 1D quadratic L(θ)=½ a θ^2 (a>0) the gradient is a θ. Gradient descent converges iff 0 < η < 2/a. For multivariate quadratic L(θ)=½ θ^T A θ (A symmetric positive definite), convergence requires 0 < η < 2/λ_max, where λ_max is the largest eigenvalue of A. Assumption: constant Hessian (quadratic), linear update analysis.
Momentum — method to accumulate a velocity vector that smooths gradient updates and adds inertia. Two common forms:
- Classical (Polyak) momentum: maintain velocity v_t and use v_{t+1} = β v_t + ∇L(θ_t) θ_{t+1} = θ_t − η v_{t+1} where 0 ≤ β < 1 is the momentum coefficient. β near 0.9 is common.
- (Optional) Nesterov accelerated gradient (NAG): approximate future position to compute gradient; update equations differ but purpose is similar: faster convergence with some theoretical guarantees for convex problems.
Intuition: momentum averages gradients over time; components with consistent sign get amplified, oscillating directions get damped. In quadratic problems momentum increases the effective stable learning rate and accelerates along low-curvature directions.
Draw: same 2D bowl; show zig-zag without momentum (short steps across steep direction) and longer smooth spirals with momentum. Label axes and paths.
Regularisation — methods to reduce model complexity and overfitting (model fits noise in training data). Define terms:
- Overfitting — model performs much better on training data than on test data; it has learned noise or specifics of training set.
- Underfitting — model performs poorly on both training and test; it is too simple to capture patterns.
Common regularisation techniques:
- L2 regularisation (weight decay / ridge): add λ/2 · ||θ||_2^2 to loss (λ ≥ 0). Penalises large weights; has closed-form effect in linear least squares (shrinks weights toward zero). Parameter λ is regularisation strength.
- L1 regularisation (lasso): add λ · ||θ||_1 to loss. Encourages sparsity (many weights zero) because L1 penalty is not differentiable at zero.
- Dropout: during training randomly set a fraction p of activations to zero each mini-batch; with standard dropout scale activations by (1 − p) at test time, or with inverted dropout scale activations by 1/(1 − p) during training and do nothing at test time. This approximates training an ensemble of subnetworks and reduces co-adaptation.
- Early stopping: monitor validation loss; stop training when validation loss stops improving to avoid overfitting. Equivalent to a form of regularisation because it limits effective capacity.
- Batch normalisation (can have regularising effect) and data augmentation (changes inputs to reduce overfitting).
Draw: training loss and validation loss vs epochs. Show training loss decreasing, validation loss decreasing then increasing — mark early stopping epoch at validation minimum.
Interaction notes:
- Learning rate and momentum interplay: with momentum you can often use a larger effective step-size; but both must be tuned together.
- Regularisation and optimisation: weight decay interacts with adaptive optimisers differently (e.g., Adam); some frameworks implement L2 regularisation by adding (λ/2) · ||θ||_2^2 to the loss (coupled; gradient becomes ∇L + λθ), while others use decoupled weight decay (e.g., AdamW) that applies parameter shrinkage separately from the loss gradient — be precise in exams about which form is used.
Formulas
θ_{t+1} = θ_t − η · ∇L(θ_t)
- θ_t: parameter vector at iteration t, unit: depends on model (often treated unitless in ML); η: learning rate, units chosen so that η∇L has the same units as θ (typically treated as dimensionless in ML practice); ∇L(θ_t): gradient vector, units of loss per parameter.
v_{t+1} = β v_t + ∇L(θ_t)
θ_{t+1} = θ_t − η · v_{t+1}
- v_t: momentum (velocity) vector, same units as gradient; β: momentum coefficient (dimensionless, typical 0.9); other symbols as above.
L_reg(θ) = L(θ) + (λ/2) · ||θ||_2^2
- λ: L2 regularisation strength (dimensionless), ||θ||_2: Euclidean norm of θ.
w^* = (X^T X + λ I)^{-1} X^T y (ridge regression closed form)
- X: design matrix (rows = samples, columns = features), unit depends on feature; y: target vector; I: identity matrix; λ ≥ 0.
Convergence condition for quadratic: 0 < η < 2 / λ_max (λ_max: largest eigenvalue of Hessian A)
- λ_max: units follow from the Hessian (loss per parameter^2). In typical ML practice we treat these quantities as dimensionless; verify units if using physical models.
Worked examples
Example 1 — 1D quadratic: stability and numeric iterations
Problem: Let L(θ)=½ a θ^2 with a = 4 (units: 1). Start θ0 = 2. Use gradient descent with η = 0.1. Show first three iterations and check stability condition.
Step 1: gradient ∇L(θ)=a θ = 4 θ.
Update formula: θ_{t+1} = θ_t − η · (4 θ_t) = θ_t · (1 − 4η).
Stability condition: 0 < η < 2/a = 2/4 = 0.5. Our η = 0.1 satisfies this.
Compute iterations (exact formula θ_t = θ0 · (1 − 4η)^t):
- factor = 1 − 4·0.1 = 1 − 0.4 = 0.6.
- θ1 = 2 · 0.6 = 1.2
- θ2 = 1.2 · 0.6 = 0.72
- θ3 = 0.72 · 0.6 = 0.432
Final: after three steps θ3 = 0.432 (unitless).
Example 2 — momentum on same quadratic (numerical first steps)
Problem: Use same L(θ)=½·4 θ^2, θ0 = 2, η = 0.1, momentum β = 0.9, v0 = 0.
Equations: v1 = β v0 + ∇L(θ0) = 0.9·0 + 4·2 = 8. θ1 = θ0 − η v1 = 2 − 0.1·8 = 2 − 0.8 = 1.2.
v2 = 0.9·8 + 4·1.2 = 7.2 + 4.8 = 12.0. θ2 = θ1 − 0.1·12.0 = 1.2 − 1.2 = 0.0.
v3 = 0.9·12.0 + 4·0.0 = 10.8 + 0 = 10.8. θ3 = 0.0 − 0.1·10.8 = −1.08.
Interpretation: momentum amplified the accumulated gradient and caused faster movement (and overshoot). These are exact arithmetic steps; check interplay between η and β in other problems.
Example 3 — L2 regularisation (ridge) closed-form small dataset
Problem: Single-feature linear regression with data x = [1, 2], y = [2, 3]. Use λ = 1.
Formula: w* = (Σ x_i y_i) / (Σ x_i^2 + λ).
Compute: Σ x_i y_i = 1·2 + 2·3 = 2 + 6 = 8. Σ x_i^2 = 1^2 + 2^2 = 1 + 4 = 5. Denominator = 5 + 1 = 6.
w* = 8 / 6 = 1.333333... Final: w = 1.3333 (unitless). Without regularisation (λ=0) w = 8/5 = 1.6, so L2 shrinks weight.
Common mistakes
- Treating learning rate as having SI units — in ML contexts it is unitless; however, derived stability bounds use eigenvalues of Hessian which carry units; state assumptions.
- Forgetting to tune learning rate when switching optimiser (SGD vs Adam behave differently).
- Misunderstanding weight decay vs L2: some implementations apply weight decay to raw parameters (decoupled) whereas others add λ in the loss; results differ with adaptive optimisers.
- For dropout: forgetting to scale activations at test time (or use inverted dropout during training).
- Early stopping: not using a separate validation set (using test set for stopping leaks test information).
For your exam
Typical question types and what full-marks answers include:
- Definitions: clearly define terms (hyperparameter, learning rate, momentum, regularisation). Give units and roles.
- Short notes: explain effect of learning rate on convergence and show the quadratic convergence condition 0 < η < 2/λ_max for convex quadratic losses; mention momentum and how it changes dynamics qualitatively.
- Derivations: derive the stability range for 1D quadratic L(θ)=½ a θ^2 showing factor (1 − a η) and require |1 − a η| < 1 leading to 0 < η < 2/a. For multivariate, replace a with λ_max of Hessian.
- Numericals: compute a few gradient descent and momentum steps (show arithmetic), show effect of L2 regularisation in ridge regression with closed-form solution and compute numeric result.
- Diagrams: draw loss (bowl) with update arrows and training/validation loss vs epochs for early stopping.
Full-marks answers must state assumptions (differentiability, convexity if using quadratic convergence arguments), show algebra step-by-step, and include labelled diagrams where asked.
Quick check
- Define learning rate in one sentence.
- For L(θ)=½·a·θ^2 with a>0, what is the gradient descent stability interval for η?
- Write the closed-form solution for ridge regression weights w* in matrix form.
Answers: 1) The learning rate η is the scalar hyperparameter that multiplies the gradient in parameter updates θ ← θ − η∇L and controls step size. 2) 0 < η < 2/a. 3) w* = (X^T X + λ I)^{-1} X^T y.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?