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 subject to constraints:
For example: minimize a loss, subject to the constraint that the softmax weights sum to 1.
Without constraints, we use the condition . 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 ) but you're forced to stay on a trail (the constraint ). 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 is parallel to the gradient of :
The scalar is the Lagrange multiplier.

The Lagrangian
We combine the objective and constraints into a single function:
The optimality conditions are then:
The first condition gives (tangency). The second gives back the constraints .
Concrete example
Maximize entropy subject to the constraint :
Differentiating with respect to and solving, we get for all classes: the uniform distribution maximizes entropy. This elegant result follows directly from the Lagrange method.
Interpretation of the multiplier
The multiplier has an economic interpretation: it's the sensitivity of the optimal objective to a relaxation of the constraint. If 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:
For example: the margin of an SVM must be at least 1, i.e., .
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 :
- Stationarity:
- Primal feasibility: for all
- Dual feasibility: for all
- Complementary slackness: for all
The complementary slackness condition
This is the most subtle and most useful condition. It says: either the constraint is active (, we're on the boundary), or the multiplier is zero (, 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:
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:
Support vectors
By the complementary slackness condition, only if . These points, exactly on the margin, are the support vectors. They are the only ones that matter for defining the hyperplane. All other points () could be removed without changing the solution.

The kernel trick
The dual depends on the data only through dot products . By replacing this dot product with a kernel , 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 () 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 (penalized formulation), you can write the equivalent problem:
The two formulations are linked by Lagrangian duality. For each , there exists a corresponding , 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 () 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.