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, , and gradient descent to find one. Many ML problems, however, restrict where 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: and ;
- the first principal component is a unit vector: ;
- a max-norm weight constraint or gradient clipping keeps a vector in a ball: .
The general problem has inequality constraints and equality constraints :
A point that satisfies all constraints is called feasible.
The running example of this topic is
Here the constraint can simply be eliminated. Substitute : the function is minimal at . So and . Note that : the unconstrained test fails at a perfectly good minimum.
Elimination does not scale. A circle gives : two branches and a square root. With constraints , each one may hold with equality or not, which makes 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 : a curve in the plane, a surface in . Walk along it and watch the level sets of (see the topic Gradients, Jacobians and Hessians). While decreases, we cross level sets one after another. At the constrained minimum we cannot go lower: the level set of just touches the constraint.
Now the same in gradients. Let be a direction tangent to the constraint at . To first order, changes along at the rate . If this rate were nonzero, moving along the constraint in the direction or would decrease . So is orthogonal to every tangent direction. When , the tangent directions are exactly the with . Split with orthogonal to . Then is itself a tangent direction, so . Hence , and is a multiple of the normal :
The number is the Lagrange multiplier. Its sign carries no meaning: and are the same constraint.
Check the running example. At we have and . They are parallel, with . Compare another feasible point, , where . Its component along the line, in the direction , is . A step in that direction lowers , so is not a minimum.
With several constraints the condition becomes . It needs the gradients to be linearly independent; for one constraint, . 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:
Setting gives the parallel-gradient condition. Setting gives back the constraint . A problem with variables and constraints becomes equations in unknowns.
For the running example, , and the system is
Its solution is , , , as before, and no substitution was needed.
A more useful example: the point of a plane closest to a point . We minimize ; the only tidies the gradient. Stationarity gives , so . The constraint then reads , which fixes :
This is the orthogonal projection from the topic Inner products, norms and orthogonality, derived without a picture. With , and we get , and the distance .
A stationary point of is only a candidate. On a closed curve such as a circle, has a minimum and a maximum, and both satisfy the condition. Compare the values of at the candidates, or use convexity (later in this topic).
Maximum entropy gives softmax
Now an ML problem where elimination would be painful. Outcomes have values . Among all distributions with a given mean , which one has the largest entropy ? Maximizing means minimizing . There are two equality constraints, so there are two multipliers, and :
Set the derivative in to zero:
The factor is the same for every , and normalization fixes it:
This is a softmax with logits . The multiplier only sets the normalizer. The multiplier sets the shape, and its value is whatever makes the mean equal to . We never imposed , yet it holds: an exponential is positive.
Two facts make this the answer and not just a candidate. A suitable exists whenever lies strictly between the smallest and the largest : the mean of is continuous in and runs from the largest as to the smallest as . And is convex, since its Hessian is diagonal with entries , 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 with mean instead of the fair . Solving for the mean gives , and the probabilities grow from for face to for face . The entropy is nats, below 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 can be in one of two states at the solution. It is inactive if . Then it does not press: a small step in any direction stays feasible, and as in the unconstrained problem. It is active if . Then it acts like an equality:
Unlike , this multiplier has a sign: . The gradient points out of the feasible set, where grows. At a minimum, the descent direction must point outward too. Otherwise a small step inside would decrease . Both states fit one formula if we set for an inactive constraint.
One dimension: minimize subject to , so . The unconstrained minimum is infeasible, so the constraint is active and . Stationarity gives , so . With instead, the point is feasible, the constraint is inactive and .
A multiplier is also a price. Relax the bound to . The optimum becomes , and at its derivative is . For an equality the chain rule shows why. If the solution moves smoothly with , stationarity gives
because has derivative . An active inequality acts as an equality, so relaxing to changes the optimal value at the rate . A looser constraint can only help, so the rate is . This is a second reason why .
The KKT conditions
Now put everything together. Take the full Lagrangian
The Karush–Kuhn–Tucker (KKT) conditions at a point are:
Complementary slackness repeats the previous block for every constraint. Either the constraint is tight, , and its multiplier may be positive. Or it is slack, , and its multiplier is zero. A support vector machine has one constraint per training point. The points with 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 ). Without one, KKT can fail. Minimize subject to : the only feasible point is , yet has no solution.
When are they sufficient? If and all are convex and all are affine (see the topic Convex sets and functions), every KKT point is a global minimum. The argument is short. With fixed, is convex in , and stationarity makes its minimizer. For any feasible we have and , so
The last equality uses complementary slackness and .
Projection onto a ball and gradient clipping
The second ML example is projection. Projected gradient descent handles a constraint simply: take an ordinary gradient step, then return to by the nearest-point map :
Take the ball . Write the constraint as , so that . The problem is convex, so the KKT conditions are enough. Stationarity gives
Complementary slackness leaves two cases. If , then and : a point inside the ball stays put. If , then would give an infeasible . So the constraint is active, , and this equation fixes . Both cases fit one formula:
This is gradient clipping by norm: a gradient longer than is shrunk to length and keeps its direction. Max-norm weight constraints do the same to weight vectors. For and the projection is . For a box the same reasoning gives coordinate-wise clipping , 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 for fixed multipliers gives a lower bound on , and maximizing that bound over the multipliers often gives exactly.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.