BayesianOptimization

Bayesian optimization results

Description

A BayesianOptimization object contains the results of a Bayesian optimization. It is the output of bayesopt or a fit function that accepts the OptimizeHyperparameters name-value pair such as fitcdiscr. In addition, a BayesianOptimization object contains data for each iteration of bayesopt that can be accessed by a plot function or an output function.

Creation

Create a BayesianOptimization object using the bayesopt function or a fit function with the OptimizeHyperparameters name-value pair.

Properties

expand all

Problem Definition Properties

This property is read-only.

ObjectiveFcn argument used by bayesopt, returned as a function handle.

  • If you call bayesopt directly, ObjectiveFcn is the bayesopt objective function argument.

  • If you call a fit function containing the 'OptimizeHyperparameters' name-value pair argument, ObjectiveFcn is a function handle that returns the misclassification rate for classification or returns the logarithm of one plus the cross-validation loss for regression, measured by five-fold cross-validation.

Data Types: function_handle

This property is read-only.

VariableDescriptions argument that bayesopt used, returned as a vector of optimizableVariable objects.

  • If you called bayesopt directly, VariableDescriptions is the bayesopt variable description argument.

  • If you called a fit function with the OptimizeHyperparameters name-value pair, VariableDescriptions is the vector of hyperparameters.

This property is read-only.

Options that bayesopt used, returned as a structure.

  • If you called bayesopt directly, Options is the options used in bayesopt, which are the name-value pairs See bayesopt Input Arguments.

  • If you called a fit function with the OptimizeHyperparameters name-value pair, Options are the default bayesopt options, modified by the HyperparameterOptimizationOptions name-value pair.

Options is a read-only structure containing the following fields.

Option NameMeaning
AcquisitionFunctionNameAcquisition function name. See Acquisition Function Types.
IsObjectiveDeterministictrue means the objective function is deterministic, false otherwise.
ExplorationRatioUsed only when AcquisitionFunctionName is 'expected-improvement-plus' or 'expected-improvement-per-second-plus'. See Plus.
  
MaxObjectiveEvaluationsObjective function evaluation limit.
MaxTimeTime limit.
  
XConstraintFcnDeterministic constraints on variables. See Deterministic Constraints — XConstraintFcn.
ConditionalVariableFcnConditional variable constraints. See Conditional Constraints — ConditionalVariableFcn.
NumCoupledConstraintsNumber of coupled constraints. See Coupled Constraints.
CoupledConstraintTolerancesCoupled constraint tolerances. See Coupled Constraints.
AreCoupledConstraintsDeterministicLogical vector specifying whether each coupled constraint is deterministic.
  
VerboseCommand-line display level.
OutputFcnFunction called after each iteration. See Bayesian Optimization Output Functions.
SaveVariableNameVariable name for the @assignInBase output function.
SaveFileNameFile name for the @saveToFile output function.
PlotFcnPlot function called after each iteration. See Bayesian Optimization Plot Functions
  
InitialXPoints where bayesopt evaluated the objective function.
InitialObjectiveObjective function values at InitialX.
InitialConstraintViolationsCoupled constraint function values at InitialX.
InitialErrorValuesError values at InitialX.
InitialObjectiveEvaluationTimesObjective function evaluation times at InitialX.
InitialIterationTimesTime for each iteration, including objective function evaluation and other computations.

Data Types: struct

Solution Properties

This property is read-only.

Minimum observed value of objective function, returned as a real scalar. When there are coupled constraints or evaluation errors, this value is the minimum over all observed points that are feasible according to the final constraint and Error models.

Data Types: double

This property is read-only.

Observed point with minimum objective function value, returned as a 1-by-D table, where D is the number of variables.

Data Types: table

This property is read-only.

Minimum estimated value of objective function, returned as a real scalar. MinEstimatedObjective uses the final objective model.

MinEstimatedObjective is the same as the CriterionValue result of bestPoint with default criterion.

Data Types: double

This property is read-only.

Point with minimum estimated objective function value, returned as a 1-by-D table, where D is the number of variables. XAtMinEstimatedObjective uses the final objective model.

Data Types: table

This property is read-only.

Number of objective function evaluations, returned as a positive integer. This includes the initial evaluations to form a posterior model as well as evaluation during the optimization iterations.

Data Types: double

This property is read-only.

Total elapsed time of optimization in seconds, returned as a positive scalar.

Data Types: double

This property is read-only.

Next point to evaluate if optimization continues, returned as a 1-by-D table, where D is the number of variables.

Data Types: table

Trace Properties

This property is read-only.

Points where the objective function was evaluated, returned as a T-by-D table, where T is the number of evaluation points and D is the number of variables.

Data Types: table

This property is read-only.

Objective function values, returned as a column vector of length T, where T is the number of evaluation points. ObjectiveTrace contains the history of objective function evaluations.

Data Types: double

This property is read-only.

Objective function evaluation times, returned as a column vector of length T, where T is the number of evaluation points. ObjectiveEvaluationTimeTrace includes the time in evaluating coupled constraints, because the objective function computes these constraints.

Data Types: double

This property is read-only.

Iteration times, returned as a column vector of length T, where T is the number of evaluation points. IterationTimeTrace includes both objective function evaluation time and other overhead.

Data Types: double

This property is read-only.

Coupled constraint values, returned as a T-by-K array, where T is the number of evaluation points and K is the number of coupled constraints.

Data Types: double

This property is read-only.

Error indications, returned as a column vector of length T of -1 or 1 entries, where T is the number of evaluation points. Each 1 entry indicates that the objective function errored or returned NaN on the corresponding point in XTrace. Each -1 entry indicates that the objective function value was computed.

Data Types: double

This property is read-only.

Feasibility indications, returned as a logical column vector of length T, where T is the number of evaluation points. Each 1 entry indicates that the final constraint model predicts feasibility at the corresponding point in XTrace.

Data Types: logical

This property is read-only.

Probability that evaluation point is feasible, returned as a column vector of length T, where T is the number of evaluation points. The probabilities come from the final constraint model, including the error constraint model, on the corresponding points in XTrace.

Data Types: double

This property is read-only.

Which evaluation gave minimum feasible objective, returned as a column vector of integer indices of length T, where T is the number of evaluation points. Feasibility is determined with respect to the constraint models that existed at each iteration, including the error constraint model.

Data Types: double

This property is read-only.

Minimum observed objective, returned as a column vector of integer indices of length T, where T is the number of evaluation points.

Data Types: double

This property is read-only.

Minimum estimated objective, returned as a column vector of integer indices of length T, where T is the number of evaluation points. The estimated objective at each iteration is determined with respect to the objective model that existed at that iteration.

Data Types: double

This property is read-only.

Auxiliary data from the objective function, returned as a cell array of length T, where T is the number of evaluation points. Each entry in the cell array is the UserData returned in the third output of the objective function.

Data Types: cell

Object Functions

bestPointBest point in a Bayesian optimization according to a criterion
plotPlot Bayesian optimization results
predictConstraintsPredict coupled constraint violations at a set of points
predictErrorPredict error value at a set of points
predictObjectivePredict objective function at a set of points
predictObjectiveEvaluationTimePredict objective function run times at a set of points
resumeResume a Bayesian optimization

Examples

collapse all

This example shows how to create a BayesianOptimization object by using bayesopt to minimize cross-validation loss.

Optimize hyperparameters of a KNN classifier for the ionosphere data, that is, find KNN hyperparameters that minimize the cross-validation loss. Have bayesopt minimize over the following hyperparameters:

  • Nearest-neighborhood sizes from 1 to 30

  • Distance functions 'chebychev', 'euclidean', and 'minkowski'.

For reproducibility, set the random seed, set the partition, and set the AcquisitionFunctionName option to 'expected-improvement-plus'. To suppress iterative display, set 'Verbose' to 0. Pass the partition c and fitting data X and Y to the objective function fun by creating fun as an anonymous function that incorporates this data. See Parameterizing Functions.

load ionosphere
rng default
num = optimizableVariable('n',[1,30],'Type','integer');
dst = optimizableVariable('dst',{'chebychev','euclidean','minkowski'},'Type','categorical');
c = cvpartition(351,'Kfold',5);
fun = @(x)kfoldLoss(fitcknn(X,Y,'CVPartition',c,'NumNeighbors',x.n,...
    'Distance',char(x.dst),'NSMethod','exhaustive'));
results = bayesopt(fun,[num,dst],'Verbose',0,...
    'AcquisitionFunctionName','expected-improvement-plus')

results = 
  BayesianOptimization with properties:

                      ObjectiveFcn: [function_handle]
              VariableDescriptions: [1x2 optimizableVariable]
                           Options: [1x1 struct]
                      MinObjective: 0.1197
                   XAtMinObjective: [1x2 table]
             MinEstimatedObjective: 0.1213
          XAtMinEstimatedObjective: [1x2 table]
           NumObjectiveEvaluations: 30
                  TotalElapsedTime: 49.9819
                         NextPoint: [1x2 table]
                            XTrace: [30x2 table]
                    ObjectiveTrace: [30x1 double]
                  ConstraintsTrace: []
                     UserDataTrace: {30x1 cell}
      ObjectiveEvaluationTimeTrace: [30x1 double]
                IterationTimeTrace: [30x1 double]
                        ErrorTrace: [30x1 double]
                  FeasibilityTrace: [30x1 logical]
       FeasibilityProbabilityTrace: [30x1 double]
               IndexOfMinimumTrace: [30x1 double]
             ObjectiveMinimumTrace: [30x1 double]
    EstimatedObjectiveMinimumTrace: [30x1 double]

This example shows how to minimize the cross-validation loss in the ionosphere data using Bayesian optimization of an SVM classifier.

Load the data.

load ionosphere

Optimize the classification using the 'auto' parameters.

rng default % For reproducibility
Mdl = fitcsvm(X,Y,'OptimizeHyperparameters','auto')
|=====================================================================================================|
| Iter | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   | BoxConstraint|  KernelScale |
|      | result |             | runtime     | (observed)  | (estim.)    |              |              |
|=====================================================================================================|
|    1 | Best   |     0.21652 |      17.676 |     0.21652 |     0.21652 |       64.836 |    0.0015729 |
|    2 | Accept |     0.35897 |    0.074952 |     0.21652 |     0.22539 |     0.036335 |       5.5755 |
|    3 | Best   |     0.13105 |       6.686 |     0.13105 |     0.14152 |    0.0022147 |    0.0023957 |
|    4 | Accept |     0.35897 |    0.077766 |     0.13105 |     0.13108 |       5.1259 |        98.62 |
|    5 | Best   |     0.12251 |     0.08597 |     0.12251 |     0.12253 |    0.0010264 |     0.042908 |
|    6 | Accept |     0.12536 |      0.1123 |     0.12251 |     0.12242 |    0.0010276 |      0.01796 |
|    7 | Accept |     0.13105 |      1.3562 |     0.12251 |     0.12363 |      0.04548 |     0.028371 |
|    8 | Accept |     0.12821 |     0.10173 |     0.12251 |      0.1249 |    0.0010072 |     0.030413 |
|    9 | Accept |      0.1339 |    0.075746 |     0.12251 |     0.12488 |    0.0010192 |     0.074576 |
|   10 | Accept |     0.12536 |     0.14206 |     0.12251 |     0.12493 |    0.0010018 |     0.012976 |
|   11 | Accept |     0.12821 |    0.090896 |     0.12251 |     0.12596 |     0.001015 |     0.031011 |
|   12 | Accept |     0.13105 |     0.19583 |     0.12251 |     0.12568 |    0.0010129 |    0.0096616 |
|   13 | Accept |     0.12251 |     0.11303 |     0.12251 |     0.12429 |    0.0016169 |     0.030272 |
|   14 | Accept |     0.12251 |     0.12059 |     0.12251 |     0.12397 |     0.003059 |     0.028401 |
|   15 | Accept |     0.12821 |      0.1388 |     0.12251 |      0.1249 |    0.0037274 |     0.026596 |
|   16 | Accept |     0.35897 |    0.076167 |     0.12251 |     0.12487 |        1.745 |       989.98 |
|   17 | Accept |     0.35897 |    0.087111 |     0.12251 |     0.12491 |    0.0090574 |       94.849 |
|   18 | Accept |     0.34758 |    0.083236 |     0.12251 |     0.12524 |     0.001448 |      0.66381 |
|   19 | Accept |     0.13105 |    0.080728 |     0.12251 |     0.12562 |     0.001042 |     0.045914 |
|   20 | Accept |     0.12536 |     0.15038 |     0.12251 |     0.12563 |    0.0010189 |     0.020835 |
|=====================================================================================================|
| Iter | Eval   | Objective   | Objective   | BestSoFar   | BestSoFar   | BoxConstraint|  KernelScale |
|      | result |             | runtime     | (observed)  | (estim.)    |              |              |
|=====================================================================================================|
|   21 | Accept |     0.12251 |     0.12811 |     0.12251 |     0.12468 |    0.0010139 |     0.023159 |
|   22 | Accept |     0.35897 |    0.075389 |     0.12251 |     0.12473 |     0.067964 |       23.612 |
|   23 | Accept |      0.1396 |    0.074715 |     0.12251 |     0.12449 |    0.0025917 |      0.19385 |
|   24 | Accept |     0.12251 |    0.078557 |     0.12251 |      0.1245 |    0.0042252 |      0.12741 |
|   25 | Accept |      0.1339 |    0.087403 |     0.12251 |      0.1244 |      0.01429 |       0.1308 |
|   26 | Accept |      0.1396 |    0.073631 |     0.12251 |     0.12452 |    0.0010084 |      0.13288 |
|   27 | Accept |     0.13675 |     0.54766 |     0.12251 |      0.1245 |    0.0010203 |    0.0050828 |
|   28 | Accept |     0.12536 |     0.12199 |     0.12251 |     0.12465 |    0.0010316 |     0.019592 |
|   29 | Accept |     0.35897 |    0.075367 |     0.12251 |     0.12464 |      0.65819 |       320.56 |
|   30 | Accept |     0.16524 |    0.073327 |     0.12251 |     0.12268 |     0.035233 |       1.9219 |

__________________________________________________________
Optimization completed.
MaxObjectiveEvaluations of 30 reached.
Total function evaluations: 30
Total elapsed time: 55.0716 seconds.
Total objective function evaluation time: 28.8614

Best observed feasible point:
    BoxConstraint    KernelScale
    _____________    ___________

      0.0010264       0.042908  

Observed objective function value = 0.12251
Estimated objective function value = 0.12743
Function evaluation time = 0.08597

Best estimated feasible point (according to models):
    BoxConstraint    KernelScale
    _____________    ___________

      0.0016169       0.030272  

Estimated objective function value = 0.12268
Estimated function evaluation time = 0.10537
Mdl = 
  ClassificationSVM
                         ResponseName: 'Y'
                CategoricalPredictors: []
                           ClassNames: {'b'  'g'}
                       ScoreTransform: 'none'
                      NumObservations: 351
    HyperparameterOptimizationResults: [1×1 BayesianOptimization]
                                Alpha: [98×1 double]
                                 Bias: -4.7988
                     KernelParameters: [1×1 struct]
                       BoxConstraints: [351×1 double]
                      ConvergenceInfo: [1×1 struct]
                      IsSupportVector: [351×1 logical]
                               Solver: 'SMO'


  Properties, Methods

The fit achieved about 12% loss for the default 5-fold cross validation.

Examine the BayesianOptimization object that is returned in the HyperparameterOptimizationResults property of the returned model.

disp(Mdl.HyperparameterOptimizationResults)
  BayesianOptimization with properties:

                      ObjectiveFcn: @createObjFcn/inMemoryObjFcn
              VariableDescriptions: [5×1 optimizableVariable]
                           Options: [1×1 struct]
                      MinObjective: 0.1225
                   XAtMinObjective: [1×2 table]
             MinEstimatedObjective: 0.1227
          XAtMinEstimatedObjective: [1×2 table]
           NumObjectiveEvaluations: 30
                  TotalElapsedTime: 55.0716
                         NextPoint: [1×2 table]
                            XTrace: [30×2 table]
                    ObjectiveTrace: [30×1 double]
                  ConstraintsTrace: []
                     UserDataTrace: {30×1 cell}
      ObjectiveEvaluationTimeTrace: [30×1 double]
                IterationTimeTrace: [30×1 double]
                        ErrorTrace: [30×1 double]
                  FeasibilityTrace: [30×1 logical]
       FeasibilityProbabilityTrace: [30×1 double]
               IndexOfMinimumTrace: [30×1 double]
             ObjectiveMinimumTrace: [30×1 double]
    EstimatedObjectiveMinimumTrace: [30×1 double]
Introduced in R2016b