TAGS :Viewed: 14 - Published at: a few seconds ago

[ Using Optuna to Optimize XGBoost Hyperparameters ]

This post uses XGBoost v1.0.2 and optuna v1.3.0 . XGBoost + Optuna! Optuna is a hyperparameter optimization framework applicable to machine learning frameworks and black-box optimization solvers. XGBoost isis an optimized distributed gradient boosting library designed to be highly efficient, flexible and portable. Let’s see how they can work together! Creating the Objective Function Optuna is a black-box optimizer, which means it needs an objective function, 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 a cancer detection example from the Optuna GitHub pruning examples folder. In this case, the objective function looks like this: That’s a long block of code, so let’s break it down into pieces and look at how Optuna is tuning the hyperparameters. 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 example, we optimize the hyperparameters here: There are a number of different kinds of hyperparameters set here. The booster is chosen from trial.suggest_categorical(“optimizer”, [“gbtree”, “gblinear”, “dart”]) . lambda varies by orders of magnitude, so trial.suggest_loguniform('lambda', 1e-8, 1.0) is used , which will vary the values logarithmically from .000000001 to 1.0. Depending on the booster being tested (if boost or dart ), Optuna leverages normal Python looping to determine the depth using trial.suggest_int(“max_depth”, 1, 9) . 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) to do one hundred trials. Each trial is chosen after evaluating all the trials that have been previously done, using the TPE sampler 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 XGBoost ( XGBoostPruningCallback ) pruning that provides all of these functions. After defining the pruning_callback , it is passed to the training to allow XGBoost to do the pruning, and ensure unpromising trials are stopped early. To the Future, and Beyond! Plot Contour Visualization For those interested, Optuna has many other features, including a 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! Installation Optuna Github