optimizableVariable
Variable description for bayesopt
or other
optimizers
Description
Create variables for optimizers.
Creation
Syntax
Description
variable = optimizableVariable(
creates a variable with the specified name and range of values.Name
,Range
)
variable = optimizableVariable(
sets properties using
name-value arguments. For example, Name
,Range
,Name,Value)optimizableVariable('xvar',[1
1000],'Type','integer')
creates an integer variable from 1 to
1000. You can specify multiple name-value arguments. Enclose each property name
in quotes.
Properties
Name
— Variable name
character vector | string scalar
Variable name, specified as a character vector or string scalar. The name must be unique, meaning different from those of other variables in the optimization.
Note
There are two names associated with an
optimizableVariable
:The MATLAB® workspace variable name
The name of the variable in the optimization
For example,
xvar = optimizableVariable('spacevar',[1,100]);
xvar
is the MATLAB workspace variable, and'spacevar'
is the variable in the optimization.Use these names as follows:
Use
xvar
as an element in the vector of variables you pass tobayesopt
. For example,results = bayesopt(fun,[xvar,tvar])
Use
'spacevar'
as the name of the variable in the optimization. For example, in an objective function,function objective = mysvmfun(x,cdata,grp) SVMModel = fitcsvm(cdata,grp,'KernelFunction','rbf',... 'BoxConstraint',x.spacevar,... 'KernelScale',x.tvar); objective = kfoldLoss(crossval(SVMModel));
Example: 'X1'
Data Types: char
| string
Range
— Variable range
2-element increasing real vector | string array or cell array of names of categorical variables
Variable range, specified as a 2-element finite increasing real vector, or as a string array or cell array of names of categorical variables:
For real or integer variables,
Range
gives the lower bound and upper bound of that variable.For categorical variables,
Range
gives the possible values.
Example: [-10,1]
Example: {'red','blue','black'}
Data Types: double
| string
| cell
Type
— Variable type
'real'
(default) | 'integer'
| 'categorical'
Variable type, specified as 'real'
(real variable),
'integer'
(integer variable), or
'categorical'
(categorical variable).
Note
The MATLAB data type of both 'real'
and
'integer'
variables is the standard
double-precision floating point number. The data type of
'categorical'
variables is categorical. So, for
example, to read a value of a categorical variable named
'colorv'
in a table of variables named
x
, use the command
char(x.colorv)
. For an example, see the objective
function in Custom Output Functions.
Example: 'Type','categorical'
Transform
— Transform applied to variable
'none'
(default) | 'log'
Transform applied to the variable, specified as 'none'
(no transform) or 'log'
(logarithmic transform).
For 'log'
, the variable must be a positive real
variable ('Type','real'
) or a nonnegative integer
variable ('Type','integer'
). The software searches and
models the variable on a log scale.
Example: 'Transform','log'
Optimize
— Indication to use variable in optimization
true
(default) | false
Indication to use variable in optimization, specified as
true
(use the variable) or false
(do not use the variable).
Example: 'Optimize',false
Data Types: logical
Note
You can use dot notation to change the following properties after creation.
Range
of real or integer variables. For example,xvar = optimizableVariable('x',[-10,10]); % Modify the range: xvar.Range = [1,5];
Type
between'integer'
and'real'
. For example,xvar.Type = 'integer';
Transform
of real or integer variables between'log'
and'none'
. For example,xvar.Transform = 'log';
You can use this flexibility, for example, to tweak an optimization that you want
to continue. Update the range or transform using dot notation and then call
resume
.
Object Functions
bayesopt | Select optimal machine learning hyperparameters using Bayesian optimization |
Examples
Variables for Optimization Examples
Real variable from 0 to 1:
var1 = optimizableVariable('xvar',[0 1])
var1 = optimizableVariable with properties: Name: 'xvar' Range: [0 1] Type: 'real' Transform: 'none' Optimize: 1
Integer variable from 0 to 1000 on a log scale:
var2 = optimizableVariable('ivar',[0 1000],'Type','integer','Transform','log')
var2 = optimizableVariable with properties: Name: 'ivar' Range: [0 1000] Type: 'integer' Transform: 'log' Optimize: 1
Categorical variable of rainbow colors:
var3 = optimizableVariable('rvar',{'r' 'o' 'y' 'g' 'b' 'i' 'v'},'Type','categorical')
var3 = optimizableVariable with properties: Name: 'rvar' Range: {'r' 'o' 'y' 'g' 'b' 'i' 'v'} Type: 'categorical' Transform: 'none' Optimize: 1
Version History
Introduced in R2016b
Open Example
You have a modified version of this example. Do you want to open this example with your edits?
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)