matlab 贝叶斯信息标准_贝叶斯优化之采集函数

通过之前的文章《当遗传算法遇到 Bayesian Optimization》我们已经了解贝叶斯优化的过程了。另一篇文章《当Bayesian Optimization遇到L-BFGS》中提到,L-BFGS的一大作用是优化acquisition function(采集函数),本文就来介绍一下采集函数到底是什么。

        以一个经典的黑箱函数——黄金储量预测为例:假设我们想在一个区域内开采黄金,首先需要探明哪个位置的黄金储量最大,由于钻探设备,人力都很昂贵,我们需要在最少的次数内探明最值得开采的地方。这就是一个典型的昂贵的黑箱函数优化:每次函数的计算都会十分地耗时,每打一次孔就相当于贝叶斯优化过程中去新加一个样本点并带回原函数计算,再利用新加的样本点让我们的预测模型更加精确,我们目标是在增加最少点的情况下找到储量最大的位置。当我们一无所知的时候,可以随机先钻三个孔并利用这三个孔点的储量信息建立一个初代的模型,如图1,纵坐标为黄金储量(请忽略具体数值和单位),横坐标代表位置。

86ad6855bdb0a4772a1818e61ffe19ff.png

图1  黄金储量预测

        那么问题来了,下一个孔钻在哪里?假设我们已有 图1中的黄金储量预测模型,在不知道真实储量分布时(True Gold Content),只从模型预测结果(Prediction)来看,在2附近与4到5之间发现高储量可能性较大,因

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用贝叶斯优化的示例函数Matlab代码: ```matlab function [xopt,fopt] = bayesopt_fun(f,x0,lb,ub,opts) % BAYESOPT_FUN: Bayesian optimization of a function % [XOPT,FOPT] = BAYESOPT_FUN(F,X0,LB,UB,OPTS) finds the minimum of a % function F using Bayesian optimization. X0 is the initial guess, % LB and UB are the lower and upper bounds of the variables, and OPTS % is an options structure created using BAYESOPT_OPTIONS. The function % F should take a vector of variables as input and return a scalar % output. % % Example usage: % f = @(x) sin(3*x) + x.^2 - 0.7*x; % opts = bayesopt_options('AcquisitionFunctionName','expected-improvement-plus'); % [xopt,fopt] = bayesopt_fun(f,0,0,1,opts); % % See also BAYESOPT_OPTIONS. % Check inputs narginchk(4,5); if nargin < 5, opts = bayesopt_options(); end assert(isa(f,'function_handle'),'F must be a function handle'); assert(isvector(x0) && isnumeric(x0),'X0 must be a numeric vector'); assert(isvector(lb) && isnumeric(lb),'LB must be a numeric vector'); assert(isvector(ub) && isnumeric(ub),'UB must be a numeric vector'); assert(all(size(x0)==size(lb)) && all(size(x0)==size(ub)), ... 'X0, LB, and UB must have the same size'); opts = bayesopt_options(opts); % ensure opts has all fields % Initialize X = x0(:); % column vector Y = f(X); n = numel(X); Xbest = X; Ybest = Y; fmin = min(Y); fmax = max(Y); % Loop over iterations for i = 1:opts.MaxIterations % Train surrogate model model = fitrgp(X,Y,'Basis','linear','FitMethod','exact', ... 'PredictMethod','exact','Standardize',true, ... 'KernelFunction',opts.KernelFunction,'KernelParameters',opts.KernelParameters); % Find next point to evaluate if strcmp(opts.AcquisitionFunctionName,'expected-improvement-plus') % Use expected improvement with small positive improvement threshold impThreshold = 0.01*(fmax-fmin); acqFcn = @(x) expected_improvement_plus(x,model,fmin,impThreshold); else % Use acquisition function specified in options acqFcn = str2func(opts.AcquisitionFunctionName); end xnext = bayesopt_acq(acqFcn,model,lb,ub,opts.AcquisitionSamples); % Evaluate function at next point ynext = f(xnext); % Update data X = [X; xnext(:)]; Y = [Y; ynext]; if ynext < Ybest Xbest = xnext; Ybest = ynext; end fmin = min(Y); fmax = max(Y); % Check stopping criterion if i >= opts.MaxIterations || (i > 1 && abs(Y(end)-Y(end-1))/Ybest <= opts.TolFun) break; end end % Return best point found xopt = Xbest; fopt = Ybest; end function EI = expected_improvement_plus(X,model,fmin,impThreshold) % EXPECTED_IMPROVEMENT_PLUS: Expected improvement with small positive improvement threshold % EI = EXPECTED_IMPROVEMENT_PLUS(X,MODEL,FMIN,IMPTHRESHOLD) computes % the expected improvement (EI) of a surrogate model at the point X. % The input MODEL is a regression model, FMIN is the current minimum % value of the function being modeled, and IMPTHRESHOLD is a small % positive improvement threshold. % % The expected improvement is defined as: % EI = E[max(FMIN - Y, 0)] % where Y is the predicted value of the surrogate model at X. % The expected value is taken over the posterior distribution of Y. % % However, if the predicted value Y is within IMPTHRESHOLD of FMIN, % then EI is set to IMPTHRESHOLD instead. This is done to encourage % exploration of the search space, even if the expected improvement % is very small. % % See also BAYESOPT_ACQ. % Check inputs narginchk(4,4); % Compute predicted value and variance at X [Y,~,sigma] = predict(model,X); % Compute expected improvement z = (fmin - Y - impThreshold)/sigma; EI = (fmin - Y - impThreshold)*normcdf(z) + sigma*normpdf(z); EI(sigma==0) = 0; % avoid division by zero % Check if improvement is small if Y >= fmin - impThreshold EI = impThreshold; end end function opts = bayesopt_options(varargin) % BAYESOPT_OPTIONS: Create options structure for Bayesian optimization % OPTS = BAYESOPT_OPTIONS() creates an options structure with default % values for all parameters. % % OPTS = BAYESOPT_OPTIONS(P1,V1,P2,V2,...) creates an options structure % with parameter names and values specified in pairs. Any unspecified % parameters will take on their default values. % % OPTS = BAYESOPT_OPTIONS(OLDOPTS,P1,V1,P2,V2,...) creates a copy of % the OLDOPTS structure, with any parameters specified in pairs % overwriting the corresponding values. % % Available parameters: % MaxIterations - Maximum number of iterations (default 100) % TolFun - Tolerance on function value improvement (default 1e-6) % KernelFunction - Name of kernel function for Gaussian process % regression (default 'squaredexponential') % KernelParameters - Parameters of kernel function (default []) % AcquisitionFunctionName - Name of acquisition function for deciding % which point to evaluate next (default % 'expected-improvement-plus') % AcquisitionSamples - Number of samples to use when evaluating the % acquisition function (default 1000) % % See also BAYESOPT_FUN, BAYESOPT_ACQ. % Define default options opts = struct('MaxIterations',100,'TolFun',1e-6, ... 'KernelFunction','squaredexponential','KernelParameters',[], ... 'AcquisitionFunctionName','expected-improvement-plus','AcquisitionSamples',1000); % Overwrite default options with user-specified options if nargin > 0 if isstruct(varargin{1}) % Copy old options structure and overwrite fields with new values oldopts = varargin{1}; for i = 2:2:nargin fieldname = validatestring(varargin{i},fieldnames(opts)); oldopts.(fieldname) = varargin{i+1}; end opts = oldopts; else % Overwrite fields of default options with new values for i = 1:2:nargin fieldname = validatestring(varargin{i},fieldnames(opts)); opts.(fieldname) = varargin{i+1}; end end end end function xnext = bayesopt_acq(acqFcn,model,lb,ub,nSamples) % BAYESOPT_ACQ: Find next point to evaluate using an acquisition function % XNEXT = BAYESOPT_ACQ(ACQFCN,MODEL,LB,UB,NSAMPLES) finds the next point % to evaluate using the acquisition function ACQFCN and the regression % model MODEL. LB and UB are the lower and upper bounds of the variables, % and NSAMPLES is the number of random samples to use when maximizing % the acquisition function. % % The input ACQFCN should be a function handle that takes a regression % model and a set of input points as inputs, and returns a vector of % acquisition function values. The set of input points is a matrix with % one row per point and one column per variable. % % The output XNEXT is a vector containing the next point to evaluate. % % See also BAYESOPT_FUN, EXPECTED_IMPROVEMENT_PLUS. % Check inputs narginchk(4,5); assert(isa(acqFcn,'function_handle'),'ACQFCN must be a function handle'); assert(isa(model,'RegressionGP'),'MODEL must be a regressionGP object'); assert(isvector(lb) && isnumeric(lb),'LB must be a numeric vector'); assert(isvector(ub) && isnumeric(ub),'UB must be a numeric vector'); assert(all(size(lb)==size(ub)),'LB and UB must have the same size'); if nargin < 5, nSamples = 1000; end % Generate random samples X = bsxfun(@plus,lb,bsxfun(@times,rand(nSamples,numel(lb)),ub-lb)); % Evaluate acquisition function acq = acqFcn(model,X); % Find maximum of acquisition function [~,imax] = max(acq); xnext = X(imax,:); end ``` 该示例代码实现了一个使用贝叶斯优化函数优化器。该优化器使用高斯过程回归模型来近似目标函数,并使用期望改进加上(EI+)作为获取函数。您可以将此代码用作自己的优化问题的起点,并根据需要进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值