Bayesian Optimization for Hyperparameter Tuning

# Source
* [A Tutorial on Bayesian Optimization of Expensive Cost Functions, with Application to Active User Modeling and Hierarchical Reinforcement Learning](https://arxiv.org/abs/1012.2599)
* [Hyperparameter tuning in Cloud Machine Learning Engine using Bayesian Optimization](https://cloud.google.com/blog/products/gcp/hyperparameter-tuning-cloud-machine-learning-engine-using-bayesian-optimization)
* [Hyperparameter tuning on Google Cloud Platform is now faster and smarter](https://cloud.google.com/blog/products/gcp/hyperparameter-tuning-on-google-cloud-platform-is-now-faster-and-smarter)

# Introduction
Hyperparameters in neural networks are important; they define the network structure and affect model updates by controlling variables such as learning rates, optimization method and loss function.  Therefore, it is imperative to find an efficient way of finding the optimal set of hyperparameters.

Quaint methods such as **grid search** are sluggish as they crawl through the entire search space and therefore end up wasting significant resource.  By contrast, **random search**, which samples the search space randomly, ameliorates this effect and is widely used in practice.  A drawback of random search, however, is that it doesn’t use information from prior experiments to select the next setting.  This is particularly undesirable when the cost of running experiments is high and you want to make an educated decision on what experiment to run next.

Bayesian optimization is an extremely powerful technique when the mathematical form of the objective function is unknown or expensive to compute. The main idea behind it is to compute a **posterior** distribution (also called **surrogate function**) over **prior** (the objective function) based on the data (using the famous **Bayes theorem**), and then select good points to try with respect to this posterior distribution.  If we can completely match the posterior with prior, then we can precisely determine the configuration of the hyperparameters which invokes the highest return.

Much of the efficiency stems from the ability of Bayesian optimization to incorporate prior belief about the problem to help direct the sampling, and to trade off exploration and exploitation of the search space.

# Implementation
## Goal
The main goal is to optimize a function $f(x)$ (which we will define as the performance of a function of hyperparameter values, e.g. model accuracy) over a compact set $A$ (the search space of the said hyperparameters), written mathematically as:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/cb484100-ac2a-4a59-a6a8-fcf582edf91f.png)
An example would be, if $f(x) = (1-x)e^x$, the maximum value $f(x) = 1$ will occur at $x = 0$ and so arg max is 0.

</br>
Bayes theorem states that the posterior probability of a model $M$ given evidence
(or data, or observations) $E$ is proportional to the likelihood of $E$ given $M$ multiplied by the prior probability of $M$:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/d7cefc3a-4146-505b-c763-79c6099bf30c.png)
Using the accumulated observation $D_{1:t}=\{x_{1:t},f(x_{1:t})\}$, the prior is combined with the likelihood function $P(D_{1:t}|f)$, giving us:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/6cd3a21a-30a4-f039-d1aa-5e66fba67e68.png)

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/2a720ebc-b401-d6aa-671e-223c5e084291.png)

## Exploration-exploitation
**Exploration**: Pick a random place that may yield a good or bad result (variance is high).
**Exploitation**: Pick a well acquainted place that have historically given fairly good result (mean is high).  Although it may not be the best possible result available.

**Gaussian process (GP)** is a distribution over functions specified by its mean function, $m$ and covariance function, $k$.  It returns the mean and variance of a normal distribution.

From the Gaussian process, we know that the predicted distribution (posterior) for the observed value at $x_{t+1}$ given the first $t$ observations, $D_t$ is:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/eb9190eb-5827-8dcc-f2db-e1ad4768d118.png)
where
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/f43a7a69-da7b-7e8e-3110-e3e89f3beebd.png)
$y$ is the t-dimensional vector of observed values, and $K$ and $k$ are:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/8c0d7fb6-5c72-f229-b3dc-405053dec6f0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/a7ac9b7a-7037-1110-e217-7fabb99916b2.png)

A very popular choice for $k$ is the **squared exponential function**:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/2672c18c-11bc-67db-12aa-5f0b97092c62.png)

The choice of covariance function $k$ is crucial, and the above squared exponential kernel might be slightly naive.  It may be beneficial to introduce other parameters in order to control behaviours such as kernel width and smoothness.

## Acquisition Function
Rather than sampling randomly, Bayesian optimization uses **acquisition function**, which does automatic trade-off between exploration and exploitation.  At each iteration, the acquisition function is maximized to determine where next to sample from the objective function.  The objective is then sampled at the argmax of the acquisition function, and based on the return value from the objective function, the Gaussian process is updated and this process is repeated.

Bayesian optimization use evidence and prior knowledge to maximize the posterior at each step, so that each new evaluation decreases the distance between the true global maximum and the expected maximum.

### Upper confidence bound
Perhaps the simplest acquisition function looks at an optimistic value for the point. Given a parameter $\beta$, it assumes the value of the point will be $\beta$ standard deviations above the mean. Mathematically, it is
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/1ce4bd9f-924e-d2bc-4cb4-9a39435e7c94.png)
*The equation would become $\alpha(x) = \mu(x) - \beta\sigma(x)$ for lower confidence bound if we were to find the minimum instead.*
By varying $\beta$, we can encourage the algorithm to explore or exploit more.  Note that the value for $\beta$ is left to the user.

### Probability of improvement
The main idea behind probability of improvement acquisition function is that we pick the next point based on the **maximum probability of improvement (MPI)** with respect to the current maximum.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/9c8080a9-61e6-2a51-1b04-c6b545a78f1e.png)
*Note that $y^+$ is the same as $f(x^+)$,  $u(x)$ is the acquisition function, and $P(x)$ = $PI(x)$.*

In the above figure, the maximum observed value (so far), $y^+$ is at $x^+$. The area of the green shaded region gives the probability of improvement at $x_3$. The model predicts negligible possibility of improvement at $x_1$ or $x_2$. Whereas sampling at $x_3$ is likely to produce an improvement over $y^+$.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/56d02a57-beb0-50be-c6de-7970aea24a20.png)
Where $\Phi$ is normal cumulative distribution function (the probability that a random variable $X$ will take a value less than or equal to $x$).

### Expected Improvement
The algorithm which Google's Cloud ML Engine uses for acquisition function is called **Expected Improvement**. It measures the expected increase in the maximum objective value seen over all experiments, given the next point we pick. In contrast, MPI only takes into account the probability of improvement and not the magnitude of improvement for the next point. Mathematically, as proposed by Mockus, it is
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/34b18d10-d60f-5c1b-8aa3-5a96ffec4fd5.png)
where the expectation is taken over $Y_{t+1}$ which is distributed as the posterior objective value at $x_{t+1}$ In other words, if the objective function were drawn from our posterior distribution, then $EI(x)$ measures the expected increase in the best objective we have found during entire tuning run if we select $x$ and then stop.

When using a Gaussian process we can obtain the following analytical expression for EI
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/3ce8a986-0ae5-657c-19dc-dd68f552d7af.png)

Where $Z$ has the expression denoted below and $\phi$ and $\Phi$ denote the **probability density function (PDF)** and **cumulative distribution function (CDF)** of the standard normal distribution function.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/7824356e-ff46-c641-76ef-8b4d65f485eb.png)
$\phi$ = ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/4a812354-0ebb-6346-72c9-d7252d3c63bd.png)
$\Phi$ = ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/51105d2a-f7f1-42cb-3aea-9751654985b0.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/4fb8f044-b4f3-8d57-e9fd-0fad63d22f40.png)

## Bayesian Optimization Algorithm
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/6a27cb67-230f-685d-d442-50ce8d820f9d.png)

# Result
## Interactive Bayesian optimization for material design
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/71528a4d-a28b-b5b2-def1-f784ed1f9df6.png)
In the above figure, the user would select from the provided two images, one that most resembles the target image at each iteration.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/264921/195d9077-1aa8-b91b-4eb5-cb6deb0800a8.png)
It can be seen that $argmax_{EI}$ requires conspicuously fewer iterations in order to derive the correct material parameters.

# Conclusion
Searching for hyperparameters can be made more efficiently through the usage of Bayesian Optimization.  There are actually further under the hood features implemented by Google for their AI Platform hyperparameter tuning service that further improves the quality of life during parameter searching.  Options such as early stopping, where the system stops parameter searching if it determines a better configuration does not exist, and learning from previously visited parameter set in order to speed up the algorithm when expanding on the parameter space.

There are of course other issues, such as handling the trade-off between exploration and exploitation in the acquisition function.  Or the fact that the acquisition is only looking one-step ahead (although it should be able to implement multi-step through recursion).  There may have been other advancements made in recent years that will require further investigation.