Is it possible to create an optimizableVariable of integer type as an array with step different from 1?

Hello everyone!
I'm trying to perform a Bayesian optimization to find the best hyperparameters for a deep neural network and I want the variable MiniBatchSize to just have, in this case, three possible values: 8, 10, 12. I thought I could declare optVar as if it was an array
interval = 8:2:12;
optVar = optimizableVariable('MiniBatchSize', interval, 'Type', 'integer');
This code raises the following error:
Error using optimizableVariable/checkRange
'Range' value must be a string array, a cell array of character vectors, or a numeric vector of length 2.
Am I missing something? Is there another way to perform this?
Thanks in advance.

 Accepted Answer

Suppose X is an integer variable, that takes on only the values 1:5.
But what you want are non-integer levels of some parameter. For example, perhaps you want levels at [1.5, 3.5, 5.5, 7.5, 9.5]?
Inside your code, just write
Xhat = 2*X - 0.5;
Do you agree that Xhat takes on the desired levels?
They need not even be at all regular. Suppose you want levels at [2 3 5 7 11]? Inside your code, you might do this:
levels = [2 3 5 7 11];
Xhat = levels(X);
Clearly I could have chosen ANY levels there.
In your specific case, X is an integer variable that takes on the values 1:3. But you want it to take on the values [8 10 12]. Again, inside your code, write this:
Xhat = X*2 + 6;
Do you see that Xhat takes on only the values [8, 10, 12]?

2 Comments

Yes, I see it. I just thought it was not possible to modify the scope of the optimizableVariable inside my code. Let's see if it works. Thank you very much.
It is your code. It can do anything you want, as long as you return an objective.

Sign in to comment.

More Answers (1)

Optimization variable 1 to 3 and multiply by 2 and add 6.

3 Comments

Thanks but I don't get your point. What do you mean? Could you please write the piece of code to do that?
Ah, sorry, I didn't realize that the Stats toolbox had its own kind of variable creation; the instructions I gave were for Problem-Based Optimization
After rereading again your answer I've finally got your point and I think it might actually work. Thank you very much.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!