Skip to content
BIASED

Lagrange multipliers and KKT conditions

Optimization under constraints: parallel gradients and the Lagrangian, inequalities and the sign of their multipliers, complementary slackness and the KKT conditions. Maximum entropy gives softmax, and projection onto a ball gives gradient clipping.

When the gradient need not vanish

The topic Unconstrained optimization gave us a test for a minimum, ∇f(x∗)=0\nabla f(x^*) = 0, and gradient descent to find one. Many ML problems, however, restrict where xx may go. At a restricted minimum the gradient is usually not zero, so we need a new test.

Constraints are everywhere in ML:

  • class probabilities lie on the simplex: pi≥0p_i \ge 0 and ∑ipi=1\sum_i p_i = 1;
  • the first principal component is a unit vector: u⊤u=1u^\top u = 1;
  • a max-norm weight constraint or gradient clipping keeps a vector in a ball: ∥w∥≤r\lVert w \rVert \le r.

The general problem has inequality constraints gig_i and equality constraints hjh_j:

min⁡x f(x)s.t.gi(x)≤0,  i=1,…,m,hj(x)=0,  j=1,…,p.\min_x\ f(x) \quad \text{s.t.} \quad g_i(x) \le 0,\ \ i = 1, \dots, m, \qquad h_j(x) = 0,\ \ j = 1, \dots, p.

A point that satisfies all constraints is called feasible.

The running example of this topic is

min⁡x x12+2x22s.t.x1+x2=1.\min_x\ x_1^2 + 2x_2^2 \quad \text{s.t.} \quad x_1 + x_2 = 1.

Here the constraint can simply be eliminated. Substitute x1=1−x2x_1 = 1 - x_2: the function (1−x2)2+2x22(1 - x_2)^2 + 2x_2^2 is minimal at x2=13x_2 = \tfrac13. So x∗=(23,13)x^* = (\tfrac23, \tfrac13) and f∗=23f^* = \tfrac23. Note that ∇f(x∗)=(43,43)≠0\nabla f(x^*) = (\tfrac43, \tfrac43) \ne 0: the unconstrained test fails at a perfectly good minimum.

Elimination does not scale. A circle x12+x22=1x_1^2 + x_2^2 = 1 gives x2=±1−x12x_2 = \pm\sqrt{1 - x_1^2}: two branches and a square root. With nn constraints pi≥0p_i \ge 0, each one may hold with equality or not, which makes 2n2^n cases. We need a rule that works with the constraints as given.

Equalities: the gradient is parallel to the normal

Start with a single equality constraint h(x)=0h(x) = 0: a curve in the plane, a surface in Rn\mathbb R^n. Walk along it and watch the level sets of ff (see the topic Gradients, Jacobians and Hessians). While ff decreases, we cross level sets one after another. At the constrained minimum we cannot go lower: the level set of ff just touches the constraint.

Now the same in gradients. Let dd be a direction tangent to the constraint at x∗x^*. To first order, ff changes along dd at the rate ∇f(x∗)⊤d\nabla f(x^*)^\top d. If this rate were nonzero, moving along the constraint in the direction dd or −d-d would decrease ff. So ∇f(x∗)\nabla f(x^*) is orthogonal to every tangent direction. When ∇h(x∗)≠0\nabla h(x^*) \ne 0, the tangent directions are exactly the dd with ∇h(x∗)⊤d=0\nabla h(x^*)^\top d = 0. Split ∇f(x∗)=α ∇h(x∗)+w\nabla f(x^*) = \alpha\, \nabla h(x^*) + w with ww orthogonal to ∇h(x∗)\nabla h(x^*). Then ww is itself a tangent direction, so 0=∇f(x∗)⊤w=∥w∥20 = \nabla f(x^*)^\top w = \lVert w \rVert^2. Hence w=0w = 0, and ∇f(x∗)\nabla f(x^*) is a multiple of the normal ∇h(x∗)\nabla h(x^*):

∇f(x∗)+ν ∇h(x∗)=0,h(x∗)=0.\nabla f(x^*) + \nu\, \nabla h(x^*) = 0, \qquad h(x^*) = 0.

The number ν\nu is the Lagrange multiplier. Its sign carries no meaning: h=0h = 0 and −h=0-h = 0 are the same constraint.

Check the running example. At x∗=(23,13)x^* = (\tfrac23, \tfrac13) we have ∇f=(43,43)\nabla f = (\tfrac43, \tfrac43) and ∇h=(1,1)\nabla h = (1, 1). They are parallel, with ν=−43\nu = -\tfrac43. Compare another feasible point, (0,1)(0, 1), where ∇f=(0,4)\nabla f = (0, 4). Its component along the line, in the direction (1,−1)/2(1, -1)/\sqrt2, is −4/2≈−2.83-4/\sqrt2 \approx -2.83. A step in that direction lowers ff, so (0,1)(0, 1) is not a minimum.

With several constraints h1,…,hph_1, \dots, h_p the condition becomes ∇f+∑jνj∇hj=0\nabla f + \sum_j \nu_j \nabla h_j = 0. It needs the gradients ∇hj(x∗)\nabla h_j(x^*) to be linearly independent; for one constraint, ∇h(x∗)≠0\nabla h(x^*) \ne 0. An assumption of this kind is called a constraint qualification. Without it the normal directions are not well defined, and the rule can fail.

The Lagrangian

All these conditions come from one function, the Lagrangian:

L(x,ν)=f(x)+∑j=1pνjhj(x).\mathcal L(x, \nu) = f(x) + \sum_{j=1}^p \nu_j h_j(x).

Setting ∇xL=0\nabla_x \mathcal L = 0 gives the parallel-gradient condition. Setting ∂L/∂νj=0\partial \mathcal L / \partial \nu_j = 0 gives back the constraint hj(x)=0h_j(x) = 0. A problem with nn variables and pp constraints becomes n+pn + p equations in n+pn + p unknowns.

For the running example, L=x12+2x22+ν(x1+x2−1)\mathcal L = x_1^2 + 2x_2^2 + \nu(x_1 + x_2 - 1), and the system is

2x1+ν=0,4x2+ν=0,x1+x2=1.2x_1 + \nu = 0, \qquad 4x_2 + \nu = 0, \qquad x_1 + x_2 = 1.

Its solution is x1=23x_1 = \tfrac23, x2=13x_2 = \tfrac13, ν=−43\nu = -\tfrac43, as before, and no substitution was needed.

A more useful example: the point of a plane a⊤x=ba^\top x = b closest to a point pp. We minimize 12∥x−p∥2\tfrac12\lVert x - p \rVert^2; the 12\tfrac12 only tidies the gradient. Stationarity gives x−p+νa=0x - p + \nu a = 0, so x=p−νax = p - \nu a. The constraint then reads a⊤p−ν∥a∥2=ba^\top p - \nu \lVert a \rVert^2 = b, which fixes ν\nu:

x∗=p−a⊤p−b∥a∥2 a.x^* = p - \frac{a^\top p - b}{\lVert a \rVert^2}\, a.

This is the orthogonal projection from the topic Inner products, norms and orthogonality, derived without a picture. With a=(1,2,2)a = (1, 2, 2), b=3b = 3 and p=(1,1,1)p = (1, 1, 1) we get ν=29\nu = \tfrac29, x∗=(79,59,59)x^* = (\tfrac79, \tfrac59, \tfrac59) and the distance 23\tfrac23.

A stationary point of L\mathcal L is only a candidate. On a closed curve such as a circle, ff has a minimum and a maximum, and both satisfy the condition. Compare the values of ff at the candidates, or use convexity (later in this topic).

Maximum entropy gives softmax

Now an ML problem where elimination would be painful. Outcomes 1,…,n1, \dots, n have values x1,…,xnx_1, \dots, x_n. Among all distributions pp with a given mean ∑ipixi=m\sum_i p_i x_i = m, which one has the largest entropy H(p)=−∑ipilog⁡piH(p) = -\sum_i p_i \log p_i? Maximizing HH means minimizing ∑ipilog⁡pi\sum_i p_i \log p_i. There are two equality constraints, so there are two multipliers, ν0\nu_0 and β\beta:

L=∑ipilog⁡pi+ν0(∑ipi−1)+β(∑ipixi−m).\mathcal L = \sum_i p_i \log p_i + \nu_0 \Big(\sum_i p_i - 1\Big) + \beta \Big(\sum_i p_i x_i - m\Big).

Set the derivative in pip_i to zero:

log⁡pi+1+ν0+βxi=0⟹pi=e−1−ν0 e−βxi.\log p_i + 1 + \nu_0 + \beta x_i = 0 \quad\Longrightarrow\quad p_i = e^{-1-\nu_0}\, e^{-\beta x_i}.

The factor e−1−ν0e^{-1-\nu_0} is the same for every ii, and normalization fixes it:

pi=e−βxi∑je−βxj.p_i = \frac{e^{-\beta x_i}}{\sum_j e^{-\beta x_j}}.

This is a softmax with logits −βxi-\beta x_i. The multiplier ν0\nu_0 only sets the normalizer. The multiplier β\beta sets the shape, and its value is whatever makes the mean equal to mm. We never imposed pi≥0p_i \ge 0, yet it holds: an exponential is positive.

Two facts make this the answer and not just a candidate. A suitable β\beta exists whenever mm lies strictly between the smallest and the largest xix_i: the mean of pp is continuous in β\beta and runs from the largest xix_i as β→−∞\beta \to -\infty to the smallest as β→+∞\beta \to +\infty. And ∑ipilog⁡pi\sum_i p_i \log p_i is convex, since its Hessian is diagonal with entries 1/pi>01/p_i > 0, while both constraints are affine. The sufficiency result later in this topic then makes the stationary point the global maximum of the entropy.

A die shows this in numbers. Ask for faces 1,…,61, \dots, 6 with mean m=4.5m = 4.5 instead of the fair 3.53.5. Solving for the mean gives β≈−0.371\beta \approx -0.371, and the probabilities grow from 0.0540.054 for face 11 to 0.3470.347 for face 66. The entropy is 1.6141.614 nats, below log⁡6≈1.792\log 6 \approx 1.792 for the fair die: the constraint on the mean costs entropy.

So softmax is not an arbitrary choice. It is the least committal distribution with a prescribed expected value. The same calculation gives the Boltzmann distribution in physics and entropy-regularized policies in reinforcement learning.

Inequalities: active or inactive

An inequality constraint g(x)≤0g(x) \le 0 can be in one of two states at the solution. It is inactive if g(x∗)<0g(x^*) < 0. Then it does not press: a small step in any direction stays feasible, and ∇f(x∗)=0\nabla f(x^*) = 0 as in the unconstrained problem. It is active if g(x∗)=0g(x^*) = 0. Then it acts like an equality:

∇f(x∗)+λ ∇g(x∗)=0.\nabla f(x^*) + \lambda\, \nabla g(x^*) = 0.

Unlike ν\nu, this multiplier has a sign: λ≥0\lambda \ge 0. The gradient ∇g\nabla g points out of the feasible set, where gg grows. At a minimum, the descent direction −∇f=λ∇g-\nabla f = \lambda \nabla g must point outward too. Otherwise a small step inside would decrease ff. Both states fit one formula if we set λ=0\lambda = 0 for an inactive constraint.

One dimension: minimize (x−2)2(x - 2)^2 subject to x≤1x \le 1, so g(x)=x−1g(x) = x - 1. The unconstrained minimum x=2x = 2 is infeasible, so the constraint is active and x∗=1x^* = 1. Stationarity gives 2(1−2)+λ=02(1 - 2) + \lambda = 0, so λ=2≥0\lambda = 2 \ge 0. With x≤3x \le 3 instead, the point x=2x = 2 is feasible, the constraint is inactive and λ=0\lambda = 0.

A multiplier is also a price. Relax the bound to x≤1+ux \le 1 + u. The optimum becomes f∗(u)=(1−u)2f^*(u) = (1 - u)^2, and at u=0u = 0 its derivative is −2=−λ-2 = -\lambda. For an equality h(x)=uh(x) = u the chain rule shows why. If the solution x∗(u)x^*(u) moves smoothly with uu, stationarity ∇f=−ν∇h\nabla f = -\nu \nabla h gives

df∗du=∇f(x∗)⊤dx∗du=−ν ∇h(x∗)⊤dx∗du=−ν,\frac{d f^*}{du} = \nabla f(x^*)^\top \frac{d x^*}{du} = -\nu\, \nabla h(x^*)^\top \frac{d x^*}{du} = -\nu,

because h(x∗(u))=uh(x^*(u)) = u has derivative 11. An active inequality acts as an equality, so relaxing gi≤0g_i \le 0 to gi≤ug_i \le u changes the optimal value at the rate −λi-\lambda_i. A looser constraint can only help, so the rate is ≤0\le 0. This is a second reason why λ≥0\lambda \ge 0.

The KKT conditions

Now put everything together. Take the full Lagrangian

L(x,λ,ν)=f(x)+∑iλigi(x)+∑jνjhj(x).\mathcal L(x, \lambda, \nu) = f(x) + \sum_i \lambda_i g_i(x) + \sum_j \nu_j h_j(x).

The Karush–Kuhn–Tucker (KKT) conditions at a point x∗x^* are:

∇f(x∗)+∑iλi∇gi(x∗)+∑jνj∇hj(x∗)=0(stationarity)gi(x∗)≤0,hj(x∗)=0(primal feasibility)λi≥0(dual feasibility)λi gi(x∗)=0(complementary slackness)\begin{aligned} &\nabla f(x^*) + \textstyle\sum_i \lambda_i \nabla g_i(x^*) + \sum_j \nu_j \nabla h_j(x^*) = 0 && \text{(stationarity)}\\ &g_i(x^*) \le 0, \quad h_j(x^*) = 0 && \text{(primal feasibility)}\\ &\lambda_i \ge 0 && \text{(dual feasibility)}\\ &\lambda_i\, g_i(x^*) = 0 && \text{(complementary slackness)} \end{aligned}

Complementary slackness repeats the previous block for every constraint. Either the constraint is tight, gi=0g_i = 0, and its multiplier may be positive. Or it is slack, gi<0g_i < 0, and its multiplier is zero. A support vector machine has one constraint per training point. The points with λi>0\lambda_i > 0 are exactly the support vectors: only they shape the classifier.

When are the KKT conditions necessary? At a local minimum, under a constraint qualification: for example, the gradients of the active constraints are linearly independent, or, for a convex problem, Slater's condition holds (some feasible point satisfies every gi<0g_i < 0). Without one, KKT can fail. Minimize xx subject to x2≤0x^2 \le 0: the only feasible point is x∗=0x^* = 0, yet 1+λ⋅0=01 + \lambda \cdot 0 = 0 has no solution.

When are they sufficient? If ff and all gig_i are convex and all hjh_j are affine (see the topic Convex sets and functions), every KKT point is a global minimum. The argument is short. With λ≥0\lambda \ge 0 fixed, L(x,λ,ν)\mathcal L(x, \lambda, \nu) is convex in xx, and stationarity makes x∗x^* its minimizer. For any feasible xx we have λigi(x)≤0\lambda_i g_i(x) \le 0 and hj(x)=0h_j(x) = 0, so

f(x) ≥ L(x,λ,ν) ≥ L(x∗,λ,ν)=f(x∗).f(x) \ \ge\ \mathcal L(x, \lambda, \nu) \ \ge\ \mathcal L(x^*, \lambda, \nu) = f(x^*).

The last equality uses complementary slackness and hj(x∗)=0h_j(x^*) = 0.

Projection onto a ball and gradient clipping

The second ML example is projection. Projected gradient descent handles a constraint θ∈C\theta \in C simply: take an ordinary gradient step, then return to CC by the nearest-point map ΠC\Pi_C:

θ←ΠC(θ−η∇L(θ)),ΠC(v)=arg⁡min⁡x∈C 12∥x−v∥2.\theta \leftarrow \Pi_C\big(\theta - \eta \nabla L(\theta)\big), \qquad \Pi_C(v) = \arg\min_{x \in C}\ \tfrac12 \lVert x - v \rVert^2.

Take the ball C={x:∥x∥≤r}C = \{x : \lVert x \rVert \le r\}. Write the constraint as g(x)=12(∥x∥2−r2)≤0g(x) = \tfrac12 \big(\lVert x \rVert^2 - r^2\big) \le 0, so that ∇g=x\nabla g = x. The problem is convex, so the KKT conditions are enough. Stationarity gives

x−v+λx=0⟹x=v1+λ.x - v + \lambda x = 0 \quad\Longrightarrow\quad x = \frac{v}{1 + \lambda}.

Complementary slackness leaves two cases. If ∥v∥≤r\lVert v \rVert \le r, then λ=0\lambda = 0 and x=vx = v: a point inside the ball stays put. If ∥v∥>r\lVert v \rVert > r, then λ=0\lambda = 0 would give an infeasible xx. So the constraint is active, ∥x∥=r\lVert x \rVert = r, and this equation fixes λ>0\lambda > 0. Both cases fit one formula:

ΠC(v)=v⋅min⁡(1,r∥v∥).\Pi_C(v) = v \cdot \min\Big(1, \frac{r}{\lVert v \rVert}\Big).

This is gradient clipping by norm: a gradient longer than rr is shrunk to length rr and keeps its direction. Max-norm weight constraints do the same to weight vectors. For v=(3,4)v = (3, 4) and r=1r = 1 the projection is (0.6,0.8)(0.6, 0.8). For a box li≤xi≤uil_i \le x_i \le u_i the same reasoning gives coordinate-wise clipping min⁡(max⁡(vi,li),ui)\min(\max(v_i, l_i), u_i), and each multiplier equals how far its coordinate was clipped.

In every example the multipliers meant something: the price of a constraint, the inverse temperature of a softmax, the amount of clipping. The topic Duality makes them variables of a problem of their own. Minimizing the Lagrangian over xx for fixed multipliers gives a lower bound on f∗f^*, and maximizing that bound over the multipliers often gives f∗f^* exactly.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.