Gradient-Based Hyperparameter Optimization and the Implicit Function Theorem
The most approaches to hyperparameter optimization can be viewed as a bi-level optimization—the "inner" optimization optimizes training loss (wrt ), while the "outer" optimizes hyperparameters ().
Can we estimate so that we can run gradient-based optimization over ?
Well, what does it mean to have an inside a function?
Well, it means that there is a that gets passed to . And, is a function of , denoted . Furthermore, must set the derivative of the inner optimization to zero in order to be a local optimum of the inner function. So we can rephrase the problem as
Now how does change as the result of an infinitesimal change to ?
The constraint on the derivative implies a type of "equilibrium"—the inner optimization process will continue to optimize regardless of how we change . Assuming we don't change too much, then the inner optimization shouldn't change too much and it will change in a predictable way.
To do this, we'll appeal to the implicit function theorem. Let's look at the general case to simplify notation. Suppose and are related through a function as follows,
Assuming is a smooth function in and , we can perturb either argument, say by a small amount and by . Because system preserves the constraint, i.e.,
We can solve for the change of as a result of an infinitesimal change in . We take the first-order expansion,
Since is already zero,
Next, we solve for .
Back to the original problem: Now we can use the implicit function theorem to estimate how varies in by plugging in $g \mapsto \frac{\partial \mathcal{L}_{\text{train}}}{\partial \theta}$, and :
This tells us how changes with respect to an infinitesimal change to . Now, we can apply the chain rule to get the gradient of the whole optimization problem wrt ,
Since we don't like (explicit) matrix inverses, we compute $- \left( \frac{ \partial^2 \mathcal{L}{\text{train}} }{ \partial \theta, \partial \theta^\top } \right)^{-1} \frac{ \partial^2 \mathcal{L}{\text{train}} }{ \partial \theta, \partial \lambda^\top}$ as the solution to $\left( \frac{ \partial^2 \mathcal{L}{\text{train}} }{ \partial \theta, \partial \theta^\top } \right) x = -\frac{ \partial^2 \mathcal{L}{\text{train}}}{ \partial \theta, \partial \lambda^\top}$. When the Hessian is positive definite, the linear system can be solved with conjugate gradient, which conveniently only requires matrix-vector products—i.e., you never have to materialize the Hessian. (Apparently, matrix-free linear algebra is a thing.) In fact, you don't even have to implement the Hessian-vector and Jacobian-vector products because they are accurately and efficiently approximated with centered differences (see earlier post).
At the end of the day, this is an easy algorithm to implement! However, the estimate of the gradient can be temperamental if the linear system is ill-conditioned.
In a later post, I'll describe a more-robust algorithms based on automatic differentiation through the inner optimization algorithm, which make fewer and less-brittle assumptions about the inner optimization.
Further reading:
- Truncated Bi-Level Optimization
- Efficient multiple hyperparameter learning for log-linear models
- Gradient-based Hyperparameter Optimization through Reversible Learning
- Hyperparameter optimization with approximate gradient (paper): This paper looks at the implicit differentiation approach where you have an approximate solution to the inner optimization problem. They are able to provide error bounds and convergence guarantees under some reasonable conditions.