Electronics & Telecommunication (University) is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Gradient descent

Gradient descent: iterative algorithm to minimise a differentiable objective by taking steps opposite the gradient; includes batch, stochastic, convergence conditions and practical variants.

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

Gradient descent is the basic optimisation method used to train many machine-learning models (for example, linear regression, logistic regression, neural networks). An engineer uses it to find model parameters that minimise a loss function when closed-form solutions are impossible or expensive. Understanding step-size, convergence, and stochastic variants is essential to implement stable training and tune models for real datasets.

Key ideas

  • Gradient: ∇f(θ) is the vector of partial derivatives of f at θ. It points in the direction of steepest increase. We define it as ∇f(θ) = (∂f/∂θ1, ..., ∂f/∂θd)^T.
  • Stationary point: a point θ* where ∇f(θ) = 0*. For differentiable convex f, any stationary point is a global minimizer.
  • Learning rate (step size): α > 0 is a scalar multiplier controlling step length.
  • Gradient descent (GD) update rule: from θ_k produce θ_{k+1} = θ_k − α ∇f(θ_k). This moves opposite the gradient (steepest descent).

Derivation of the update (common exam derivation):

  1. Take first-order Taylor expansion of f around θ_k: f(θ_k + p) ≈ f(θ_k) + ∇f(θ_k)^T p + O(‖p‖^2).
  2. Restrict to a step p with fixed norm or small magnitude; to decrease the linear term choose p in direction −∇f(θ_k). The minimizing direction for the linear approximation is p = −α ∇f(θ_k) for some scalar α > 0.
  3. That yields the gradient descent update θ_{k+1} = θ_k − α ∇f(θ_k).

Assumptions and convergence statements used in exams (state precisely):

  • Differentiability: f is continuously differentiable (C^1) on R^d.
  • Lipschitz gradient (L-smooth): There exists L > 0 such that for all x,y, ‖∇f(x) − ∇f(y)‖ ≤ L‖x − y‖. For twice-differentiable f, L can be taken as an upper bound on the operator norm of the Hessian over the domain (e.g., L ≥ sup_x λ_max(∇^2 f(x))).
  • Strong convexity (μ-strongly convex): f satisfies for μ > 0 and all x,y: f(y) ≥ f(x) + ∇f(x)^T (y−x) + (μ/2)‖y−x‖^2.

Common convergence results (exact, exam-style):

  • For L-smooth (possibly nonconvex) f and fixed 0 < α ≤ 1/L, GD ensures descent f(θ_{k+1}) ≤ f(θ_k) and a gradient-norm rate: min_{t<k} ‖∇f(θ_t)‖^2 ≤ (2L/k)(f(θ_0) − f*).
  • If f is convex and L-smooth, gradient descent with fixed 0 < α ≤ 1/L gives function-value convergence O(1/k) where k is iteration count (specifically f(θ_k) − f* = O(1/k)).
  • If f is μ-strongly convex and L-smooth, choosing α = 1/L yields linear convergence: f(θ_k) − f* ≤ (1 − μ/L)^k (f(θ_0) − f*) (geometric decay).

Special-case derivation for a quadratic (common exam question): let f(θ) = (1/2) θ^T A θ − b^T θ + c with symmetric positive-definite A. Then ∇f(θ) = A θ − b. GD update becomes θ_{k+1} = (I − α A) θ_k + α b. Convergence requires eigenvalues λ_i(A) and step-size satisfying 0 < α < 2 / λ_max(A). The asymptotic rate is governed by spectral radius ρ(I − α A) = max_i |1 − α λ_i|. The optimal fixed step for this quadratic is α* = 2/(λ_max(A) + λ_min(A)), yielding rate factor ((κ−1)/(κ+1))^k in the error norm, where κ = λ_max(A)/λ_min(A).

What to draw (diagram asked in exams): draw 2D contour (level curves) of a convex quadratic (ellipses). Mark starting point θ_0. Draw arrows for successive gradient descent steps: arrows should be orthogonal to contour lines (pointing towards smaller contours). Label axes θ1, θ2, and contours f(θ) = const. Show gradient vectors at θ_k pointing outward and descent steps in opposite direction.

Variants (define before use):

  • Batch gradient descent: compute ∇f using full dataset (exact gradient for empirical loss).
  • Stochastic gradient descent (SGD): use gradient estimate from a single randomly sampled example; update θ ← θ − α ∇ℓ_i(θ). This has higher variance but cheaper per step and can converge faster in practice on large data. For unbiased stochastic gradients, convergence to a stationary point typically requires decreasing step sizes satisfying Σ_k α_k = ∞ and Σ_k α_k^2 < ∞.
  • Mini-batch SGD: use gradient from a small subset (batch) of data balancing variance and cost.
  • Momentum (heavy-ball): track velocity v_{k+1} = β v_k + ∇f(θ_k); θ_{k+1} = θ_k − α v_{k+1}. This accelerates along consistent directions. (Also mention Nesterov accelerated gradient as a variant with lookahead.)

Practical line-search rules to choose α briefly defined: Armijo (backtracking) and Wolfe conditions — use these when exact step-size is needed for theoretical guarantees.

Where unsure: constants in sharpest O(1/k) bounds and precise multiplicative constants in non-strongly-convex case; please verify references below.

Formulas

θ_{k+1} = θ_k − α ∇f(θ_k) — θ_k: parameter vector at iteration k (units depend on model), ∇f units same as f per unit parameter, α: learning rate (units = parameter units^2 / loss units if tracking physical units), applies to differentiable f. ∇f(θ) = (∂f/∂θ1, ..., ∂f/∂θd)^T — gradient components, units = loss per parameter unit, applies when f is differentiable. f(θ) ≈ f(θ_k) + ∇f(θ_k)^T (θ − θ_k) + (1/2)(θ − θ_k)^T H(ξ) (θ − θ_k) — second-order Taylor with Hessian H, units of f. 0 < α < 2 / λ_max(A) — step-size condition for quadratic f(θ) = (1/2) θ^T A θ − b^T θ + c, where λ_max(A) is the largest eigenvalue of A (units: loss per parameter^2), applies when A is positive-definite. If f is μ-strongly convex and L-smooth then with α = 1/L: f(θ_k) − f* ≤ (1 − μ/L)^k (f(θ_0) − f*) — μ, L positive constants, applies under stated convexity and smoothness assumptions. Batch GD per-iteration cost ≈ O(n·d) — n = number of samples, d = parameters, units = operations, applies to empirical loss. SGD update: θ ← θ − α ∇ℓ_i(θ) — where ℓ_i is loss for sample i, applies per-sample update.

Worked examples

Example 1 — one-dimensional quadratic (numerical, units): Problem: Minimise f(w) = w^2 − 4w + 5. Here w has units metres (m), so f has units m^2. Step 1: compute derivative: f'(w) = 2w − 4. Step 2: gradient descent update with α = 0.1 (dimensionless here since f has units m^2 and w has units m): w_{k+1} = w_k − 0.1 * (2 w_k − 4). Start w_0 = 0 m. Iteration 1: w_1 = 0 − 0.1*(−4) = 0.4 m. Iteration 2: w_2 = 0.4 − 0.1*(0.8 − 4) = 0.4 − 0.1*(−3.2) = 0.72 m. Iteration 3: w_3 = 0.72 − 0.1*(1.44 − 4) = 0.72 − 0.1*(−2.56) = 0.976 m. The analytic minimiser is w* = 2 m. After 3 iterations we approach it; values are 0.4 m, 0.72 m, 0.976 m.

Example 2 — simple linear regression fitted with gradient descent (numerical with units): Dataset (x in metres, y in metres): (1, 1.8), (2, 3.2), (3, 4.9). Model ŷ = w0 + w1 x. Use MSE loss J(w) = (1/(2n)) Σ (ŷ_i − y_i)^2. Step 1: n = 3. Initial w0 = 0 m, w1 = 0 (dimensionless). Learning rate α = 0.1. Step 2: compute errors at start: e = [−1.8, −3.2, −4.9]. Gradients: ∂J/∂w0 = (1/n) Σ e_i = (−1.8 −3.2 −4.9)/3 = −3.3 (units: m; J has units m^2 and w0 has units m, so ∂J/∂w0 has units m). ∂J/∂w1 = (1/n) Σ e_i x_i = (−1.8*1 −3.2*2 −4.9*3)/3 = −22.9/3 = −7.633333... (units: m^2; J has units m^2 and w1 is dimensionless). Step 3: updates: w0_new = 0 − 0.1*(−3.3) = 0.33 m. w1_new = 0 − 0.1*(−7.633333) = 0.7633333. Compute new MSE J_new = 1/(2*3)*Σ errors^2 ≈ 1.2503865 (m^2). Initial J_initial ≈ 6.248333 m^2. GD reduced the loss. Final parameters after one update: w0 = 0.33 m, w1 = 0.763333.

(Third example omitted to keep notes focused; reviewer: add a SGD example if needed.)

Common mistakes

  • Using an α that is too large: divergence or oscillation (especially for quadratic problems where condition 0 < α < 2/λ_max is required).
  • Mixing per-sample gradients and batch gradients without the 1/n factor when using MSE and comparing to closed-form solutions.
  • Incorrect units or ignoring units when interpreting learning rate; treating α as dimensionless blindly can confuse physical intuition.
  • For strong convexity bounds: swapping μ and L or using incorrect rate constants (verify which is smallest/largest).
  • Not checking if f is differentiable or L-smooth before quoting convergence theorems.

For your exam

  • Short notes: explain SGD vs batch GD; momentum and why it helps.
  • Derivations: derive GD update from Taylor expansion; show condition for convergence for 1D/quadratic and sketch proof for spectral radius condition.
  • Numericals: carry out 2–3 iterations of GD on a small quadratic or linear regression dataset (show steps and units).
  • Diagrams: draw contour plot of quadratic loss and show descent path; label gradients and updates. What full-marks answers include: precise definitions with assumptions, step-by-step derivation, correctly computed numeric iterations with units, and clear labelled diagram if requested.

Quick check

  1. Write the gradient descent update rule for parameter vector θ.
  2. State the Lipschitz-gradient (L-smooth) condition.
  3. For f(θ) = (1/2) θ^T A θ − b^T θ with A positive-definite, what range of α guarantees convergence of GD? Answers: 1) θ_{k+1} = θ_k − α ∇f(θ_k). 2) ‖∇f(x) − ∇f(y)‖ ≤ L ‖x − y‖ for some L > 0. 3) 0 < α < 2 / λ_max(A).

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?