error while using bayesopt

7 次查看(过去 30 天)
Mohamed
Mohamed 2023-9-26
I am trying to use bayesopt tools in matlab to find the optimal parameters acquisition_function = 'expected-improvement-plus';
this example was exaplined using python in
I tried to regenrate this in matlab
but have the error
Undefined function 'mtimes' for input arguments of type 'table'. Error in untitled12>@(x)sin(5*x)*(1-tanh(x^2))+noise_level*randn (line 7) f = @(x) sin(5 * x) * (1 - tanh(x^2
this is my code
% Set noise level
noise_level =0.1;
% Define function f
f = @(x) sin(5 * x) * (1 - tanh(x^2)) + noise_level * randn;
% Generate data for plotting f(x)
x = linspace(-2, 2, 400)';
fx = arrayfun(@(x) f(x), x);
% Plot f(x) + contours
figure; hold on;
plot(x, fx, 'r--', 'DisplayName', 'True (unknown)');
% Define the objective function
fun = @(x) f(x);
% Define the search space dimensions
dimensions = [optimizableVariable('x', [-2, 2])];
% Bayesian optimization settings
n_calls = 15;
n_initial_points = 5;
n_random_starts = n_initial_points; % Equivalent to n_random_starts
initial_point_generator = 'random';
acquisition_function = 'expected-improvement-plus';
x0 = []; % Provide initial points if needed
y0 = []; % Provide function evaluations at initial points if needed
random_state = 1234; % Set a specific random seed for reproducibility
verbose = true;
% Perform Bayesian optimization
rng(1234)
results = bayesopt(fun, dimensions, 'AcquisitionFunctionName', acquisition_function,...
'MaxObjectiveEvaluations', n_calls, ...
'IsObjectiveDeterministic', true,...
'PlotFcn', {@plotMinObjective,@plotConstraintModels}, ...
'Verbose', 1 );
% Get the optimized result
best_x = results.XAtMinObjective.x;
best_fval = results.MinObjective;
% Display the result
fprintf('Optimal x: %.4f, f(x): %.4f\n', best_x, best_fval);

回答(1 个)

檮杌
檮杌 2023-9-27
It is expected that you provide an objective function as the 'fun' argument. You are currently using an incorrect function for bayesopt. Additionally, the input objective function for bayesopt accepts an input as a table and outputs an objective value. This information is clearly stated in the documentation of bayesopt. To quote:
"fun accepts x, a 1-by-D table of variable values, and returns objective, a real scalar representing the objective function value fun(x)."
In other words, the way you are using bayesopt is not aligned with its intended usage. I recommend carefully reading the documentation of bayesopt to understand how the function argument should be utilized. It is crucial to provide an objective function for the function argument.

类别

Help CenterFile Exchange 中查找有关 Model Building and Assessment 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by