After self-implementing a grid-search but having a horrible time writing pyplot visualizing the result, I finally decided to find an existing tool to do the HP tuning for me.
There are two popular HP tuning framework
- RayTune: almost industry standard
- Optuna: user friendly, requires minimal modification to original code
There’s also skorch integrating
scikit-learn and pytorch, so you can use sklearn
GridSearchCV
. For our simple task, we will go with
Optuna
.
Getting Started
To get Optuna running, you just need to add 4 lines in your training logic and a few more lines to start its search. In training logic:
1 | def train_model(image_datasets, lr, weight_decay, num_epochs, trial : optuna.trial.Trial=None): |
The following code shows how to set the search space and start the search.
1 | def optuna_objective(trial : optuna.trial.Trial): |
The above example was adapted from Optuna’s PyTorch starting example. For more reporting printout statements, check the original example.
Saving Study and Board Visualization
In addition to printing out all the info to the console and losing
them from memory after this python script finishes, we can save them in
the form of an RDB (Relational Database, or just database as most
databases are RDB). To do this, we pass a database URL to the
storage
argument
1 | study = optuna.create_study( |
You can now Ctrl+C stop this search at anytime and resume it by running the same code again.
Database exposes itself as a server in machines. Therefore, to access
it (even in local machine), we use Database URL. Just like to access a
webpage online, we use an HTTPS url. In our example here, the history
will be stored in a file called db.sqlite3
under current
directory.
This file is a general database and can store study other than the
one called plant_144
. You can store another study inside
it.
1 | study = optuna.create_study( |
For me this code just worked without having to install SQLite DB. This is probably because it comes with my Ubuntu but I have no idea. Check official tutorial Saving/Resuming Study for more on saving and loading.
You can now visualize the search history, each parameter’s importance, etc. with optuna-dashboard
1 | optuna-dashboard sqlite:///db.sqlite3 |

Multi-GPU Parallelism Support
roman’s Stack Overflow answer provides a very simple way to do multi-GPU tuning by utilizing Optuna’s resume feature. To do so, create a study by following the previous code. Then modify your code now to resume instead of starting a new study.
1 | if __name__ == '__main__': |
and simply start “resume” this study on different available GPUs
1 | CUDA_VISIBLE_DEVICES=3 nohup python optuna.py > log3.txt 2>&1 & |
The history from both processes will be stored under the study called
plant_144
in file db.sqlite3
.
For more information on parallelizing on multiple gpu, check official guide: Easy Parallelization
Some Complaints
In its visualization, Optuna doesn’t provide an option to filter out the “bad” trial runs, making the scale of all graph ridiculous and usually of no information.