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.
| Function | Where it is nonsmooth | Valid subgradient choice |
|---|---|---|
| Absolute value |x| | At x = 0 | Any value in [−1, 1], often 0 |
| L1 norm | Where a coordinate is 0 | sign(x) per coordinate, 0 at zero |
| Hinge loss | Where the margin equals 1 | −y·x if margin < 1, otherwise 0 |
| ReLU | At input 0 | Any value in [0, 1], usually 0 |
| Pointwise maximum | Where several pieces tie | Gradient of any active piece |
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.
| Rule | Formula | Guarantee | Practical note |
|---|---|---|---|
| Constant step | α(k) = α | Converges to a neighborhood | Simple, but the result oscillates |
| Constant step length | α(k) = γ / ‖g(k)‖ | Converges to a neighborhood | Scale-invariant to subgradient size |
| Square summable | α(k) = a / (k + 1) | Converges to the optimum | Can 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 rate | Requires the optimal value f* |
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.
Subgradient method compared with related algorithms
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.
| Method | Requirement on the objective | Rate for convex problems | Cost per step |
|---|---|---|---|
| Subgradient method | Convex, Lipschitz continuous | O(1/√k) | One subgradient |
| Gradient descent | Convex, smooth gradient | O(1/k) | One gradient |
| Proximal gradient method | Smooth part plus simple nonsmooth part | O(1/k), O(1/k²) accelerated | Gradient and proximal step |
| Bundle method | Convex, nonsmooth | Often much faster in practice | Small quadratic program |
| Interior-point method | Structured convex programs | Very few iterations | Linear system solve |
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.
| Symptom | Likely cause | Correction |
|---|---|---|
| Objective oscillates and stalls | Constant step too large | Use a diminishing step schedule |
| Reported result is poor | Returning the last iterate | Keep the best point or an average |
| Progress stops early | Steps shrink too fast | Use a / √(k + 1) instead of a / k |
| Stops at wrong point | Small subgradient norm used as test | Use a duality gap or a bound |
| Slow on structured problems | Ignoring a cheap proximal operator | Switch to a proximal gradient method |
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.