hyperparameter optimization in classification learner app
after you choose a particular type of model to train, for example a decision tree or a support vector machine (svm), you can tune your model by selecting different advanced options. for example, you can change the maximum number of splits for a decision tree or the box constraint of an svm. some of these options are internal parameters of the model, or hyperparameters, that can strongly affect its performance. instead of manually selecting these options, you can use hyperparameter optimization within the classification learner app to automate the selection of hyperparameter values. for a given model type, the app tries different combinations of hyperparameter values by using an optimization scheme that seeks to minimize the model classification error, and returns a model with the optimized hyperparameters. you can use the resulting model as you would any other trained model.
note
because hyperparameter optimization can lead to an overfitted model, the recommended approach is to create a separate test set before importing your data into the classification learner app. after you train your optimizable model, you can see how it performs on your test set. for an example, see .
to perform hyperparameter optimization in classification learner, follow these steps:
choose a model type and decide which hyperparameters to optimize. see .
note
hyperparameter optimization is not supported for logistic regression, efficiently trained linear, or kernel approximation models.
(optional) specify how the optimization is performed. for more information, see .
train your model. use the to track the optimization results.
inspect your trained model. see .
select hyperparameters to optimize
in the classification learner app, in the models section of the classification learner tab, click the arrow to open the gallery. the gallery includes optimizable models that you can train using hyperparameter optimization.
after you select an optimizable model, you can choose which of its hyperparameters you want to optimize. in the model summary tab, in the model hyperparameters section, select optimize check boxes for the hyperparameters that you want to optimize. under values, specify the fixed values for the hyperparameters that you do not want to optimize or that are not optimizable.
this table describes the hyperparameters that you can optimize for each type of model and the search range of each hyperparameter. it also includes the additional hyperparameters for which you can specify fixed values.
model | optimizable hyperparameters | additional hyperparameters | notes |
---|---|---|---|
optimizable
tree |
|
| for more information, see . |
optimizable
discriminant |
|
for more information, see . | |
optimizable naive
bayes |
|
|
for more information, see . |
optimizable
svm |
|
for more information, see . | |
optimizable
knn |
| for more information, see . | |
optimizable
ensemble |
|
|
for more information, see . |
optimizable neural
network |
|
| for more information, see . |
optimization options
by default, the classification learner app performs hyperparameter tuning by using bayesian optimization. the goal of bayesian optimization, and optimization in general, is to find a point that minimizes an objective function. in the context of hyperparameter tuning in the app, a point is a set of hyperparameter values, and the objective function is the loss function, or the classification error. for more information on the basics of bayesian optimization, see bayesian optimization workflow.
you can specify how the hyperparameter tuning is performed. for example, you can change the optimization method to grid search or limit the training time. on the classification learner tab, in the options section, click optimizer. the app opens a dialog box in which you can select optimization options.
after making your selections, click save and apply. your selections affect all draft optimizable models in the models pane and will be applied to new optimizable models that you create using the gallery in the models section of the classification learner tab.
to specify optimization options for a single optimizable model, open and edit the model summary before training the model. click the model in the models pane. the model summary tab includes an editable optimizer section.
this table describes the available optimization options and their default values.
option | description |
---|---|
optimizer | the optimizer values are:
|
acquisition function | when the app performs bayesian optimization for hyperparameter tuning, it uses the acquisition function to determine the next set of hyperparameter values to try. the acquisition function values are:
for details on how these acquisition functions work in the context of bayesian optimization, see acquisition function types. |
iterations | each iteration corresponds to a combination of
hyperparameter values that the app tries. when you use
bayesian optimization or random search, specify a positive
integer that sets the number of iterations. the default
value is when you use grid search, the app ignores the iterations value and evaluates the loss at every point in the entire grid. you can set a training time limit to stop the optimization process prematurely. |
training time limit | to set a training time limit, select this option and set the maximum training time in seconds option. by default, the app does not have a training time limit. |
maximum training time in seconds | set the training time limit in seconds as a positive real
number. the default value is 300 . the run
time can exceed the training time limit because this limit does
not interrupt an iteration evaluation. |
number of grid divisions | when you use grid search, set a positive integer as the
number of values the app tries for each numeric hyperparameter.
the app ignores this value for categorical hyperparameters. the
default value is 10 . |
minimum classification error plot
after specifying which model hyperparameters to optimize and setting any additional optimization options (optional), train your optimizable model. on the classification learner tab, in the train section, click train all and select train selected. the app creates a minimum classification error plot that it updates as the optimization runs.
the minimum classification error plot displays the following information:
estimated minimum classification error – each light blue point corresponds to an estimate of the minimum classification error computed by the optimization process when considering all the sets of hyperparameter values tried so far, including the current iteration.
the estimate is based on an upper confidence interval of the current classification error objective model, as mentioned in the bestpoint hyperparameters description.
if you use grid search or random search to perform hyperparameter optimization, the app does not display these light blue points.
observed minimum classification error – each dark blue point corresponds to the observed minimum classification error computed so far by the optimization process. for example, at the third iteration, the dark blue point corresponds to the minimum of the classification error observed in the first, second, and third iterations.
bestpoint hyperparameters – the red square indicates the iteration that corresponds to the optimized hyperparameters. you can find the values of the optimized hyperparameters listed in the upper right of the plot under optimization results.
the optimized hyperparameters do not always provide the observed minimum classification error. when the app performs hyperparameter tuning by using bayesian optimization (see for a brief introduction), it chooses the set of hyperparameter values that minimizes an upper confidence interval of the classification error objective model, rather than the set that minimizes the classification error. for more information, see the
"criterion","min-visited-upper-confidence-interval"
name-value argument ofbestpoint
.minimum error hyperparameters – the yellow point indicates the iteration that corresponds to the hyperparameters that yield the observed minimum classification error.
for more information, see the
"criterion","min-observed"
name-value argument ofbestpoint
.if you use grid search to perform hyperparameter optimization, the bestpoint hyperparameters and the minimum error hyperparameters are the same.
missing points in the plot correspond to nan
minimum
classification error values.
optimization results
when the app finishes tuning model hyperparameters, it returns a model trained with the optimized hyperparameter values (bestpoint hyperparameters). the model metrics, displayed plots, and exported model correspond to this trained model with fixed hyperparameter values.
to inspect the optimization results of a trained optimizable model, select the model in the models pane and look at the model summary tab.
the model summary tab includes these sections:
training results – shows the performance of the optimizable model. see .
model hyperparameters – displays the type of optimizable model and lists any fixed hyperparameter values
optimized hyperparameters – lists the values of the optimized hyperparameters
hyperparameter search range – displays the search ranges for the optimized hyperparameters
optimizer – shows the selected optimizer options
when you perform hyperparameter tuning using bayesian optimization and you export
the resulting trained optimizable model to the workspace as a structure, the
structure includes a bayesianoptimization
object in the
hyperparameteroptimizationresult
field. the object contains
the results of the optimization performed in the app.
when you generate matlab® code from a trained optimizable model, the generated code uses the fixed and optimized hyperparameter values of the model to train on new data. the generated code does not include the optimization process. for information on how to perform bayesian optimization when you use a fit function, see bayesian optimization using a fit function.