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

Illustration
uttamingh - 2021-04-13T11:00:00+00:00
Question: 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?

Expert Answer

Profile picture of John Williams John Williams answered . 2025-11-20

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]?
 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!