bayesian optimization workflow
what is bayesian optimization?
optimization, in its most general form, is the process of locating a point that minimizes a real-valued function called the objective function. bayesian optimization is the name of one such process. bayesian optimization internally maintains a gaussian process model of the objective function, and uses objective function evaluations to train the model. one innovation in bayesian optimization is the use of an acquisition function, which the algorithm uses to determine the next point to evaluate. the acquisition function can balance sampling at points that have low modeled objective functions, and exploring areas that have not yet been modeled well. for details, see bayesian optimization algorithm.
bayesian optimization is part of statistics and machine learning toolbox™ because it is well-suited to optimizing hyperparameters of classification and regression algorithms. a hyperparameter is an internal parameter of a classifier or regression function, such as the box constraint of a support vector machine, or the learning rate of a robust classification ensemble. these parameters can strongly affect the performance of a classifier or regressor, and yet it is typically difficult or time-consuming to optimize them. see bayesian optimization characteristics.
typically, optimizing the hyperparameters means that you try to minimize the cross-validation loss of a classifier or regression.
ways to perform bayesian optimization
you can perform a bayesian optimization in several ways:
fitcauto
and — pass predictor and response data to thefitcauto
orfitrauto
function to optimize across a selection of model types and hyperparameter values. unlike other approaches, usingfitcauto
orfitrauto
does not require you to specify a single model before the optimization; model selection is part of the optimization process. the optimization minimizes cross-validation loss, which is modeled using a multi-treebagger
model infitcauto
and a multi-regressiongp
model infitrauto
, rather than a single gaussian process regression model as used in other approaches. see bayesian optimization forfitcauto
and forfitrauto
.classification learner and regression learner apps — choose optimizable models in the machine learning apps and automatically tune their hyperparameter values by using bayesian optimization. the optimization minimizes the model loss based on the selected validation options. this approach has fewer tuning options than using a fit function, but allows you to perform bayesian optimization directly in the apps. see hyperparameter optimization in classification learner app and .
fit function — include the
optimizehyperparameters
name-value argument in many fitting functions to apply bayesian optimization automatically. the optimization minimizes cross-validation loss. this approach gives you fewer tuning options than usingbayesopt
, but enables you to perform bayesian optimization more easily. see bayesian optimization using a fit function.— exert the most control over your optimization by calling
bayesopt
directly. this approach requires you to write an objective function, which does not have to represent cross-validation loss. see bayesian optimization using bayesopt.
bayesian optimization using a fit function
to minimize the error in a cross-validated response via bayesian optimization, follow these steps.
choose your classification or regression solver among the fit functions that accept the
optimizehyperparameters
name-value argument.classification fit functions:
fitcdiscr
,fitcecoc
,fitcensemble
, , ,fitcknn
,fitclinear
,fitcnb
,fitcnet
,fitcsvm
,fitctree
regression fit functions: , ,
fitrgp
, ,fitrlinear
, ,fitrsvm
,fitrtree
decide on the hyperparameters to optimize, and pass them in the
optimizehyperparameters
name-value argument. for each fit function, you can choose from a set of hyperparameters. see eligible hyperparameters for fit functions, or use the function, or consult the fit function reference page.you can pass a cell array of parameter names. you can also set
'auto'
as theoptimizehyperparameters
value, which chooses a typical set of hyperparameters to optimize, or'all'
to optimize all available parameters.for ensemble fit functions
fitcecoc
,fitcensemble
, andfitrensemble
, also include parameters of the weak learners in theoptimizehyperparameters
cell array.optionally, create an options structure for the
hyperparameteroptimizationoptions
name-value argument. see hyperparameter optimization options for fit functions.call the fit function with the appropriate name-value arguments.
for examples, see and . also, every fit function reference page contains a bayesian optimization example.
bayesian optimization using bayesopt
to perform a bayesian optimization using , follow these steps.
prepare your variables. see .
create your objective function. see . if necessary, create constraints, too. see . to include extra parameters in an objective function, see .
decide on options, meaning the
bayseopt
pairs. you are not required to pass any options to but you typically do, especially when trying to improve a solution.call .
examine the solution. you can decide to resume the optimization by using , or restart the optimization, usually with modified options.
for an example, see .
bayesian optimization characteristics
bayesian optimization algorithms are best suited to these problem types.
characteristic | details |
---|---|
low dimension | bayesian optimization works best in a low number of dimensions, typically 10 or fewer. while bayesian optimization can solve some problems with a few dozen variables, it is not recommended for dimensions higher than about 50. |
expensive objective | bayesian optimization is designed for objective functions that are slow to evaluate. it has considerable overhead, typically several seconds for each iteration. |
low accuracy | bayesian optimization does not necessarily give very
accurate results. if you have a deterministic objective function,
you can sometimes improve the accuracy by starting a standard optimization
algorithm from the |
global solution | bayesian optimization is a global technique. unlike many other algorithms, to search for a global solution you do not have to start the algorithm from various initial points. |
hyperparameters | bayesian optimization is well-suited to optimizing hyperparameters of
another function. a hyperparameter is a parameter that controls the
behavior of a function. for example, the |
parameters available for fit functions
eligible hyperparameters for fit functions
hyperparameter optimization options for fit functions
when optimizing using a fit function, you have these options available in the
hyperparameteroptimizationoptions
name-value argument. give
the value as a structure. all fields in the structure are optional.
field name | values | default |
---|---|---|
optimizer |
| 'bayesopt' |
acquisitionfunctionname |
acquisition functions whose names include
| 'expected-improvement-per-second-plus' |
maxobjectiveevaluations | maximum number of objective function evaluations. | 30 for 'bayesopt' and
'randomsearch' , and the entire grid for
'gridsearch' |
maxtime | time limit, specified as a positive real scalar. the time limit is in seconds, as
measured by | inf |
numgriddivisions | for 'gridsearch' , the number of values in each dimension. the value can be
a vector of positive integers giving the number of
values for each dimension, or a scalar that
applies to all dimensions. this field is ignored
for categorical variables. | 10 |
showplots | logical value indicating whether to show plots. if true , this field plots
the best observed objective function value against the iteration number. if you
use bayesian optimization (optimizer is
'bayesopt' ), then this field also plots the best
estimated objective function value. the best observed objective function values
and best estimated objective function values correspond to the values in the
bestsofar (observed) and bestsofar
(estim.) columns of the iterative display, respectively. you can
find these values in the properties objectiveminimumtrace and estimatedobjectiveminimumtrace of
mdl.hyperparameteroptimizationresults . if the problem
includes one or two optimization parameters for bayesian optimization, then
showplots also plots a model of the objective function
against the parameters. | true |
saveintermediateresults | logical value indicating whether to save results when optimizer is
'bayesopt' . if
true , this field overwrites a
workspace variable named
'bayesoptresults' at each
iteration. the variable is a bayesianoptimization object. | false |
verbose | display at the command line:
for details, see the | 1 |
useparallel | logical value indicating whether to run bayesian optimization in parallel, which requires parallel computing toolbox™. due to the nonreproducibility of parallel timing, parallel bayesian optimization does not necessarily yield reproducible results. for details, see . | false |
repartition | logical value indicating whether to repartition the cross-validation at every
iteration. if this field is the setting
| false |
use no more than one of the following three options. | ||
cvpartition | a cvpartition object, as created by cvpartition | 'kfold',5 if you do not specify a cross-validation
field |
holdout | a scalar in the range (0,1) representing the holdout fraction | |
kfold | an integer greater than 1 |