AI State of the Art
Math/Optimization/12 min
Section 332

Constrained Optimization: Lagrange and KKT

Listen as a podcast →

Why constraints?

In machine learning, we don't simply seek to minimize a function. We want to do it while respecting rules. Probabilities must sum to 1. Embeddings must have unit norm. An SVM's margin must be maximal while correctly classifying the points. Constrained optimization formalizes these situations.

The general problem

Formulation

We want to minimize an objective function f(x)f(x) subject to constraints:

min⁡xf(x)subject togi(x)=0,i=1,…,m\min_x f(x) \quad \text{subject to} \quad g_i(x) = 0, \quad i = 1, \ldots, m

For example: minimize a loss, subject to the constraint that the softmax weights sum to 1.

Without constraints, we use the condition ∇f(x)=0\nabla f(x) = 0. But with constraints, the minimum may be at a point where the gradient is not zero: the constraint "prevents" going lower.

Lagrange multipliers

The geometric intuition

Imagine you're walking on a mountainside (the function ff) but you're forced to stay on a trail (the constraint g(x)=0g(x) = 0). The lowest point you can reach is where the trail is tangent to a contour line of the mountain. At that point, the slope of the mountain is perpendicular to the trail.

Mathematically: at the optimal point, the gradient of ff is parallel to the gradient of gg:

∇f(x∗)=λ∇g(x∗)\nabla f(x^*) = \lambda \nabla g(x^*)

The scalar λ\lambda is the Lagrange multiplier.

An x y plane with two closed dashed blue level curves, labelled f of x y equals d1 for the large one and f of x y equals d2 for the small one, each carrying short perpendicular arrows. A solid red curve labelled g of x y equals c crosses the figure and touches the small level curve at one point. At that contact point, a blue arrow and an orange arrow leave in opposite directions along the same line, perpendicular to both curves.
This is the mountain path, drawn out. The blue dashed curves are the level sets of the objective, the red curve is the constraint you are forced to stay on. The point of interest is where the constraint grazes a level curve instead of crossing it: anywhere else you could still slide along the path towards a better level. At that tangency point, the blue arrow and the orange arrow lie on the same line, which is exactly the two gradients being equal up to a factor.Source : UKAEA, PROCESS documentation, VMCON Optimisation Solver page

The Lagrangian

We combine the objective and constraints into a single function:

L(x,λ)=f(x)+∑i=1mλigi(x)\mathcal{L}(x, \lambda) = f(x) + \sum_{i=1}^m \lambda_i g_i(x)

The optimality conditions are then:

∇xL=0and∇λL=0\nabla_x \mathcal{L} = 0 \quad \text{and} \quad \nabla_\lambda \mathcal{L} = 0

The first condition gives ∇f=−∑λi∇gi\nabla f = -\sum \lambda_i \nabla g_i (tangency). The second gives back the constraints gi(x)=0g_i(x) = 0.

Concrete example

Maximize entropy H=−∑ipilog⁡piH = -\sum_i p_i \log p_i subject to the constraint ∑ipi=1\sum_i p_i = 1:

L(p,λ)=−∑ipilog⁡pi+λ(∑ipi−1)\mathcal{L}(p, \lambda) = -\sum_i p_i \log p_i + \lambda\left(\sum_i p_i - 1\right)

Differentiating with respect to pip_i and solving, we get pi=1/Kp_i = 1/K for all classes: the uniform distribution maximizes entropy. This elegant result follows directly from the Lagrange method.

Interpretation of the multiplier

The multiplier λ\lambda has an economic interpretation: it's the sensitivity of the optimal objective to a relaxation of the constraint. If λ\lambda is large, the constraint is "costly": relaxing it a little would greatly improve the objective.

Inequality constraints and KKT conditions

The extended problem

In practice, constraints are often inequalities:

min⁡xf(x)subject togi(x)≤0,i=1,…,m\min_x f(x) \quad \text{subject to} \quad g_i(x) \leq 0, \quad i = 1, \ldots, m

For example: the margin of an SVM must be at least 1, i.e., gi(x)=1−yi(w⋅xi+b)≤0g_i(x) = 1 - y_i(\mathbf{w} \cdot \mathbf{x}_i + b) \leq 0.

The KKT conditions

The Karush-Kuhn-Tucker (KKT) conditions, formulated independently by Karush (1939) then Kuhn and Tucker (1951), generalize the Lagrange conditions to inequalities. At the optimal point (x∗,λ∗)(x^*, \lambda^*):

  1. Stationarity: ∇xL(x∗,λ∗)=0\nabla_x \mathcal{L}(x^*, \lambda^*) = 0
  2. Primal feasibility: gi(x∗)≤0g_i(x^*) \leq 0 for all ii
  3. Dual feasibility: λi≥0\lambda_i \geq 0 for all ii
  4. Complementary slackness: λigi(x∗)=0\lambda_i g_i(x^*) = 0 for all ii

The complementary slackness condition

This is the most subtle and most useful condition. It says: either the constraint is active (gi(x∗)=0g_i(x^*) = 0, we're on the boundary), or the multiplier is zero (λi=0\lambda_i = 0, the constraint has no effect). Both are never simultaneously nonzero.

In intuitive terms: a constraint that isn't "tight" costs nothing. Only active constraints influence the solution.

Major application: SVMs

The primal problem

A linear SVM seeks the hyperplane that separates classes with maximum margin:

min⁡w,b12∥w∥2subject toyi(w⋅xi+b)≥1,∀i\min_{\mathbf{w}, b} \frac{1}{2} \|\mathbf{w}\|^2 \quad \text{subject to} \quad y_i(\mathbf{w} \cdot \mathbf{x}_i + b) \geq 1, \quad \forall i

You minimize the norm of weights (maximum margin) subject to the constraint that each point is correctly classified with a margin of at least 1.

The dual

By forming the Lagrangian and applying KKT, you get the dual problem:

max⁡α∑iαi−12∑i,jαiαjyiyj(xi⋅xj)subject toαi≥0,∑iαiyi=0\max_\alpha \sum_i \alpha_i - \frac{1}{2} \sum_{i,j} \alpha_i \alpha_j y_i y_j (\mathbf{x}_i \cdot \mathbf{x}_j) \quad \text{subject to} \quad \alpha_i \geq 0, \quad \sum_i \alpha_i y_i = 0

Support vectors

By the complementary slackness condition, αi>0\alpha_i > 0 only if yi(w⋅xi+b)=1y_i(\mathbf{w} \cdot \mathbf{x}_i + b) = 1. These points, exactly on the margin, are the support vectors. They are the only ones that matter for defining the hyperplane. All other points (αi=0\alpha_i = 0) could be removed without changing the solution.

A two-class scatter plot, light blue at the bottom left and orange at the top right, in a Feature 1 versus Feature 2 plane. A solid line labelled Maximum Margin Hyperplane separates the two groups, flanked by two parallel dashed lines labelled Margin. Only three points are ringed in black and labelled Support Vectors; they sit exactly on the two dashed lines.
Complementary slackness is visible to the naked eye here. Only the three ringed points touch one of the dashed lines: these are the support vectors, the only ones whose constraint is tight and whose multiplier is non-zero. Every other point, further from the boundary, has a zero multiplier; you could erase them from the plot without moving the separating line by a millimetre.Source : Springer, chapter Machine Learning: Building Intelligence from Patterns

The kernel trick

The dual depends on the data only through dot products xi⋅xj\mathbf{x}_i \cdot \mathbf{x}_j. By replacing this dot product with a kernel K(xi,xj)K(\mathbf{x}_i, \mathbf{x}_j), you can separate data that isn't linearly separable without ever explicitly computing the transformation. That's the kernel trick, a direct consequence of the dual formulation.

Other applications in ML

Embedding normalization

Constraining embedding vectors to live on the unit sphere (∥e∥=1\|e\| = 1) is an equality constraint. In practice, you normalize directly rather than using Lagrange, but the underlying theory is the same.

Regularization as a constraint

Instead of minimizing L(θ)+λ∥θ∥2L(\theta) + \lambda \|\theta\|^2 (penalized formulation), you can write the equivalent problem:

min⁡θL(θ)subject to∥θ∥2≤C\min_\theta L(\theta) \quad \text{subject to} \quad \|\theta\|^2 \leq C

The two formulations are linked by Lagrangian duality. For each λ\lambda, there exists a corresponding CC, and vice versa. That's why regularization can be seen as a constraint on model complexity.

Fairness constraints

Increasingly, we want fair models. For example: the probability of a positive prediction must be the same for two demographic groups. This is expressed as an equality constraint on model outputs, and Lagrange multipliers allow integrating this constraint into training.

Strong and weak duality

In convex optimization, strong duality means the dual problem has the same optimal solution as the primal. This is a powerful result because the dual is sometimes easier to solve. Weak duality (dual≤primal\text{dual} \leq \text{primal}) is always true and provides a useful lower bound.

Key takeaways

  • Lagrange multipliers allow optimizing under equality constraints
  • The geometric intuition is tangency between the contour lines of the objective and the constraint
  • KKT conditions extend the method to inequality constraints
  • Complementary slackness says only active constraints matter
  • SVMs are the flagship application: the dual formulation leads to the kernel trick
  • Regularization can be seen as a constraint on parameters
  • These tools remain relevant for fairness and normalization constraints in modern ML

Where this is used. A penalty added to the loss is a constraint rewritten as a Lagrange multiplier: see regularization.