Skip to main content
© 2026 ePowerAI. All rights reserved.
CoursesContact
eAI
CoursesContact
Week 10: Nonsmooth and Composite Objectives
Optimization
01Week 1: Optimization for Learning
02Week 2: Convex Sets and Functions
03Week 3: Gradient Descent Theory
04Week 4: Least Squares and Conditioning
05Week 5: Classification Losses
06Week 6: Stochastic Gradient Descent
07Week 7: Momentum and Acceleration
08Week 8: Adaptive First-Order Methods
09Week 9: Practical Training Dynamics
10Week 10: Nonsmooth and Composite Objectives
11Week 11: Constraints: Projections and Penalties
12Week 12: Duality for Practitioners
13Week 13: Nonconvex Deep Learning
14Week 14: Capstone — Theory Meets Training
Week 10· Optimization· Structure22 min read

Week 10: Nonsmooth and Composite Objectives

✦Learning Outcomes
  • Define the subgradient of a convex function at a nondifferentiable point
  • Compute subgradients for ∣x∣|x|∣x∣, ReLU, and max functions
  • Write the proximal gradient step for composite f(x)=g(x)+h(x)f(x) = g(x) + h(x)f(x)=g(x)+h(x) with smooth ggg and nonsmooth hhh
  • Explain why early stopping implicitly regularises in overparameterised settings
◆Prerequisites

Background: Weeks 1–3 (GD, convexity, subgradients of convex functions). Week 2's convexity definitions extend directly to nondifferentiable functions via subgradients.

Later weeks: Proximal methods preview the constrained optimisation of Week 11 (projected GD) and the operator-splitting ideas in Advanced Optimization (ADMM, proximal Newton).

When Gradients Don't Exist#

Not every useful loss function is differentiable everywhere. Common nondifferentiable functions in ML:

  • f(x)=∣x∣f(x) = |x|f(x)=∣x∣ — nondifferentiable at x=0x = 0x=0 (the kink)
  • ReLU(x)=max⁡(0,x)\text{ReLU}(x) = \max(0, x)ReLU(x)=max(0,x) — nondifferentiable at x=0x = 0x=0
  • f(x)=∥x∥1f(x) = \|x\|_1f(x)=∥x∥1​ — the ℓ1\ell_1ℓ1​ norm, nondifferentiable on the coordinate axes
  • f(x)=max⁡(x1,x2)f(x) = \max(x_1, x_2)f(x)=max(x1​,x2​) — nondifferentiable when x1=x2x_1 = x_2x1​=x2​

These functions are convex (Week 2's Jensen inequality still holds), so we can extend the notion of "gradient" to a set-valued generalisation.

Subgradients#

For a convex function fff, a vector ggg is a subgradient at xxx if:

f(y)≥f(x)+gT(y−x)for all yf(y) \geq f(x) + g^T (y - x) \quad \text{for all } yf(y)≥f(x)+gT(y−x)for all y

This is the same first-order condition from Week 2, but now ggg is not unique. The set of all subgradients at xxx is the subdifferential ∂f(x)\partial f(x)∂f(x).

Worked examples:

  • f(x)=∣x∣f(x) = |x|f(x)=∣x∣: ∂f(0)=[−1,1]\partial f(0) = [-1, 1]∂f(0)=[−1,1] (any value between −1-1−1 and 111 works). For x>0x > 0x>0, ∂f(x)={1}\partial f(x) = \{1\}∂f(x)={1}; for x<0x < 0x<0, ∂f(x)={−1}\partial f(x) = \{-1\}∂f(x)={−1}.

  • ReLU(x)=max⁡(0,x)\text{ReLU}(x) = \max(0, x)ReLU(x)=max(0,x): ∂f(0)=[0,1]\partial f(0) = [0, 1]∂f(0)=[0,1]. For x>0x > 0x>0, ∂f(x)={1}\partial f(x) = \{1\}∂f(x)={1}; for x<0x < 0x<0, ∂f(x)={0}\partial f(x) = \{0\}∂f(x)={0}.

  • f(x)=max⁡(x1,x2)f(x) = \max(x_1, x_2)f(x)=max(x1​,x2​): At x1=x2x_1 = x_2x1​=x2​, ∂f(x)={(θ,1−θ)∣θ∈[0,1]}\partial f(x) = \{(\theta, 1-\theta) \mid \theta \in [0, 1]\}∂f(x)={(θ,1−θ)∣θ∈[0,1]} — the convex combinations of the two active gradients.

The subgradient method is the direct extension of GD:

xk+1=xk−ηkgkwhere gk∈∂f(xk)x_{k+1} = x_k - \eta_k g_k \quad \text{where } g_k \in \partial f(x_k)xk+1​=xk​−ηk​gk​where gk​∈∂f(xk​)

But it has a major drawback: the subgradient method is not a descent method. Unlike GD, taking a subgradient step does not guarantee f(xk+1)<f(xk)f(x_{k+1}) < f(x_k)f(xk+1​)<f(xk​). The function value can increase. For this reason, the subgradient method is less popular in ML than proximal methods.

Composite Objectives#

Many important problems have the form:

min⁡x  f(x)=g(x)⏟smooth+h(x)⏟nonsmooth\min_x \; f(x) = \underbrace{g(x)}_{\text{smooth}} + \underbrace{h(x)}_{\text{nonsmooth}}minx​f(x)=smoothg(x)​​+nonsmoothh(x)​​

The classic example is the lasso:

min⁡w  12∥Xw−y∥22+λ∥w∥1\min_w \; \frac{1}{2}\|Xw - y\|_2^2 + \lambda \|w\|_1minw​21​∥Xw−y∥22​+λ∥w∥1​

where g(w)=12∥Xw−y∥22g(w) = \frac{1}{2}\|Xw - y\|_2^2g(w)=21​∥Xw−y∥22​ is LLL-smooth (least squares) and h(w)=λ∥w∥1h(w) = \lambda \|w\|_1h(w)=λ∥w∥1​ is nonsmooth (sparsity-inducing regulariser).

The ℓ1\ell_1ℓ1​ penalty encourages sparse solutions: many wiw_iwi​ become exactly zero, which performs automatic feature selection.

Proximal Gradient (lite)#

The proximal gradient method (also called ISTA) elegantly handles composite objectives:

xk+1=proxηh(xk−η∇g(xk))x_{k+1} = \text{prox}_{\eta h}\big(x_k - \eta \nabla g(x_k)\big)xk+1​=proxηh​(xk​−η∇g(xk​))

where the proximal operator of hhh is:

proxηh(v)=arg⁡min⁡x{h(x)+12η∥x−v∥22}\text{prox}_{\eta h}(v) = \arg\min_x \left\{ h(x) + \frac{1}{2\eta}\|x - v\|_2^2 \right\}proxηh​(v)=argminx​{h(x)+2η1​∥x−v∥22​}

This takes a gradient step on the smooth part ggg, then "projects" the result through the proximal operator of hhh.

The key example — soft thresholding for ℓ1\ell_1ℓ1​: For h(x)=λ∥x∥1h(x) = \lambda \|x\|_1h(x)=λ∥x∥1​ (vector case):

proxηλ∥⋅∥1(v)i=sign(vi)⋅max⁡(∣vi∣−ηλ,0)\text{prox}_{\eta \lambda \|\cdot\|_1}(v)_i = \text{sign}(v_i) \cdot \max(|v_i| - \eta\lambda, 0)proxηλ∥⋅∥1​​(v)i​=sign(vi​)⋅max(∣vi​∣−ηλ,0)

This shrinks each coordinate toward zero by ηλ\eta\lambdaηλ, and sets it to exactly zero if it was within that threshold. This is why ISTA produces sparse solutions — coordinates smaller than the threshold vanish.

Worked example — 1D lasso step:

Take g(w)=12(w−3)2g(w) = \frac{1}{2}(w - 3)^2g(w)=21​(w−3)2 (gradient ∇g(w)=w−3\nabla g(w) = w - 3∇g(w)=w−3), h(w)=2∣w∣h(w) = 2|w|h(w)=2∣w∣, η=0.5\eta = 0.5η=0.5.

  • Gradient step: v=wk−η∇g(wk)v = w_k - \eta \nabla g(w_k)v=wk​−η∇g(wk​). If wk=1w_k = 1wk​=1, v=1−0.5⋅(−2)=2v = 1 - 0.5 \cdot (-2) = 2v=1−0.5⋅(−2)=2.
  • Proximal step: soft-threshold v=2v = 2v=2 with ηλ=0.5⋅2=1\eta\lambda = 0.5 \cdot 2 = 1ηλ=0.5⋅2=1. The result is sign(2)⋅max⁡(2−1,0)=1\text{sign}(2) \cdot \max(2-1, 0) = 1sign(2)⋅max(2−1,0)=1.

The iterate moved from wk=1w_k = 1wk​=1 to wk+1=1w_{k+1} = 1wk+1​=1 — stopped at the threshold. If the gradient were stronger, the iterate would cross the threshold and move.

Early Stopping as Regularization#

An intriguing phenomenon: running GD on the unregularised least squares problem

min⁡w12∥Xw−y∥22\min_w \frac{1}{2}\|Xw - y\|_2^2minw​21​∥Xw−y∥22​

and stopping early (after a small number of iterations) produces a solution that behaves similarly to ridge regression. The early-stopped GD solution has smaller norm and better generalisation than the fully-converged solution.

Intuition: GD started from w0=0w_0 = 0w0​=0 explores the parameter space from the origin outward. The first few iterations capture large-scale, low-curvature directions (principal components); later iterations fit fine-grained, high-curvature directions that often correspond to noise. Stopping early implicitly truncates the high-frequency fitting.

This is a special case of implicit regularisation — the optimisation algorithm itself, not an explicit penalty term, biases the solution. This theme returns in Week 13 (nonconvex deep learning).

Exercise · Fill in the blank

The subgradient of $|x|$ at $x = 0$ is any value in the interval ___.

Question 1 of 4

A vector g is a subgradient of convex f at x if:

f(y) ≥ f(x) + g^T(y−x) for all y
f(y) ≤ f(x) + g^T(y−x) for all y
g = ∇f(x)
f is differentiable at x

Browser lab#

Compare the subgradient method vs proximal gradient (ISTA) on L1-regularised least squares (lasso). Plot objective vs iteration.

python · runs in browser
import numpy as np
import matplotlib.pyplot as plt

rng = np.random.default_rng(42)
n, d = 100, 50
w_true = rng.normal(size=d) * (rng.random(d) > 0.7)
X = rng.normal(size=(n, d))
y = X @ w_true + rng.normal(scale=0.3, size=n)

lam = 0.5
L = np.linalg.eigvalsh(X.T @ X / n).max()
eta = 1.0 / L
K = 200

def soft_thresh(v, thresh):
    return np.sign(v) * np.maximum(np.abs(v) - thresh, 0.0)

def objective(w):
    return 0.5 * np.mean((X @ w - y)**2) + lam * np.sum(np.abs(w))

w_sg = np.zeros(d)
loss_sg = [objective(w_sg)]
for k in range(1, K+1):
    g = X.T @ (X @ w_sg - y) / n
    subg = g + lam * np.sign(w_sg)
    subg[np.abs(w_sg) < 1e-10] = g[np.abs(w_sg) < 1e-10] + lam * np.sign(g[np.abs(w_sg) < 1e-10])
    w_sg = w_sg - (eta / np.sqrt(k)) * subg
    loss_sg.append(objective(w_sg))

w_ista = np.zeros(d)
loss_ista = [objective(w_ista)]
for _ in range(K):
    grad_g = X.T @ (X @ w_ista - y) / n
    w_ista = soft_thresh(w_ista - eta * grad_g, eta * lam)
    loss_ista.append(objective(w_ista))

plt.figure(figsize=(8, 5))
plt.plot(loss_sg, label="Subgradient method (η_k = η₀/√k)", alpha=0.8)
plt.plot(loss_ista, label="ISTA (proximal gradient)", linewidth=2)
plt.yscale("log"); plt.xlabel("Iteration"); plt.ylabel("Objective f(w)")
plt.title("Subgradient Method vs ISTA on Lasso")
plt.legend(); plt.grid(True, alpha=0.3); plt.show()
← Previous
Week 9: Practical Training Dynamics
Next →
Week 11: Constraints: Projections and Penalties
On this page
  • When Gradients Don't Exist
  • Subgradients
  • Composite Objectives
  • Proximal Gradient (lite)
  • Early Stopping as Regularization
  • Browser lab