Using Optuna to Optimize MXNet Hyperparameters

Crissman Loomis
Optuna
Published in
4 min readMay 11, 2020

MXNet + Optuna!

Optuna is a hyperparameter optimization framework applicable to machine learning frameworks and black-box optimization solvers. MXNet is an open source machine learning framework for flexible research prototyping and production. Let’s see how they can work together!

Creating the Objective Function

Optuna is a black-box optimizer, which means it needs an objectivefunction, which returns a numerical value to evaluate the performance of the hyperparameters, and decide where to sample in upcoming trials.

In our example, we will be doing this for identifying MNIST characters. In this case, the objective function looks like this:

Notice that the objective function is passed an Optuna specific argument of trial. This object is passed to the objective function to specify which hyperparameters should be tuned. This returns the accuracy of the model, which is used by Optuna as feedback on the performance of the trial.

Defining the hyperparameters to be tuned

Optuna allows you to define the kinds and ranges of hyperparameters you want to tune directly within your code using the trial object. This saves the effort of learning specialized syntax for hyperparameters, and also means you can use normal Python code for looping through or defining your hyperparameters.

Optuna supports a variety of hyperparameter settings, which can be used to optimize floats, integers, or discrete categorical values. Numerical values can be suggested from a logarithmic continuum as well. In our MNIST example, the model hyperparameters are optimized here:

For the definition of the model itself, Optuna leverages eager mode to allow normal Python looping to determine the number of layers and nodes in each layer with trial.suggest_int(“n_layers”, 1, 3)for the layers and num_hidden = int(trial.suggest_loguniform(“n_units_1{}”.format(i), 4, 128)) for the number of hidden nodes in each layer. suggest_loguniformis used to vary the number by orders of magnitude.

The optimizer is chosen from optimizer_name = trial.suggest_categorical(“optimizer”, [“Adam”, “MomentumSGD”]), which chooses among Adam and Momentum Stochastic Gradient Descent optimizers.

The learning rates for these optimizers varies by orders of magnitude, so weight_decay = trial.suggest_loguniform(“weight_decay”, 1e-10, 1e-3) is used, which will vary the values logarithmically from 10^-10 to 0.001.

Running the Trials

The default sampler in Optuna Tree-structured Parzen Estimater (TPE), which is a form of Bayesian Optimization. Optuna uses TPE to search more efficiently than a random search, by choosing points closer to previous good results.

To run the trials, create a study object to set the direction of optimization (maximize or minimize). Then, run the study object with study.optimize(objective, n_trials=100, timeout=600) to do one hundred trials, with a timeout of ten minutes.

Each trial is chosen after evaluating all the trials that have been previously done, using the TPEsampler to make smart guesses where the best values hyperparameters can be found. The best values from the trials can be accessed through study.best_trial. Other methods of viewing the trials, such as formatting in a dataframe, are also available.

Pruning — Early Stopping of Poor Trials

Pruning trials is a form of early-stopping which terminates unpromising trials, so that computing time can be used for trials that show more potential. In order to do pruning, it’s necessary to open up the black-box of the Objective function some more to provide intermittent feedback on how the trial is going to Optuna, so it can compare the progress with the progress of other trials, and decide whether to stop the trial early, and provide a method to receive a method from Optuna when the trial should be terminated, and also allow the trial in session to terminate cleanly after recording the results. Fortunately, Optuna provides an integration for MXNet (MXNetPruningCallback) pruning that provides all of these functions.

This integration is passed as the eval_end_callback during the fitting of the model to do the pruning. The eval_metric argument of the MXNetPruningCallback function can reference the MXNet such as accuracy or loss.

To the Future, and Beyond!

Plot Contour Visualization

For those interested, Optuna has many other features, including visualizations, alternative samplers, optimizers, and pruning algorithms, as well as the ability to create user-defined versions as well. If you have more computing resources available, Optuna provides an easy interface for parallel trials to increase tuning speed.

Give Optuna a try!

This post uses MXNet v1.6.0 and optuna v1.3.0.

--

--