Subgradient method

On this page 7

The subgradient method is an iterative algorithm for minimizing a convex function that is not differentiable everywhere, in which each step moves against a subgradient instead of a gradient. In optimization for artificial intelligence, it is the simplest way to train models whose objectives contain kinks, such as the hinge loss of support vector machines, L1 regularization and piecewise linear penalties.

The method was developed in the 1960s and 1970s, notably by Naum Shor and Boris Polyak, and it remains a reference point in convex optimization. It is cheap per iteration and easy to implement, but it is not a descent method and converges slowly, which is why modern practice often replaces it with proximal or smoothing techniques when the structure of the problem allows.

How the subgradient method works

The method generalizes gradient descent by replacing the gradient with any element of the subdifferential. For a convex function f, a vector g is a subgradient at point x when f(y) ≥ f(x) + gᵀ(y − x) holds for every y. Geometrically, g defines a supporting hyperplane that lies below the graph of f and touches it at x.

At a smooth point the subdifferential contains only the gradient. At a kink it is a convex set of slopes; for the absolute value |x| at zero, it is the whole interval [−1, 1]. The iteration is x(k+1) = x(k) − α(k) g(k), where α(k) is the step size and g(k) is any subgradient at x(k).

Because a subgradient direction does not always decrease the objective, the algorithm tracks the best point found so far. The guarantee is that the distance to the set of minimizers shrinks for small enough steps, even when the function value temporarily rises. This property follows from convexity and is the core of every convergence proof for the method.

The following table lists common nonsmooth functions in machine learning and a valid subgradient at their kinks.

FunctionWhere it is nonsmoothValid subgradient choice
Absolute value |x|At x = 0Any value in [−1, 1], often 0
L1 normWhere a coordinate is 0sign(x) per coordinate, 0 at zero
Hinge lossWhere the margin equals 1−y·x if margin < 1, otherwise 0
ReLUAt input 0Any value in [0, 1], usually 0
Pointwise maximumWhere several pieces tieGradient of any active piece
Subgradients of common nonsmooth functions

Step size rules and convergence rates

The step size rule decides whether the method converges at all. A constant step only guarantees that the best value ends within a band around the optimum whose width grows with the step. To reach the exact optimum, the steps must shrink to zero while their sum still diverges, as in the classic choice α(k) = a / √(k + 1).

With such diminishing steps and bounded subgradients, the gap between the best value and the optimum falls at a rate of O(1/√k). In practice this means about 1/ε² iterations to reach accuracy ε. This rate is optimal for first-order methods on general nonsmooth convex functions, so no algorithm that only queries subgradients can do fundamentally better on that class.

RuleFormulaGuaranteePractical note
Constant stepα(k) = αConverges to a neighborhoodSimple, but the result oscillates
Constant step lengthα(k) = γ / ‖g(k)‖Converges to a neighborhoodScale-invariant to subgradient size
Square summableα(k) = a / (k + 1)Converges to the optimumCan become too slow early on
Diminishingα(k) = a / √(k + 1)Converges at O(1/√k)The most common default
Polyak step(f(x) − f*) / ‖g‖²Converges at the optimal rateRequires the optimal value f*
Step size rules and what they guarantee

The Polyak step is attractive when the optimal value is known, for example in feasibility problems where it equals zero. When f* is unknown, practitioners use an estimate that is updated as better points appear.

The subgradient method sits at the slow but general end of first-order algorithms. Methods that exploit more structure converge faster, but they also require more from the problem, such as smoothness or a cheap proximal operator.

MethodRequirement on the objectiveRate for convex problemsCost per step
Subgradient methodConvex, Lipschitz continuousO(1/√k)One subgradient
Gradient descentConvex, smooth gradientO(1/k)One gradient
Proximal gradient methodSmooth part plus simple nonsmooth partO(1/k), O(1/k²) acceleratedGradient and proximal step
Bundle methodConvex, nonsmoothOften much faster in practiceSmall quadratic program
Interior-point methodStructured convex programsVery few iterationsLinear system solve
Comparison with neighboring optimization methods

The stochastic subgradient method replaces the exact subgradient with an estimate from a mini-batch. The same O(1/√k) rate holds in expectation, which explains why stochastic training of nonsmooth models such as linear support vector machines works well. The Pegasos algorithm is a well-known instance.

Where subgradients appear in artificial intelligence

Subgradients appear wherever piecewise linear components enter a model or a loss. The most direct application is training linear classifiers with the hinge loss, where a stochastic subgradient step processes one example at a time and scales to very large datasets.

  • Sparse regression: the Lasso objective has an L1 term whose subgradient is the sign vector, although proximal methods usually solve it better.
  • Lagrangian duality: the dual function of an integer program is concave and nonsmooth, and dual subgradient ascent updates the multipliers in Lagrangian relaxation.
  • Neural networks: ReLU activations are nonsmooth, and automatic differentiation frameworks silently return a subgradient-like value at zero.
  • Robust and quantile losses: absolute error and pinball losses use the same sign-based subgradient rules.

In deep learning the objective is not convex, so the classical guarantees do not apply. Frameworks still rely on the same idea of choosing one element of a generalized derivative, formalized as the Clarke subdifferential.

Limitations and common mistakes

The main limitation is slow convergence with zigzagging near kinks. Because the subgradient magnitude does not shrink near a nonsmooth minimum, a fixed step keeps overshooting. The method also provides no natural stopping criterion, since a small subgradient norm does not signal optimality.

SymptomLikely causeCorrection
Objective oscillates and stallsConstant step too largeUse a diminishing step schedule
Reported result is poorReturning the last iterateKeep the best point or an average
Progress stops earlySteps shrink too fastUse a / √(k + 1) instead of a / k
Stops at wrong pointSmall subgradient norm used as testUse a duality gap or a bound
Slow on structured problemsIgnoring a cheap proximal operatorSwitch to a proximal gradient method
Typical mistakes and how to correct them

A frequent misconception is that a subgradient step always lowers the loss. Only the distance to the optimum decreases, and only for suitable steps. Averaging iterates, as in Polyak-Ruppert averaging, gives a more stable output for stochastic variants.

In plain words

Imagine walking down into a valley to find its lowest point. On a smooth slope, the ground tells you exactly which way is down. In a valley with sharp folds and creases, the slope at a crease is unclear.

The subgradient method solves this by picking any reasonable downhill direction at a crease and taking a step. Some steps go slightly wrong. Still, if the steps get smaller over time, you end up close to the bottom. You remember the lowest spot you have seen, because the last step is not always the best one.

It is a simple and reliable tool. Its weakness is speed: it needs many small steps to get very close.

A real-world example

A team at an online retailer trains a linear spam classifier on 50 million short product reviews, each described by about one million sparse word features. The hinge loss with L2 regularization fits the task, but the loss has a kink at every margin of exactly one, so plain gradient descent is not defined there.

The engineers use a stochastic subgradient method in the style of Pegasos. Each step samples 256 reviews, computes the hinge loss subgradient for the misclassified or low-margin examples, and applies a step size of 1 / (λk) with λ set to 0.0001. They report the averaged weight vector instead of the last iterate.

After five passes over the data, which take under 20 minutes on one server, validation accuracy stops improving. The model matches the accuracy of a batch solver that needed several hours, showing why the method remains useful for large and sparse nonsmooth problems.