TL;DR
Bilevel optimization is the problem of choosing a value that minimises an upper-level objective, where the value of a second quantity is not free: must be the best response to under an inner, lower-level objective. The problem looks like two nested searches, and a naive solver runs the inner search again for every candidate . The practical method computes the derivative of the upper objective through the inner solution, which the implicit function theorem provides without ever solving for in closed form.
Builds on: Constrained Optimization, Gradient Descent, Implicit Function Theorem, Hessian Matrix · Leads to: Hyperparameter Optimization, Meta-Learning, and neural architecture search such as DARTS
Where it's used
Choosing training settings. A model is trained on a training set with settings such as the strength of a penalty, the learning rate or the size of a network. Those settings are chosen by how well the trained model does on a separate validation set. Training is the inner problem, because the weights are the best response to the settings, and validation error is the outer objective. Hyperparameter tuning is a bilevel problem whenever the gradient of validation error with respect to the settings is used.
Neural architecture search. The DARTS method (2018) relaxes the choice of operations in a network into continuous weights, then optimises those weights on validation data (the outer problem) after training the network weights on training data (the inner problem). The architecture is then read off the learned weights. The whole method depends on differentiating the outer loss through the inner training.
Meta-learning. In model-agnostic meta-learning, an initialisation is chosen so that a few gradient steps on a new task give a good model for that task. The inner problem adapts the model to one task, and the outer problem scores the adapted model across many tasks, so the initialisation is itself the result of a bilevel problem. The same nested structure also appears in economics, where a leader chooses a policy knowing how followers will respond.
How it works
Take the smallest instance we can. The inner problem chooses to make as small as possible, so the best response is . The outer problem chooses to make small, but is always the best response, so the outer problem sees .
- Solve the inner problem for a given . Input: a number . The inner objective is zero exactly when , so the best response is . Check: at the inner minimiser is , which makes the squared gap zero.
- Substitute the response into the outer objective. Replace by in . The outer objective becomes , which depends only on . Check: at we get .
- Evaluate a few candidates. , , and . The middle value is the smallest so far. Check: , which is larger than .
- Find the minimiser of the outer problem. Differentiate : . Setting this to zero gives , and then and . Check: the slope of is negative left of and positive right of it, so is the minimum.
- Read the slope without the closed form. The outer slope is . Here , so the slope is , the same as in step 4. Check: at the slope is . Moving from to changes from to , a slope of about , close to .
The plot shows the outer objective after the inner problem has been solved. It is the curve the outer minimiser actually sees.
- outer objective after the inner solve
- outer objective with y frozen at 0
The math
Definition
Let be the upper-level variable and the lower-level variable. A bilevel problem is
The function is the upper-level objective, is the lower-level objective, and is the lower-level best response. The arg min is the set of minimisers, and the problem is well defined when that set has one point for each .
In words: the outer search tries values of , the inner search finds the best for each one, and the outer objective is evaluated at that best . The lower problem is a constraint on that the upper problem cannot change.
The inner solve (walkthrough step 1)
Suppose is smooth and its minimiser is an interior point. Then the first-order condition holds for every . In the example, gives , which is zero at .
The slope through the response (walkthrough step 5)
Differentiate the condition in the inner solve with respect to , using the chain rule on :
This is the implicit function theorem applied to the equation . When the second derivative in is invertible, it gives
The outer derivative, called the hypergradient, is then
The tiny example in symbols (walkthrough steps 2 to 4)
For we have and . So , which matches . For we have and . The hypergradient is , as in the walkthrough.
A matrix version
When and are vectors, the same argument gives , where is the Hessian of in . Inverting this matrix is the expensive part of the method, so practical methods use conjugate-gradient solves or approximations instead of forming the inverse.
Worked examples
Lower problem: . Upper problem: . Find the bilevel minimiser.
- The inner best response is , since is zero there.
- Substitute into the outer objective: .
- Differentiate: .
- Set : , so .
- Compute the outer value: , so .
- Check with the hypergradient: at , and , and . The total is , so the slope is zero.
- Interpretation: the upper objective wants near and near . The lower problem forces , so the best compromise is halfway, at .
Lower problem: . Upper problem: .
- Inner condition: , so .
- Derivatives for Step 2: and . So , which agrees with .
- Substitute into the outer objective: .
- Differentiate: .
- Set : . Then .
- Check the hypergradient at : , and . Then , so the slope vanishes.
- Outer value: .
- Interpretation: the lower level pulls to half of , so the upper objective only has to balance against . Ignoring that link would give a different answer.
A model learns a weight by minimising a training loss plus a penalty: the inner problem is , where is the penalty strength. The outer problem chooses to minimise a validation loss , since the validation data prefer . Find the best .
- The inner best response: , so .
- Outer objective: .
- Derivatives for Step 2: , and . At these are and , with . So .
- Hypergradient at : , so .
- Check against the closed form: , which at is . The two agree.
- Since is decreasing at , a larger penalty helps. The outer objective reaches zero at , where exactly matches the validation target.
- Interpretation: this is hyperparameter tuning in miniature. The penalty is the setting, the training problem is the inner solve, and the validation loss is the outer objective.
Try it: using , find . Answer: , and squaring gives .
Common mistakes and tradeoffs
Mistake 1: "A bilevel problem is just two optimisations run one after the other." It is tempting to think you can solve the inner problem once and then optimise the outer one. In fact the inner solution changes with , so the outer objective must be evaluated at the best response for each candidate. Solving the inner problem once, at a single , gives a wrong answer for every other .
Mistake 2: "Hold the lower variable fixed while optimising the upper one." It is tempting to think can be treated as a constant. In fact the upper objective depends on through , so the total derivative must include . In Example 1, holding at gives the minimiser with value , but the true bilevel minimiser is with value .
Mistake 3: "The hypergradient always exists." It is tempting to think the implicit formula works for any problem. In fact it needs the inner minimiser to be unique and smooth, with invertible. If the inner problem has several minimisers, or is not smooth, the outer objective can jump and no single derivative describes it.
When to use it. Use bilevel methods when one set of decisions is chosen by optimising a quantity that depends on another optimisation: tuning training settings, meta-learning, or leader-follower design. When not to. If the inner problem has no unique minimiser, or if the inner solve is so cheap that the whole problem can be posed as one joint optimisation, a simpler formulation may be better. Alternatives include unrolling a fixed number of inner steps and differentiating through them, reformulating the inner problem with its optimality conditions as constraints, or using black-box search over when is low-dimensional.
History
The leader-follower structure dates to economics. In 1934 Heinrich von Stackelberg described a market in which one firm moves first and another responds, a structure now called a Stackelberg game, and the bilevel formulation is its mathematical version. Optimisation researchers later wrote such problems as mathematical programs with an optimisation problem inside the constraints. In machine learning, the idea of using the gradient of validation error with respect to training settings was studied around 2000, and it became common when differentiable networks made the inner solve practical to differentiate through. The DARTS paper in 2018 made the bilevel view central to architecture search, and it caught on because a single gradient method could then tune both weights and architecture.
Recap
- A bilevel problem minimises an outer objective , where is the best response to under an inner objective.
- The inner first-order condition, differentiated with respect to , gives by the implicit function theorem.
- The hypergradient lets ordinary gradient descent optimise the outer variable.
- The same structure appears in hyperparameter tuning, Meta-Learning and architecture search, and each is a special case of tuning one variable through another's best response.
What to read next: Hyperparameter Optimization, where the outer variable is a training setting, and Lagrange Multipliers, which handles the constraints that the inner problem sometimes imposes.
Check yourself
QuestionRecall: why does the outer objective depend on the inner solution, not just on ?Show answer
Because the outer objective is , and is the lower-level minimiser for that . The outer problem cannot choose directly, so its value depends on through the best response.
QuestionComputation: for lower objective and upper objective , find and .Show answer
The inner minimiser is , since the square is zero there. Then . Check: at , and , matching .
QuestionWhy: why is holding fixed at its current value while optimising a mistake?Show answer
Because the outer objective depends on through , which changes when changes. Holding fixed drops the term from the derivative, so the step taken can increase the outer objective. In the first worked example, this gives the wrong minimiser, instead of .
QuestionApplied: a team tunes a learning rate by minimising validation loss, and each candidate learning rate requires a full training run. Is this a bilevel problem, and what does the hypergradient require here?Show answer
Yes. Training is the inner problem, because the weights are the best response to the learning rate, and validation loss is the outer objective. A hypergradient requires the derivative of the trained weights with respect to the learning rate. That is expensive to compute exactly, since it needs the inner Hessian solve, so a team usually searches over candidates directly unless it can afford differentiating through training.