bayesopt
试图最小化目标函数。如果在stead, you want to maximize a function, set the objective function to the negative of the function you want to maximize. SeeMaximizing Functions. To include extra parameters in an objective function, seeParameterizing Functions.
bayesopt
passes a table of variables to the objective function. The variables have the names and types that you declare; seeVariables for a Bayesian Optimization.
The objective function has the following signature:
[objective,coupledconstraints,userdata] = fun(x)
objective
— The objective function value atx
, a numeric scalar.bayesopt
returns an error if the objective function returns a nonnumeric value or a matrix with more than one entry.
coupledconstraints
— Value of coupled constraints, if any (optional output), a vector of real values. A negative value indicates that a constraint is satisfied, a positive value indicates that it is not satisfied. For details, seeCoupled Constraints.
userdata
— Optional data that your function can return for further uses, such as plotting or logging (optional output). For an example, seeBayesian Optimization Plot Functions.
This objective function returns the loss in a cross-validated fit of an SVM model with parametersbox
andsigma
. The objective also returns a coupled constraint function that is positive (infeasible) when the number of support vectors exceeds 100 (100 is feasible, 101 is not).
function[objective,constraint] = mysvmfun(x,cdata,grp) SVMModel = fitcsvm(cdata,grp,'KernelFunction','rbf',...'BoxConstraint',x.box,...'KernelScale',x.sigma); objective = kfoldLoss(crossval(SVMModel)); constraint = sum(SVMModel.SupportVectors) - 100.5;
To use the objective function, assuming thatcdata
andgrp
exist in the workspace, create an anonymous function that incorporates the data, as described inParameterizing Functions.
fun = @(x)mysvmfun(x,cdata,grp); results = bayesopt(fun,vars)% Assumes vars exists
bayesopt
deems your objective function to return an error when the objective function returns anything other than a finite real scalar. For example, if your objective function returns a complex value,NaN
, orInf
, thenbayesopt
deems that your objective function errors. Ifbayesopt
encounters an error, it continues to optimize, and automatically updates a Bayesian model of points that lead to errors. This Bayesian model is theError model.bayesopt
incorporates the Error model as a coupled constraint. SeeCoupled Constraints.
When errors exist, you can plot the Error model by setting thebayesopt
PlotFcn
name-value argument@plotConstraintModels
. Or you can retrospectively callplot
on the results of a Bayesian optimization, and include@plotConstraintModels
.