Search

Search papers and lessons

Bilevel Optimization

Level 4 · 13 min read

Bilevel optimization is minimising one objective where the variables must be the best response to a second, inner optimization; the trick is to differentiate through the inner solution.

On this page

  • Colour = topic mix
  • Linear algebra
  • Calculus
  • Discrete math
  • Probability
  • Optimization
  • Losses & regularization
  • ReadReady to readNot yet

TL;DR

Bilevel optimization is the problem of choosing a value xx that minimises an upper-level objective, where the value of a second quantity yy is not free: yy must be the best response to xx 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 xx. The practical method computes the derivative of the upper objective through the inner solution, which the implicit function theorem provides without ever solving for yy 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 yy to make f(x,y)=(y−x)2f(x, y) = (y - x)^2 as small as possible, so the best response is y=xy = x. The outer problem chooses xx to make F(x,y)=(y−3)2+x2F(x, y) = (y - 3)^2 + x^2 small, but yy is always the best response, so the outer problem sees y=xy = x.

  1. Solve the inner problem for a given xx. Input: a number xx. The inner objective (y−x)2(y - x)^2 is zero exactly when y=xy = x, so the best response is y∗(x)=xy^\ast(x) = x. Check: at x=2x = 2 the inner minimiser is y=2y = 2, which makes the squared gap zero.
  2. Substitute the response into the outer objective. Replace yy by y∗(x)=xy^\ast(x) = x in FF. The outer objective becomes G(x)=(x−3)2+x2G(x) = (x - 3)^2 + x^2, which depends only on xx. Check: at x=0x = 0 we get G=9+0=9G = 9 + 0 = 9.
  3. Evaluate a few candidates. G(0)=9G(0) = 9, G(3)=9G(3) = 9, and G(1.5)=2.25+2.25=4.5G(1.5) = 2.25 + 2.25 = 4.5. The middle value is the smallest so far. Check: G(1)=4+1=5G(1) = 4 + 1 = 5, which is larger than 4.54.5.
  4. Find the minimiser of the outer problem. Differentiate GG: G′(x)=2(x−3)+2x=4x−6G'(x) = 2(x - 3) + 2x = 4x - 6. Setting this to zero gives x=1.5x = 1.5, and then y∗=1.5y^\ast = 1.5 and G=4.5G = 4.5. Check: the slope of GG is negative left of 1.51.5 and positive right of it, so 1.51.5 is the minimum.
  5. Read the slope without the closed form. The outer slope is G′(x)=∂F/∂x+∂F/∂y⋅dy∗/dxG'(x) = \partial F/\partial x + \partial F/\partial y \cdot dy^\ast/dx. Here dy∗/dx=1dy^\ast/dx = 1, so the slope is 2x+2(x−3)=4x−62x + 2(x - 3) = 4x - 6, the same as in step 4. Check: at x=0x = 0 the slope is −6-6. Moving from 00 to 0.10.1 changes GG from 99 to 8.428.42, a slope of about −5.8-5.8, close to −6-6.

The plot shows the outer objective after the inner problem has been solved. It is the curve the outer minimiser actually sees.

−101234051015xobjective
  • outer objective after the inner solve
  • outer objective with y frozen at 0
The solid curve is the true outer objective. Its minimum at x = 1.5 is not where the dashed curve (y held at 0) is minimised.

The math

Definition

Definition: Bilevel problem

Let xx be the upper-level variable and yy the lower-level variable. A bilevel problem is

min⁡x  F(x,y∗(x))wherey∗(x)∈arg⁡min⁡y  f(x,y).\min_x \; F\bigl(x, y^\ast(x)\bigr) \quad \text{where} \quad y^\ast(x) \in \arg\min_y \; f(x, y).

The function FF is the upper-level objective, ff is the lower-level objective, and y∗(x)y^\ast(x) 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 xx.

In words: the outer search tries values of xx, the inner search finds the best yy for each one, and the outer objective is evaluated at that best yy. The lower problem is a constraint on yy that the upper problem cannot change.

The inner solve (walkthrough step 1)

Suppose ff is smooth and its minimiser is an interior point. Then the first-order condition ∂f/∂y (x,y∗(x))=0\partial f/\partial y\,(x, y^\ast(x)) = 0 holds for every xx. In the example, f=(y−x)2f = (y - x)^2 gives ∂f/∂y=2(y−x)\partial f/\partial y = 2(y - x), which is zero at y∗=xy^\ast = x.

The slope through the response (walkthrough step 5)

Differentiate the condition in the inner solve with respect to xx, using the chain rule on y∗(x)y^\ast(x):

∂2f∂y2 dy∗dx+∂2f∂y ∂x=0.\frac{\partial^2 f}{\partial y^2}\, \frac{dy^\ast}{dx} + \frac{\partial^2 f}{\partial y\, \partial x} = 0.

This is the implicit function theorem applied to the equation ∂f/∂y=0\partial f/\partial y = 0. When the second derivative in yy is invertible, it gives

dy∗dx=−(∂2f∂y2)−1∂2f∂y ∂x.\frac{dy^\ast}{dx} = -\left(\frac{\partial^2 f}{\partial y^2}\right)^{-1} \frac{\partial^2 f}{\partial y\, \partial x}.

The outer derivative, called the hypergradient, is then

dFdx=∂F∂x+∂F∂y dy∗dx.\frac{dF}{dx} = \frac{\partial F}{\partial x} + \frac{\partial F}{\partial y}\,\frac{dy^\ast}{dx}.

The tiny example in symbols (walkthrough steps 2 to 4)

For f=(y−x)2f = (y - x)^2 we have ∂2f/∂y2=2\partial^2 f/\partial y^2 = 2 and ∂2f/∂y ∂x=−2\partial^2 f/\partial y\,\partial x = -2. So dy∗/dx=−(2)−1(−2)=1dy^\ast/dx = -(2)^{-1}(-2) = 1, which matches y∗=xy^\ast = x. For F=(y−3)2+x2F = (y - 3)^2 + x^2 we have ∂F/∂x=2x\partial F/\partial x = 2x and ∂F/∂y=2(y−3)\partial F/\partial y = 2(y - 3). The hypergradient is 2x+2(x−3)=4x−62x + 2(x - 3) = 4x - 6, as in the walkthrough.

A matrix version

When xx and yy are vectors, the same argument gives dy∗/dx=−(∇yy2f)−1∇yx2fdy^\ast/dx = -(\nabla^2_{yy} f)^{-1} \nabla^2_{yx} f, where ∇yy2f\nabla^2_{yy} f is the Hessian of ff in yy. 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

Example
1. The tiny instance from the walkthrough

Lower problem: f(x,y)=(y−x)2f(x, y) = (y - x)^2. Upper problem: F(x,y)=(y−3)2+x2F(x, y) = (y - 3)^2 + x^2. Find the bilevel minimiser.

  1. The inner best response is y∗(x)=xy^\ast(x) = x, since (y−x)2(y - x)^2 is zero there.
  2. Substitute into the outer objective: G(x)=(x−3)2+x2=2x2−6x+9G(x) = (x - 3)^2 + x^2 = 2x^2 - 6x + 9.
  3. Differentiate: G′(x)=4x−6G'(x) = 4x - 6.
  4. Set G′(x)=0G'(x) = 0: 4x=64x = 6, so x=1.5x = 1.5.
  5. Compute the outer value: y∗=1.5y^\ast = 1.5, so F(1.5,1.5)=(1.5−3)2+1.52=2.25+2.25=4.5F(1.5, 1.5) = (1.5 - 3)^2 + 1.5^2 = 2.25 + 2.25 = 4.5.
  6. Check with the hypergradient: at x=1.5x = 1.5, ∂F/∂x=3\partial F/\partial x = 3 and ∂F/∂y=2(1.5−3)=−3\partial F/\partial y = 2(1.5 - 3) = -3, and dy∗/dx=1dy^\ast/dx = 1. The total is 3+(−3)(1)=03 + (-3)(1) = 0, so the slope is zero.
  7. Interpretation: the upper objective wants yy near 33 and xx near 00. The lower problem forces y=xy = x, so the best compromise is halfway, at 1.51.5.
Example
2. A two-variable instance with a slope

Lower problem: f(x,y)=y2−xyf(x, y) = y^2 - xy. Upper problem: F(x,y)=(y−1)2+x2F(x, y) = (y - 1)^2 + x^2.

  1. Inner condition: ∂f/∂y=2y−x=0\partial f/\partial y = 2y - x = 0, so y∗(x)=x/2y^\ast(x) = x/2.
  2. Derivatives for Step 2: ∂2f/∂y2=2\partial^2 f/\partial y^2 = 2 and ∂2f/∂y ∂x=−1\partial^2 f/\partial y\,\partial x = -1. So dy∗/dx=−(2)−1(−1)=1/2dy^\ast/dx = -(2)^{-1}(-1) = 1/2, which agrees with y∗=x/2y^\ast = x/2.
  3. Substitute into the outer objective: G(x)=(x/2−1)2+x2G(x) = (x/2 - 1)^2 + x^2.
  4. Differentiate: G′(x)=(x/2−1)+2x=2.5x−1G'(x) = (x/2 - 1) + 2x = 2.5x - 1.
  5. Set G′(x)=0G'(x) = 0: x=0.4x = 0.4. Then y∗=0.2y^\ast = 0.2.
  6. Check the hypergradient at x=0.4x = 0.4: ∂F/∂x=2(0.4)=0.8\partial F/\partial x = 2(0.4) = 0.8, and ∂F/∂y=2(0.2−1)=−1.6\partial F/\partial y = 2(0.2 - 1) = -1.6. Then 0.8+(−1.6)(0.5)=00.8 + (-1.6)(0.5) = 0, so the slope vanishes.
  7. Outer value: F(0.4,0.2)=(0.2−1)2+0.42=0.64+0.16=0.8F(0.4, 0.2) = (0.2 - 1)^2 + 0.4^2 = 0.64 + 0.16 = 0.8.
  8. Interpretation: the lower level pulls yy to half of xx, so the upper objective only has to balance xx against x/2x/2. Ignoring that link would give a different answer.
Example
3. Tuning a penalty strength

A model learns a weight ww by minimising a training loss plus a penalty: the inner problem is min⁡w(w−2)2+λw2\min_w (w - 2)^2 + \lambda w^2, where λ\lambda is the penalty strength. The outer problem chooses λ\lambda to minimise a validation loss F(λ,w)=(w−1)2F(\lambda, w) = (w - 1)^2, since the validation data prefer w=1w = 1. Find the best λ\lambda.

  1. The inner best response: ∂/∂w [(w−2)2+λw2]=2(w−2)+2λw=0\partial/\partial w\,[(w - 2)^2 + \lambda w^2] = 2(w - 2) + 2\lambda w = 0, so w∗(λ)=2/(1+λ)w^\ast(\lambda) = 2/(1 + \lambda).
  2. Outer objective: G(λ)=(w∗−1)2=(21+λ−1)2=(1−λ1+λ)2G(\lambda) = (w^\ast - 1)^2 = \left(\dfrac{2}{1 + \lambda} - 1\right)^2 = \left(\dfrac{1 - \lambda}{1 + \lambda}\right)^2.
  3. Derivatives for Step 2: ∂2f/∂w2=2+2λ\partial^2 f/\partial w^2 = 2 + 2\lambda, and ∂2f/∂w ∂λ=2w\partial^2 f/\partial w\,\partial\lambda = 2w. At λ=0\lambda = 0 these are 22 and 44, with w∗=2w^\ast = 2. So dw∗/dλ=−4/2=−2dw^\ast/d\lambda = -4/2 = -2.
  4. Hypergradient at λ=0\lambda = 0: ∂F/∂w=2(2−1)=2\partial F/\partial w = 2(2 - 1) = 2, so dG/dλ=2⋅(−2)=−4dG/d\lambda = 2 \cdot (-2) = -4.
  5. Check against the closed form: G′(λ)=21−λ1+λ⋅−2(1+λ)2G'(\lambda) = 2\dfrac{1 - \lambda}{1 + \lambda} \cdot \dfrac{-2}{(1 + \lambda)^2}, which at λ=0\lambda = 0 is −4-4. The two agree.
  6. Since GG is decreasing at 00, a larger penalty helps. The outer objective reaches zero at λ=1\lambda = 1, where w∗=1w^\ast = 1 exactly matches the validation target.
  7. 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 G(λ)=(1−λ1+λ)2G(\lambda) = \left(\dfrac{1 - \lambda}{1 + \lambda}\right)^2, find G(3)G(3). Answer: (1−3)/(1+3)=−2/4=−1/2(1 - 3)/(1 + 3) = -2/4 = -1/2, and squaring gives G(3)=0.25G(3) = 0.25.

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 xx, so the outer objective must be evaluated at the best response for each candidate. Solving the inner problem once, at a single xx, gives a wrong answer for every other xx.

Mistake 2: "Hold the lower variable fixed while optimising the upper one." It is tempting to think yy can be treated as a constant. In fact the upper objective depends on yy through y∗(x)y^\ast(x), so the total derivative must include ∂F/∂y⋅dy∗/dx\partial F/\partial y \cdot dy^\ast/dx. In Example 1, holding yy at 00 gives the minimiser x=0x = 0 with value 99, but the true bilevel minimiser is x=1.5x = 1.5 with value 4.54.5.

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 ∂2f/∂y2\partial^2 f/\partial y^2 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 xx when xx 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 F(x,y∗(x))F(x, y^\ast(x)), where y∗(x)y^\ast(x) is the best response to xx under an inner objective.
  • The inner first-order condition, differentiated with respect to xx, gives dy∗/dxdy^\ast/dx by the implicit function theorem.
  • The hypergradient dF/dx=∂F/∂x+∂F/∂y⋅dy∗/dxdF/dx = \partial F/\partial x + \partial F/\partial y \cdot dy^\ast/dx 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 xx?Show answer

Because the outer objective is F(x,y∗(x))F(x, y^\ast(x)), and y∗(x)y^\ast(x) is the lower-level minimiser for that xx. The outer problem cannot choose yy directly, so its value depends on xx through the best response.

QuestionComputation: for lower objective f(x,y)=(y−2x)2f(x, y) = (y - 2x)^2 and upper objective F(x,y)=y2+x2F(x, y) = y^2 + x^2, find y∗(x)y^\ast(x) and G(x)=F(x,y∗(x))G(x) = F(x, y^\ast(x)).Show answer

The inner minimiser is y∗=2xy^\ast = 2x, since the square is zero there. Then G(x)=(2x)2+x2=5x2G(x) = (2x)^2 + x^2 = 5x^2. Check: at x=1x = 1, y∗=2y^\ast = 2 and G=4+1=5G = 4 + 1 = 5, matching 5x25x^2.

QuestionWhy: why is holding yy fixed at its current value while optimising xx a mistake?Show answer

Because the outer objective depends on yy through y∗(x)y^\ast(x), which changes when xx changes. Holding yy fixed drops the term ∂F/∂y⋅dy∗/dx\partial F/\partial y \cdot dy^\ast/dx from the derivative, so the step taken can increase the outer objective. In the first worked example, this gives the wrong minimiser, x=0x = 0 instead of x=1.5x = 1.5.

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.

Sources