ADSTuner
ADSTuner
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 1/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 2/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
Nupur Chatterji
Machine Learning Engineer at Oracle
A key step in model development is the optimization of hyperparameters. The ADSTuner class
performs a hyperparameter search, sometimes called hyperparameter tuning. It does this by searching
over a range or distribution of values looking for the best model. This powerful module is available as
part of the most recent release of the Oracle Accelerated Data Science (ADS) library. This blog post
shows you how to:
Each class of model has its own set of hyperparameters, and a common problem is determining what
range or distribution should be used in the search for the best hyperparameters. If the range is too
broad, it takes a long time to carry out the search. If the range is too narrow, it may miss out on the
globally optimal solution. ADSTuner addresses this problem by providing a sensible set of values that
are specific to each supported model class, or you can specify your own values. This optimization
process can be expensive if there is a large search space. Therefore, ADSTuner prunes trials that do
not appear promising, thus reducing computational costs.
Let us start by using sklearn's stochastic gradient descent classifier model, SGDClassifier(), and the
classic Iris dataset. Before we tune the model, the data needs to be split into test and train datasets.
model = SGDClassifier()
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y)
To instantiate the ADSTuner object, it requires the model that is to be tuned. Optionally, we can specify
the number of cross-validation folds to be used with each set of parameters and/or the search
strategy.
Subscribe to the Oracle Data Science Newsletter to get the latest ML and data science content sent
straight to your inbox!
The default strategy is perfunctory and this approach searches over a subset of the most important
hyperparameters. Generally, this is used in the early stages of model assessment, when we are trying
to determine which class of models show the most promise. By using the perfunctory strategy, the
cost of computing is reduced by not prematurely optimizing on model classes that do relatively poorly.
The detailed strategy creates a sensible though larger search space for ADSTuner. It generally finds
a better solution than the perfunctory strategy, but the computational cost is usually much higher.
The detailed strategy is useful when you know what model classes perform well on your data and
you want to find an optimal solution. The following code snippet shows how the define a strategy
when instantiating the tuner.
At its core, the strategy parameter is a dictionary where the key is the name of the hyperparameter
and the value is a distribution function. The perfunctory and detailed strategies are aliases to
dictionaries that are specific to each model class. Later on, we will see how you can access dictionaries
and optionally modify them.
Each hyperparameter in the model can have an entry in the strategy parameter. If it is omitted, then
that parameter is not tuned and a default value is used. In the following example, a logistic regression
is tuned. This model has the C , solver , and max_iter hyperparameters. The C hyperparameter is
sampled from a log-uniform distribution. The solver is set to 'saga' using a categorical distribution
with a single category and the max_iter is sampled from a uniform integer distribution between 500
d 2000
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 4/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
and 2000.
The tune() method has a scoring parameter used to score the model's performance. By default, it
uses sklearn's behavior. This parameter is used to assess the quality of the model and it can also be
used as a stopping criterion, more on that soon. The following example is a logistic regression, and we
want to balance the type I and II errors so we will score on the f1_score .
model = LogisticRegression()
tuner = ADSTuner(model, cv=3,
strategy = {'C': LogUniformDistribution(low=1e-05, high=1
'solver': CategoricalDistribution(['saga']),
'max_iter': IntUniformDistribution(500, 2000,
scoring=sklearn.metrics.make_scorer(f1_score, average='we
tuner.tune(X_train, y_train)
The stopping criteria can also be specified with the exit_criterion parameter. ADSTuner continues
to search until one of the stopping criteria is reached. The supported stopping criteria are:
2. TimeBudget(T): The maximum amount of seconds that the search is allowed to run.
It is possible and often desirable to combine these criteria together. The values are passed as a list, and
the search stops once any criteria is reached.
The following example shows that for the model class being used with the perfunctory strategy, the
alpha parameter is sampled from a log-uniform distribution between 0.0001 and 0.1, and there are
three penalty types tried, L1, L2, and no penalty.
Detailed information about the tuning trials is obtained from the tuner.trials attribute. It provides
information like the trial number, model score, start/end times, and duration of the tuning step. It also
provides information about the various hyperparameter values used in the trial.
The tuner.best_index attribute gives the index in tuner.trials where the model score was the best.
This allows us to determine what hyperparameters should be used to train the final model, or to
perform a finer-grained hyperparameter search near these values. Depending on the stopping criteria,
the number of trials conducted can vary. Therefore, the tuner.n_trials gives the number of trials,
and also the cardinality of the tuner.trials output.
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 6/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
Each tuning operation uses a model score to assess the quality of the model and its associated
hyperparameters. The tuner.scoring_name provides the name of the metric that was used. The
tuner.best_score gives the numerical value of the best model score. In the following example, the
quality of the models were assessed by the mean accuracy.
The tuner.best_param provides a dictionary of the hyperparameters that were tuned. It also provides
the set of parameters that resulted in the best model, as assessed by the tuner.scoring_name metric
and the tuner.best_score value.
To maximize the potential for finding the most ideal hyperparameters, ADSTuner offers the option to
stop tuning, monitor intermediate results, and resume tuning if there is potential for further
improvement with additional runs. Perhaps in the prior run, the exit criterion limited tuning to only 5
trials or 200 seconds. With the option to resume, the tuning can continue for longer and possibly find
more optimal hyperparameters. The next code snippet displays how to do this.
tuner.resume(exit_criterion=[NTrials(1500)])
tuner.plot_best_scores()
It is often helpful to understand not just the best score but how the score varies with various
hyperparameters. A contour map can be created with tuner.plot_contour_scores() where the
hyperparameters in which the score is to be plotted against is given. In the following figure, the alpha
value and penalty (L1, L2, and no penalty) are plotted. It shows that there are two alpha values (the
dark regions on the left-hand side and the middle) where the score is near the lowest value. The figure
also indicates that L2 penalty performs slightly better than L1 and no penalty, but the model is not
very sensitive to the penalty term.
tuner.plot_contour_scores(params=['penalty', 'alpha'])
Parallel coordinate plots can be a way to distill complex relationships down into simple to understand
representations. In the following plot, the model score is plotted with the penalty and alpha scores. It
shows that many of the lines pass through alpha values near 0.01 and 0.001. The objective values are
all coming from values near 0.98. This suggests that there is a local extremum value near there when
the alpha values are near 0 01 and 0 001 and the penalty is none This information can be used to
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 8/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
the alpha values are near 0.01 and 0.001 and the penalty is none. This information can be used to
modify the search criteria and allow the tuner to search over a smaller space and still obtain a near-
optimal set of hyperparameters.
tuner.plot_parallel_coordinate_scores(params=['penalty', 'alpha'])
The empirical cumulative distribution plot of the model score provides information on the distribution
of score values. It can be used to assess the size of the solution space near the optimal score.
tuner.plot_edf_scores()
tuner.plot_intermediate_scores()
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 9/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
In summary
This post provides an overview of the ADSTuner, a hyperparameter optimization engine that can be
run in the Oracle Cloud Infrastructure Data Science service notebook session environment. Various
code snippets detail how to use the engine, customize strategies, and visualize the tuning process.
I also invite you to participate in our upcoming Oracle Developer Live: AI and ML for Your Enterprise
event on January 26, 28, and February 2, 2021. Learn how to optimize the machine learning lifecycle
with technical sessions and hands-on labs, including two sessions on Accelerated Data Science.
Keep in touch!
- Visit our website
Nupur Chatterji
Machine Learning Engineer at Oracle
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 10/11
29/10/2024, 17:29 Optimizing estimators with the ADSTuner: A hyperparameter optimization engine
© 2024 Oracle Privacy / Do Not Sell My Info Cookie Preferences Ad Choices Careers
https://blogs.oracle.com/ai-and-datascience/post/optimizing-estimators-with-the-adstuner-a-hyperparameter-optimization-engine 11/11