error while using bayesopt

7 Ansichten (letzte 30 Tage)
Mohamed
Mohamed am 26 Sep. 2023
Bearbeitet: Walter Roberson am 27 Sep. 2023
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);

Antworten (1)

檮杌
檮杌 am 27 Sep. 2023
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.

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by