Hyperparameter Optimization Fun
Published: 2022-01-16 12:26:35

---
title: Hyperparameter Optimization Fun
published: true
date: 2021-09-27 23:00:00 UTC
tags: 
canonical_url: https://dataroots.io/research/contributions/hyperparameter-optimization-fun
---

<sub><b>original author(s):</b> Hans Tierens</sub>

You all know that datarootsians are excellent data athletes. Olympics athletes train <u>with</u> weights, we evolved past that mere display of physical strength and started training <u>the</u> weights. In this way, Machine Learning Engineers train our models to achieve optimal performance on any task given to us. That’s how we shine!

![weight](https://media.giphy.com/media/KDQ25pahVfwGRvvT9X/giphy.gif?cid=ecf05e47z4pp5k1wkdxdbvvvev66kbx8qag6z8b2ml0vzyg2&rid=giphy.gif)

Behind the shine and bling-bling, we have to admit that the performance of our models depends largely on how our model looks like, call it model infrastructure. This infrastructure is governed by a set of ‘hyperparameters’. Think about the depth of a tree, the number of layers and nodes in a neural network, weight regularization terms and even distributions, activation functions, and dropout probabilities. All of these choices need to be made before we can even start training our weights and these choices cannot be trained by the model itself. Therefore, choosing the best hyperparameters is one of the key challenges of many Machine Learning Engineer (PS: we do a lot more cool stuff, but we’ll talk about that in the next Homemade Muffin-meeting).

In the days of yore, we chose not to tune hyperparameters at all and rely on vanilla models or used Thibauld’s darts-at-office skills to pick the best hyperparameters. Even though Thibauld’s skills remain uncontested, we can do better when picking hyperparameters.

By now, we are quite familiar with the _good ol’_ grid search, random search and some of us might have heard about evolutionary algorithms using heuristics (based on birds and ants) or genetic approaches (based on NSFW). The problem with these algorithms are that (1) they take a _friggin’_ long time as a whole new model needs to be trained for every possible combination of hyperparameters, (2) they do not ensure optimal hyperparameters as we can only hope to randomly pick the best, and (3) these algorithms do not really _learn_ from previous trials as we can only take blind steps in the direction of where we hope the grass is greener.

![grass greener](https://media.giphy.com/media/A03pTCglKdaRG/giphy.gif?cid=ecf05e47zdbs5xfg8e3hy4ssl3ykc7chfptrevb6wt57azc6&rid=giphy.gif&ct=g)

Before we can prance through green meadows with flowers in our hair, we need to come up with an approach that gives us a flexible and robust approach to effectively and efficiently find the best hyperparameters in the configuration space… in a fast and preferably cheap way. When confronted with a seemingly impossible problem, we all know that one guy that says: “Let’s go Bayesian”.

![](https://media.giphy.com/media/l378BzHA5FwWFXVSg/giphy.gif?cid=ecf05e47t6z632ky9je9iafd6s0v9xs7md2qsuovl7sat2gl&rid=giphy.gif&ct=g)

Honoring the true Bayesian spirit, allow me to make things easy for you:

- First, we just have to try training the full model a few times, each time using distinct hyperparameters. We log the hyperparameters and the resulting loss metric of our trials. We call this _'evaluation of the objective function'_. Luckily for us, about 3 runs will do the trick.

- After this, we infer a **surrogate model** –call it a meta-model– which maps the hyperparameters to our loss metric (in the test set). A good surrogate model predicts how our loss is expected to evolve when we change our hyperparameters and incorporates our uncertainty. The key takeaways for a surrogate model are:

- We also define an **acquisition function** to help us choose the next hyperparameters to be tried. This acquisition function balances exploitation (_rely on the current best_) and exploration (_keep exploring better possibilities_) of our configuration space. We would be wise to move in the direction we expect the next trial to be much better than the best result found so far, call this Expected Improvement.

- Repeat this process a few times leads you to the best set of hyperparameters quite fast and with as few iterations as possible.

Starts to sound insanely -Bayesian-y- simple, doesn’t it?

Remember I started of with talking about athletics (if not, scroll up)? Well, in hyperparameter optimization, we are allowed to play dirty! We can actually put our meta-models on **steroids** , which resulted in the creation of our good friend [BOHB](https://tenor.com/view/spongebob-muscles-ripped-gif-9019709.gif), the current state-of-the-art in hyperparameter optimization. This is basically where most AutoML libraries, both commercial and open-source, are thriving on.

I’ll explain this, and much more, in the upcoming Rootlabs@Lunch. Make sure to tune in and spread the word!