Knowledge / Artificial Intelligence and Data Science / Optimisation for machine learning

Optimisation for machine learning

Artificial Intelligence and Data Science (B.E./B.Tech) · Mathematics and statistics for AI · free study notes for exams, placements and interviews

In short

Optimisation is the machinery that actually *fits* a machine-learning model: you write the learning task as "minimise a loss function over parameters, possibly subject to constraints", then choose an algorithm that finds the minimum reliably and cheaply. It decides whether a model trains at all, how fast, and how well it generalises — the loss, the optimiser, the learning rate and the regularisation penalty are all optimisation objects. Indian university AI&DS syllabi (AICTE model curriculum, UGC CBCS) examine convexity, gradient and Newton-type methods, Lagrange multipliers, KKT conditions, duality and the modern SGD family; GATE DA (Data Science and AI, introduced 2024) has a dedicated "Machine Learning" and linear-algebra/calculus block where convexity and gradient methods appear. Interviewers at TCS Research, Fractal, Mu Sigma, ZS, Flipkart, Swiggy, Nvidia and Qualcomm India ask why Adam is the default, what the learning rate does, and why L1 gives sparsity.

Explained

The optimisation problem behind learning

Convexity — why it is the dividing line

Gradient-based first-order methods

  1. Batch gradient descent: θ ← θ − η∇J(θ) using all n samples per step. Smooth, deterministic descent; cost O(n) per step, so impractical on crores of rows.
  2. Stochastic gradient descent (SGD): one random sample per update. Cheap and noisy; the noise helps escape shallow local minima and saddles but prevents exact convergence, so the step size must decay.
  3. Mini-batch SGD: the practical default — batches of 32–512 samples; vectorised on GPU, variance of the gradient falls as 1/B.
  4. Learning rate η is the single most important hyper-parameter: too large → divergence or oscillation across the valley; too small → crawling, and stalling in flat regions.
  5. Convergence (theory): for convex, L-smooth f, gradient descent with η ≤ 1/L gives f(θₖ) − f* = O(1/k); strongly convex gives linear (geometric) rate; SGD with decaying η gives O(1/k) for strongly convex, O(1/√k) convex.
  6. Condition number κ = λmax/λmin of the Hessian governs speed: a large κ means elongated contours and zig-zagging — the standard reason to standardise features.
  7. Momentum: v ← βv + ∇J(θ); θ ← θ − ηv (β ≈ 0.9). Averages past gradients, damping oscillation across the narrow axis and accelerating along the consistent one.
  8. Nesterov accelerated gradient (NAG): evaluates the gradient at the look-ahead point θ − ηβv, giving a provable O(1/k²) rate for smooth convex problems.

Adaptive methods and schedules

Second-order and derivative-free methods

Constrained optimisation: Lagrange, KKT and duality

Linear programming and the operations-research thread

Key points

Formulas and examples

Taylor model: f(θ + Δ) ≈ f(θ) + ∇f(θ)ᵀΔ + ½ΔᵀHΔ — ∇f is the gradient (n×1), H the Hessian (n×n)
Gradient descent: θₖ₊₁ = θₖ − η ∇J(θₖ) — η = learning rate (dimensionless scaling on parameter units)
Mini-batch SGD: θ ← θ − (η/B) Σ_{i∈batch} ∇L(θ; xᵢ, yᵢ) — B = batch size (samples)
Momentum: vₖ = βvₖ₋₁ + ∇J(θₖ); θₖ₊₁ = θₖ − ηvₖ — β = momentum coefficient, 0 ≤ β < 1
Adam: mₖ = β₁mₖ₋₁ + (1−β₁)gₖ; vₖ = β₂vₖ₋₁ + (1−β₂)gₖ²; m̂ = mₖ/(1−β₁ᵏ); v̂ = vₖ/(1−β₂ᵏ); θₖ₊₁ = θₖ − η m̂/(√v̂ + ε)
Newton step: θₖ₊₁ = θₖ − H(θₖ)⁻¹ ∇f(θₖ)
Ridge (L2) solution: θ = (XᵀX + λI)⁻¹Xᵀy — X is n×p, λ ≥ 0 the penalty strength
Lagrangian with inequalities: L(x, μ, λ) = f(x) + Σμᵢgᵢ(x) + Σλⱼhⱼ(x), μᵢ ≥ 0
Complementary slackness: μᵢ gᵢ(x*) = 0 for every i
Convergence of GD on L-smooth convex f: f(θₖ) − f* ≤ (2L‖θ₀ − θ*‖²)/(k + 4), valid for η ≤ 1/L
Exponential LR decay: ηₖ = η₀ e−kt; step decay: ηₖ = η₀ γ^⌊k/s⌋; cosine: ηₖ = ½η₀(1 + cos(πk/K))

Example 1 — gradient descent by hand, and the effect of η. Minimise f(θ) = θ² − 4θ + 5, so f′(θ) = 2θ − 4; the exact minimum is θ* = 2, f* = 1. With θ₀ = 0, η = 0.1: update θ ← θ − 0.1(2θ − 4) = 0.8θ + 0.4.

Now η = 1.1: θ ← θ − 1.1(2θ − 4) = −1.2θ + 4.4. θ₁ = 4.4, θ₂ = −0.88, θ₃ = 5.456 — the error multiplies by |−1.2| = 1.2, so it diverges. Here f″ = 2 = L, and stability needs η < 2/L = 1; η = 0.1 is safe, η = 1.1 is not.

Example 2 — convexity and a saddle point from the Hessian. (a) f(x, y) = x² + 3y² + xy. Hessian H = [[2, 1], [1, 6]]; trace = 8 > 0 and det = 2×6 − 1 = 11 > 0, so both eigenvalues are positive → positive definite → strictly convex, and the unique stationary point is a global minimum. Solving ∇f = 0: 2x + y = 0 and 6y + x = 0 → x = y = 0, f = 0. (b) f(x, y) = x² − y². H = [[2, 0], [0, −2]], eigenvalues +2 and −2 → indefinite, so (0,0) is a saddle point: a minimum along x, a maximum along y. Gradient descent started exactly on the y = 0 line would stall there. Condition number of (a): eigenvalues solve μ² − 8μ + 11 = 0 → μ = 4 ± √5 = 6.236 and 1.764, so κ ≈ 3.54 — mildly elongated contours.

Example 3 — Lagrange multipliers and the shadow price. Minimise f(x, y) = x² + y² subject to x + 2y = 10. L = x² + y² + λ(x + 2y − 10). ∂L/∂x = 2x + λ = 0 → x = −λ/2; ∂L/∂y = 2y + 2λ = 0 → y = −λ. Substituting: (−λ/2) + 2(−λ) = 10 → −2.5λ = 10 → λ = −4. So x = 2, y = 4, f = 4 + 16 = 20. Shadow price check: with the right-hand side b, f* = b²/5, so df*/db = 2b/5 = 4 at b = 10 — equal to −λ, confirming the multiplier measures sensitivity to the constraint (sign depends on the Lagrangian convention).

Example 4 — one Adam step versus one SGD step. Take g₁ = 0.1 at the first iteration, η = 0.001, β₁ = 0.9, β₂ = 0.999, ε = 1e−8.

Example 5 — mini-batch economics on an Indian-scale dataset. A UPI fraud model has n = 50,00,000 (50 lakh) transactions; one gradient per sample costs ~1 µs of GPU time.

MethodMemoryCost per stepConvergenceTypical use
Batch GDO(n) data passHighLinear (strongly convex)Small, convex problems
SGDO(1)Very lowO(1/√k)Online / streaming
Mini-batch + momentumO(B)LowFast in practiceCNNs, vision
Adam / AdamW2 extra arraysLowFast, robustNLP, transformers, default
L-BFGSO(mn)MediumSuperlinearLogistic regression, medium convex
NewtonO(n²)O(n³)QuadraticFew parameters only
PenaltyTerm addedEffect on θSolution path
L2 (Ridge)λΣθⱼ²Shrinks all towards 0Closed form, smooth
L1 (Lasso)λΣ\θⱼ\Sets some exactly 0Coordinate descent / LARS
Elastic netλ₁Σ\θⱼ\+ λ₂Σθⱼ²Sparse + groupedCoordinate descent
# Mini-batch SGD with momentum, written out - no library optimiser
v = np.zeros_like(theta)
for epoch in range(epochs):
    idx = np.random.permutation(n)
    for s in range(0, n, B):
        xb, yb = X[idx[s:s+B]], y[idx[s:s+B]]
        g = (xb.T @ (xb @ theta - yb)) / len(yb)   # MSE gradient
        v = 0.9 * v + g
        theta -= lr * v
    lr *= 0.95                                      # exponential decay

Where it is used

Interview questions

Common mistakes

Related topics

Calculus and gradient descent · Linear algebra for machine learning · Supervised learning: regression and classification · Support vector machines and kernels · Deep learning and neural networks · Model evaluation and regularisation

Open in the Knowledge reader →Find jobs →