Foundations to Frontier
Tests
A. Mathematical Foundations › A6 Optimization › Advanced

Bilevel Optimization and Implicit Differentiation

Many engineering problems nest one optimization inside another: choose a hyperparameter so that the model trained with it validates well; choose cost weights so that a planner reproduces an expert; design a system knowing an adversary or controller will respond optimally. You will learn the bilevel formulation, compute hypergradients by the implicit function theorem (one linear solve, no unrolling) or by differentiating through unrolled iterations, approximate inverse-Hessian products with Neumann series, tune a ridge penalty by hypergradient descent, and recover an expert's cost weight from demonstrations.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

An optimization inside an optimization

A coach picks a training plan. The athlete trains as well as possible under that plan, and then the coach judges the result in competition and adjusts the plan. The athlete solves the inner problem (train best given the plan); the coach solves the outer problem (pick the plan whose trained athlete wins).

To improve the plan quickly, the coach wants to know: “if I change the plan a little, how much better will the trained athlete do?” Answering that without retraining from scratch each time is what implicit differentiation does.

Optimize the optimizer's setting: a hyperparameter by hypergradient
validation (green) and training (thin) loss vs log₁₀ λ
train loss 0.277 · validation loss 0.316 · hypergradient dL_val/d log₁₀λ = 0.279 · best on grid: log₁₀ λ ≈ -2.70
Large dots: 14 training points. Small green dots: validation points. Dashed: the true curve. The inner problem fits degree-9 polynomial weights by ridge regression. The outer problem picks λ to minimize validation loss. Tiny λ overfits (wiggly curve, low training loss, high validation loss) and huge λ underfits. The orange tangent is the hypergradient from implicit differentiation, dw*/dλ = −H⁻¹w*, which needs no unrolling of the inner solver. Hypergradient descent walks λ to the validation minimum.
Why engineers care

Hyperparameter tuning, meta-learning (learning to adapt fast to a new robot or terrain), learning cost functions from demonstrations (inverse optimal control), differentiable MPC layers inside neural networks, and robust design against adversaries are all bilevel problems.

📝 Quick check (Level 0)
0/3 answered
  1. 1. In hyperparameter tuning, the inner problem is…
  2. 2. The outer problem judges the result on…
  3. 3. Implicit differentiation tells you…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

Bilevel problems

min⁡λ F(λ)=Louter(w⋆(λ),λ)s.t.w⋆(λ)=arg⁡min⁡wLinner(w,λ)\min_\lambda\ F(\lambda) = L_{\text{outer}}(w^\star(\lambda), \lambda)\quad\text{s.t.}\quad w^\star(\lambda) = \arg\min_wL_{\text{inner}}(w, \lambda)

The hypergradient is dFdλ=∂λLouter+(dw⋆dλ)⊤∇wLouter\frac{dF}{d\lambda} = \partial_\lambda L_{\text{outer}} + \big(\frac{dw^\star}{d\lambda}\big)^\top\nabla_wL_{\text{outer}}. The hard part is dw⋆/dλdw^\star/d\lambda.

Implicit function theorem

w⋆w^\star satisfies the optimality condition ∇wLinner(w⋆(λ),λ)=0\nabla_wL_{\text{inner}}(w^\star(\lambda), \lambda) = 0. Differentiate this identity in λ\lambda:

∇ww2Linner dw⋆dλ+∇wλ2Linner=0 ⟹ dw⋆dλ=−[∇ww2Linner]−1∇wλ2Linner\nabla^2_{ww}L_{\text{inner}}\,\frac{dw^\star}{d\lambda} + \nabla^2_{w\lambda}L_{\text{inner}} = 0\ \Longrightarrow\ \frac{dw^\star}{d\lambda} = -\big[\nabla^2_{ww}L_{\text{inner}}\big]^{-1}\nabla^2_{w\lambda}L_{\text{inner}}

You only need the optimality condition, not a formula for w⋆w^\star. Example: for ridge, Linner=12n∥Xw−y∥2+λ2∥w∥2L_{\text{inner}} = \frac1{2n}\|Xw - y\|^2 + \frac\lambda2\|w\|^2, so dw⋆/dλ=−H−1w⋆dw^\star/d\lambda = -H^{-1}w^\star with H=X⊤X/n+λIH = X^\top X/n + \lambda I.

📝 Quick check (Level 1)
0/3 answered
  1. 1. Implicit differentiation starts from…
  2. 2. For ridge, dw*/dλ =
  3. 3. The hypergradient combines dw*/dλ with…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 45 minUndergraduate students and practising engineers.

Two ways to compute hypergradients

  • Implicit: solve the inner problem to optimality, then do one linear solve with the inner Hessian. Memory is independent of how the inner problem was solved. It’s exact at an exact optimum.
  • Unrolled (iterative) differentiation: run TT inner steps and backpropagate through them (or carry forward-mode derivatives). It works for inexact or non-converged solvers but costs memory ∝ TT, and truncation biases it (short-horizon bias; Wu et al., 2018).

Large-scale inverse-Hessian products

When HH is too large to factor:

  • Conjugate gradient with Hessian-vector products (A2.4-7);
  • Neumann series H−1v≈η∑j<K(I−ηH)jvH^{-1}v\approx\eta\sum_{j<K}(I - \eta H)^jv (Lorraine, Vicol and Duvenaud, 2020), which is exactly what unrolled GD computes.

Applications

  • Hyperparameter optimization: weight decay, data augmentation strength, per-parameter learning rates.
  • Meta-learning: MAML’s outer loss is post-adaptation performance (Finn, Abbeel and Levine, 2017). Implicit MAML uses the implicit theorem (Rajeswaran et al., 2019).
  • Inverse optimal control / IRL: learn cost weights whose optimal plans match demonstrations.
  • Differentiable optimization layers (OptNet, differentiable MPC): differentiate through a QP’s KKT system (Amos and Kolter, 2017; Amos et al., 2018).
🐍 Exercise 1: hypergradients for ridge regression
Compute the validation-loss hypergradient for the ridge penalty by implicit differentiation and by unrolled GD. Compare with finite differences, then run hypergradient descent on log λ and compare with a grid search.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Unrolled differentiation's memory grows with…
  2. 2. Truncated unrolling tends to…
  3. 3. OptNet differentiates through…

Level 3: Advanced

★★★★☆ Advanced⏱ 45 minGraduate level, with full derivations and proofs.

Implicit function theorem for argmin

Let g(w,λ)=∇wLinner(w,λ)g(w, \lambda) = \nabla_wL_{\text{inner}}(w, \lambda). If g(w⋆,λ⋆)=0g(w^\star, \lambda^\star) = 0 and ∂wg=∇ww2Linner\partial_wg = \nabla^2_{ww}L_{\text{inner}} is invertible (for example, strong convexity), then near λ⋆\lambda^\star there’s a unique smooth w⋆(λ)w^\star(\lambda) with g(w⋆(λ),λ)=0g(w^\star(\lambda), \lambda) = 0. Differentiating gives ∂wg w˙+∂λg=0\partial_wg\,\dot w + \partial_\lambda g = 0. ∎

Ridge hypergradient

(H)w⋆=X⊤y/n(H)w^\star = X^\top y/n with H=X⊤X/n+λIH = X^\top X/n + \lambda I. Differentiating: Hw⋆′+w⋆=0Hw^{\star\prime} + w^\star = 0, so w⋆′=−H−1w⋆w^{\star\prime} = -H^{-1}w^\star. Then dLval/dλ=∇wLval⊤(−H−1w⋆)dL_{\text{val}}/d\lambda = \nabla_wL_{\text{val}}^\top(-H^{-1}w^\star). ∎

Unrolled converges to implicit

For a quadratic inner loss with Hessian AA, GD with forward-mode derivatives gives dwTdλ=−η∑j<T(I−ηH)j(⋯ )\frac{dw_T}{d\lambda} = -\eta\sum_{j<T}(I - \eta H)^j(\cdots), a truncated Neumann series for H−1H^{-1}. It converges geometrically to the implicit hypergradient when ∥I−ηH∥<1\|I - \eta H\| < 1. Scalar example: x←x−η(ax−θ)x\leftarrow x - \eta(ax - \theta) gives dxK/dθ=η∑j<K(1−ηa)j→1/adx_K/d\theta = \eta\sum_{j<K}(1 - \eta a)^j\to1/a. ∎

Exercise 3.1: The limit of η Σ(1 − ηa)ʲ with a = 4

The geometric series sums to η⋅1ηa=1/a=0.25\eta\cdot\frac1{\eta a} = 1/a = 0.25, provided ∣1−ηa∣<1|1 - \eta a| < 1.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The implicit function theorem for argmin requires the inner Hessian to be…
  2. 2. Unrolled GD derivatives form a truncated…
  3. 3. η Σ (1 − ηa)ʲ converges to 1/a when…

Level 4: Expert

★★★★★ Expert⏱ 40 minResearchers: original papers and open problems.

Learning what an expert cares about

An expert’s planner for a 1-D cart minimizes position error plus ρ\rho times control effort. We observe four of the expert’s (slightly noisy) control sequences, but not ρ\rho. Treat our own planner, u⋆(ρ)=arg⁡min⁡∥Φx0+Γu∥2+ρ∥u∥2u^\star(\rho) = \arg\min\|\Phi x_0 + \Gamma u\|^2 + \rho\|u\|^2, as the inner problem. The outer loss is the mismatch with the demonstrations. The implicit hypergradient du⋆/dρ=−(Γ⊤Γ+ρI)−1u⋆du^\star/d\rho = -(\Gamma^\top\Gamma + \rho I)^{-1}u^\star drives gradient descent on log⁡ρ\log\rho from 3.0 to 0.2999 (true 0.3). This is inverse optimal control, the core of learning-from-demonstration (Ratliff, Bagnell and Zinkevich, 2006; Englert et al., 2017).

🐍 Exercise 2 (research): inverse optimal control by implicit differentiation
Condense a double-integrator planner into a least-squares inner problem, generate expert demonstrations with an unknown effort weight, and recover the weight by implicit hypergradient descent.

Bilevel optimization in AI, robotics and aerospace

  • Meta-learning for fast adaptation of robot controllers to new payloads or terrains (MAML; Finn et al., 2017. Implicit MAML; Rajeswaran et al., 2019).
  • Differentiable MPC as a policy layer trained end to end (Amos et al., 2018). Differentiable QP layers (OptNet; Amos and Kolter, 2017).
  • Inverse optimal control and IRL for driving and manipulation from demonstrations (Ratliff, Bagnell and Zinkevich, 2006, maximum-margin planning; Levine and Koltun, 2012).
  • Hyperparameter optimization at scale: implicit hypergradients for millions of hyperparameters (Lorraine, Vicol and Duvenaud, 2020).
  • Stackelberg games and robust design: a designer anticipates an optimal adversary or follower (Colson, Marcotte and Savard, 2007).

References

  1. B. Colson, P. Marcotte, G. Savard (2007). “An overview of bilevel optimization.” Annals of Operations Research, 153.
  2. J. Domke (2012). “Generic methods for optimization-based modeling.” AISTATS.
  3. D. Maclaurin, D. Duvenaud, R. Adams (2015). “Gradient-based hyperparameter optimization through reversible learning.” ICML.
  4. J. Lorraine, P. Vicol, D. Duvenaud (2020). “Optimizing millions of hyperparameters by implicit differentiation.” AISTATS.
  5. C. Finn, P. Abbeel, S. Levine (2017). “Model-agnostic meta-learning for fast adaptation of deep networks.” ICML.
  6. A. Rajeswaran, C. Finn, S. Kakade, S. Levine (2019). “Meta-learning with implicit gradients.” NeurIPS.
  7. B. Amos, J. Z. Kolter (2017). “OptNet: differentiable optimization as a layer in neural networks.” ICML.
  8. B. Amos, I. Jimenez, J. Sacks, B. Boots, J. Z. Kolter (2018). “Differentiable MPC for end-to-end planning and control.” NeurIPS.
  9. N. Ratliff, J. A. Bagnell, M. Zinkevich (2006). “Maximum margin planning.” ICML.
  10. Y. Wu, M. Ren, R. Liao, R. Grosse (2018). “Understanding short-horizon bias in stochastic meta-optimization.” ICLR.