Coder configurer for multiclass model using binary learners
A ClassificationECOCCoderConfigurer
object is a coder configurer
of a multiclass error-correcting output codes (ECOC) classification model (ClassificationECOC
or CompactClassificationECOC
) that uses support vector
machine (SVM) or linear binary learners.
A coder configurer offers convenient features to configure code generation options, generate C/C++ code, and update model parameters in the generated code.
Configure code generation options and specify the coder attributes of model parameters by using object properties.
Generate C/C++ code for the predict
and update
functions of the ECOC model by using generateCode
.
Generating C/C++ code requires MATLAB®
Coder™.
Update model parameters in the generated C/C++ code without having to regenerate the
code. This feature reduces the effort required to regenerate, redeploy, and reverify
C/C++ code when you retrain the model with new data or settings. Before updating model
parameters, use validatedUpdateInputs
to validate and extract the model parameters to
update.
This flow chart shows the code generation workflow using a coder configurer.
For the code generation usage notes and limitations of a multiclass ECOC classification
model, see the Code Generation sections of CompactClassificationECOC
, predict
, and update
.
After training a multiclass ECOC classification model with SVM or linear binary learners
by using fitcecoc
, create a coder configurer for the model
by using learnerCoderConfigurer
. Use the properties of a coder configurer to specify the
coder attributes of predict
and update
arguments. Then,
use generateCode
to generate C/C++ code based on the specified coder
attributes.
predict
ArgumentsThe properties listed in this section specify the coder attributes of the predict
function arguments in the generated code.
X
— Coder attributes of predictor dataLearnerCoderInput
objectCoder attributes of predictor data to pass to the generated C/C++ code for the
predict
function of the ECOC classification
model, specified as a LearnerCoderInput
object.
When you create a coder configurer by using the learnerCoderConfigurer
function, the input argument X
determines the default values of the LearnerCoderInput
coder attributes:
SizeVector
— The default value is the array size of the input
X
.
If the Value
attribute of the
ObservationsIn
property for the
ClassificationECOCCoderConfigurer
is 'rows'
,
then this SizeVector
value is [n
p]
, where n
corresponds to the
number of observations and p
corresponds to the
number of predictors.
If the Value
attribute of the
ObservationsIn
property for the
ClassificationECOCCoderConfigurer
is 'columns'
,
then this SizeVector
value is [p
n]
.
To switch the elements of SizeVector
(for
example, to change [n p]
to [p n]
), modify
the Value
attribute of the
ObservationsIn
property for the
ClassificationECOCCoderConfigurer
accordingly. You cannot modify the
SizeVector
value directly.
VariableDimensions
— The default value is [0
0]
, which indicates that the array size is fixed as specified in
SizeVector
.
You can set this value to [1 0]
if the
SizeVector
value is [n p]
or to
[0 1]
if it is [p n]
, which indicates
that the array has variable-size rows and fixed-size columns. For example,
[1 0]
specifies that the first value of
SizeVector
(n
) is the upper bound for
the number of rows, and the second value of SizeVector
(p
) is the number of columns.
DataType
— This value is single
or double
. The default data type depends on the data type of the input X
.
Tunability
— This value must be true
, meaning that predict
in the generated C/C++ code always includes predictor data as an input.
You can modify the coder attributes by using dot notation. For example, to generate C/C++ code
that accepts predictor data with 100 observations (in rows) of three predictor variables (in
columns), specify these coder attributes of X
for the coder configurer
configurer
:
configurer.X.SizeVector = [100 3];
configurer.X.DataType = 'double';
configurer.X.VariableDimensions = [0 0];
[0
0]
indicates that the first and second dimensions of X
(number of observations and number of predictor variables, respectively) have fixed
sizes.To allow the generated C/C++ code to accept predictor data with up to 100 observations,
specify these coder attributes of
X
:
configurer.X.SizeVector = [100 3];
configurer.X.DataType = 'double';
configurer.X.VariableDimensions = [1 0];
[1
0]
indicates that the first dimension of X
(number of
observations) has a variable size and the second dimension of X
(number
of predictor variables) has a fixed size. The specified number of observations, 100 in this
example, becomes the maximum allowed number of observations in the generated C/C++ code. To
allow any number of observations, specify the bound as Inf
.
BinaryLoss
— Coder attributes of binary learner loss functionEnumeratedInput
objectCoder attributes of the binary learner loss function ('BinaryLoss'
name-value
pair argument of predict
), specified as an EnumeratedInput
object.
The default attribute values of the
EnumeratedInput
object are based on the default values of the predict
function:
Value
— Binary learner loss function, specified as one of
the character vectors in BuiltInOptions
or a character vector
designating a custom function name. If the binary learners are SVMs or linear
classification models of SVM learners, the default value is
'hinge'
. If the binary learners are linear classification
models of logistic regression learners, the default value is
'quadratic'
.
To use a custom option, define a custom function on the MATLAB search path, and specify Value
as the name of
the custom function.
SelectedOption
— This value is
'Built-in'
(default) or 'Custom'
. The
software sets SelectedOption
according to
Value
. This attribute is read-only.
BuiltInOptions
— Cell array of
'hamming'
, 'linear'
,
'quadratic'
, 'exponential'
,
'binodeviance'
, 'hinge'
, and
'logit'
. This attribute is read-only.
IsConstant
— This value must be true
.
Tunability
— The default value is false
. If you specify other attribute values when
Tunability
is false
, the software sets
Tunability
to true
.
Decoding
— Coder attributes of decoding schemeEnumeratedInput
objectCoder attributes of the decoding scheme ('Decoding'
name-value pair
argument of predict
), specified as an EnumeratedInput
object.
The default attribute values of the
EnumeratedInput
object are based on the default values of the predict
function:
Value
— Decoding scheme value, specified as
'lossweighted'
(default), 'lossbased'
, or
a LearnerCoderInput
object.
If you set IsConstant
to false
, then
the software changes Value
to a LearnerCoderInput
object with these read-only
coder attribute values:
SizeVector
— [1 12]
VariableDimensions
— [0
1]
DataType
— 'char'
Tunability
— 1
The input in the generated code is a variable-size, tunable
character vector that is either 'lossweighted'
or
'lossbased'
.
SelectedOption
— This value is
'Built-in'
(default) or 'NonConstant'
.
The software sets SelectedOption
according to
Value
. This attribute is read-only.
BuiltInOptions
— Cell array of
'lossweighted'
and 'lossbased'
. This
attribute is read-only.
IsConstant
— The default value is
true
. If you set this value to false
, the
software changes Value
to a
LearnerCoderInput
object.
Tunability
— The default value is false
. If you specify other attribute values when
Tunability
is false
, the software sets
Tunability
to true
.
ObservationsIn
— Coder attributes of predictor data observation dimensionEnumeratedInput
objectCoder attributes of the predictor data observation dimension ('ObservationsIn'
name-value pair argument of predict
), specified as an EnumeratedInput
object.
When you create a coder configurer by using the learnerCoderConfigurer
function, the 'ObservationsIn'
name-value pair argument determines the default values
of the EnumeratedInput
coder attributes:
Value
— The default value is the predictor data
observation dimension you use when creating the coder configurer, specified as
'rows'
or 'columns'
. If you do not
specify 'ObservationsIn'
when creating the coder
configurer, the default value is 'rows'
.
This value must be 'rows'
for a model that uses SVM
binary learners.
SelectedOption
— This value is always
'Built-in'
. This attribute is read-only.
BuiltInOptions
— Cell array of 'rows'
and 'columns'
. This attribute is read-only.
IsConstant
— This value must be true
.
Tunability
— The
default value is false
if you specify
'ObservationsIn','rows'
when creating the coder
configurer, and true
if you specify
'ObservationsIn','columns'
. If you set
Tunability
to false
, the software sets
Value
to 'rows'
. If you specify other
attribute values when Tunability
is
false
, the software sets Tunability
to
true
.
NumOutputs
— Number of outputs in predict
Number of output arguments to return from the generated C/C++ code for the
predict
function of the ECOC classification
model, specified as 1, 2, or 3.
The output arguments of predict
are, in order: label
(predicted class labels), NegLoss
(negated average binary
losses), and PBScore
(positive-class scores). predict
in the generated C/C++ code
returns the first n
outputs of the predict
function, where n
is the
NumOutputs
value.
After creating the coder configurer configurer
, you can
specify the number of outputs by using dot
notation.
configurer.NumOutputs = 2;
The NumOutputs
property is equivalent to the
'-nargout'
compiler option of codegen
(MATLAB Coder). This option specifies the number of output arguments in the
entry-point function of code generation. The object function generateCode
generates two entry-point
functions—predict.m
and update.m
for the
predict
and update
functions of an ECOC classification model, respectively—and generates C/C++ code for
the two entry-point functions. The specified value for the
NumOutputs
property corresponds to the number of output
arguments in the entry-point function predict.m
.
Data Types: double
update
ArgumentsThe properties listed in this section specify the coder
attributes of the update
function
arguments in the generated code. The update
function takes a trained model
and new model parameters as input arguments, and returns an updated version of the model that
contains the new parameters. To enable updating the parameters in the generated code, you need
to specify the coder attributes of the parameters before generating code. Use a LearnerCoderInput
object to specify the coder attributes of each parameter. The default attribute values are based
on the model parameters in the input argument Mdl
of learnerCoderConfigurer
.
BinaryLearners
— Coder attributes of trained binary learnersClassificationSVMCoderConfigurer
object | ClassificationLinearCoderConfigurer
objectCoder attributes of the trained binary learners (BinaryLearners
of an
ECOC classification model), specified as a ClassificationSVMCoderConfigurer
object (for SVM binary learners) or a
ClassificationLinearCoderConfigurer
object (for linear binary
learners).
Use the update
arguments of the SVM or linear coder configurer
object to specify the coder attributes of all binary learners.
For the configuration of BinaryLearners
, the
software uses only the update
argument properties and ignores the
other properties of the object.
When you train an ECOC model with SVM binary learners, each learner can have a
different number of support vectors. Therefore, the software configures the default
attribute values of the LearnerCoderInput
objects for Alpha
,
SupportVectorLabels
, and SupportVectors
to accommodate all binary learners, based on the input
argument Mdl
of learnerCoderConfigurer
.
SizeVector
This value is [s 1]
for Alpha
and SupportVectorLabels
, where s
is the largest number of support vectors in the binary learners.
This value is [s p]
for
SupportVectors
, where p
is the
number of predictors.
VariableDimensions
— This value is [0
0]
or [1 0]
. If each learner has the same number
of support vectors, the default value is [0 0]
. Otherwise,
this value must be [1 0]
.
[0 0]
indicates that the array size is fixed as specified in
SizeVector
.
[1 0]
indicates that the array has variable-size rows and
fixed-size columns. In this case, the first value of SizeVector
is
the upper bound for the number of rows, and the second value of
SizeVector
is the number of columns.
DataType
— This value is 'single'
or
'double'
. The default data type is consistent with the data type of
the training data you use to train Mdl
.
Tunability
— If you train a model with a linear kernel function, and
the model stores the linear predictor coefficients (Beta
) without the
support vectors and related values, then this value must be false
.
Otherwise, this value must be true
.
For details about the other update
arguments, see
update
arguments of
ClassificationSVMCoderConfigurer
and update
arguments of
ClassificationLinearCoderConfigurer
.
Cost
— Coder attributes of misclassification costLearnerCoderInput
objectCoder attributes of the misclassification cost (Cost
of an ECOC classification
model), specified as a LearnerCoderInput
object.
The default attribute values of the
LearnerCoderInput
object are based on the input argument
Mdl
of learnerCoderConfigurer
:
SizeVector
— This value must be [c c]
,
where c
is the number of classes.
VariableDimensions
— This value must be [0 0]
,
indicating that the array size is fixed as specified in
SizeVector
.
DataType
— This value is 'single'
or
'double'
. The default data type is consistent with the data type of
the training data you use to train Mdl
.
Tunability
— The default value is true
.
Prior
— Coder attributes of prior probabilitiesLearnerCoderInput
objectCoder attributes of the prior probabilities (Prior
of an ECOC
classification model), specified as a LearnerCoderInput
object.
The default attribute values of the
LearnerCoderInput
object are based on the input argument
Mdl
of learnerCoderConfigurer
:
SizeVector
— This value must be [1 c]
,
where c
is the number of classes.
VariableDimensions
— This value must be [0 0]
,
indicating that the array size is fixed as specified in
SizeVector
.
DataType
— This value is 'single'
or
'double'
. The default data type is consistent with the data type of
the training data you use to train Mdl
.
Tunability
— The default value is true
.
OutputFileName
— File name of generated C/C++ code'ClassificationECOCModel'
(default) | character vectorFile name of the generated C/C++ code, specified as a character vector.
The object function generateCode
of
ClassificationECOCCoderConfigurer
generates C/C++ code using this file name.
The file name must not contain spaces because they can lead to code generation failures in certain operating system configurations. Also, the name must be a valid MATLAB function name.
After creating the coder configurer configurer
, you can specify the file
name by using dot
notation.
configurer.OutputFileName = 'myModel';
Data Types: char
Verbose
— Verbosity leveltrue
(logical 1) (default) | false
(logical 0)Verbosity level, specified as true
(logical 1) or
false
(logical 0). The verbosity level controls the display of
notification messages at the command line.
Value | Description |
---|---|
true (logical 1) | The software displays notification messages when your changes to the coder attributes of a parameter result in changes for other dependent parameters. |
false (logical
0) | The software does not display notification messages. |
To enable updating machine learning model parameters in the generated code, you need to configure the coder attributes of the parameters before generating code. The coder attributes of parameters are dependent on each other, so the software stores the dependencies as configuration constraints. If you modify the coder attributes of a parameter by using a coder configurer, and the modification requires subsequent changes to other dependent parameters to satisfy configuration constraints, then the software changes the coder attributes of the dependent parameters. The verbosity level determines whether or not the software displays notification messages for these subsequent changes.
After creating the coder configurer configurer
, you can modify the
verbosity level by using dot
notation.
configurer.Verbose = false;
Data Types: logical
To customize the code generation workflow, use the generateFiles
function and the following three properties with codegen
(MATLAB Coder), instead of using the generateCode
function.
After generating the two entry-point function files (predict.m
and
update.m
) by using the generateFiles
function, you can modify these files according to your code generation workflow. For
example, you can modify the predict.m
file to include data preprocessing,
or you can add these entry-point functions to another code generation project. Then, you can
generate C/C++ code by using the codegen
(MATLAB Coder) function and the
codegen
arguments appropriate for the modified entry-point
functions or code generation project. Use the three properties described in this section as
a starting point to set the codegen
arguments.
CodeGenerationArguments
— codegen
argumentsThis property is read-only.
codegen
(MATLAB Coder) arguments, specified as a cell array.
This property enables you to customize the code generation workflow. Use the generateCode
function if you do not need to customize your
workflow.
Instead of using generateCode
with the coder configurer configurer
,
you can generate C/C++ code as
follows:
generateFiles(configurer) cgArgs = configurer.CodeGenerationArguments; codegen(cgArgs{:})
cgArgs
accordingly
before calling codegen
.
If you modify other properties of configurer
, the software updates
the CodeGenerationArguments
property accordingly.
Data Types: cell
PredictInputs
— List of tunable input arguments of predict
This property is read-only.
List of tunable input arguments of the entry-point function
predict.m
for code generation, specified as a cell array. The
cell array contains another cell array that includes coder.PrimitiveType
(MATLAB Coder) objects and coder.Constant
(MATLAB Coder) objects.
If you modify the coder attributes of predict
arguments,
then the software updates the corresponding objects accordingly. If you specify the
Tunability
attribute as false
, then the
software removes the corresponding objects from the PredictInputs
list.
The cell array in PredictInputs
is equivalent to
configurer.CodeGenerationArguments{6}
for the coder configurer
configurer
.
Data Types: cell
UpdateInputs
— List of tunable input arguments of update
This property is read-only.
List of the tunable input arguments of the entry-point function
update.m
for code generation, specified as a cell array of a
structure. The structure includes a coder.CellType
(MATLAB Coder) object for BinaryLearners
and coder.PrimitiveType
(MATLAB Coder) objects for Cost
and
Prior
.
If you modify the coder attributes of update
arguments, then the software updates the
corresponding objects accordingly. If you specify the Tunability
attribute as false
, then the software removes the corresponding
object from the UpdateInputs
list.
The structure in UpdateInputs
is equivalent to
configurer.CodeGenerationArguments{3}
for the coder configurer
configurer
.
Data Types: cell
generateCode | Generate C/C++ code using coder configurer |
generateFiles | Generate MATLAB files for code generation using coder configurer |
validatedUpdateInputs | Validate and extract machine learning model parameters to update |
Train a machine learning model, and then generate code for the predict
and update
functions of the model by using a coder configurer.
Load Fisher's iris data set and train a multiclass ECOC model using SVM binary learners.
load fisheriris
X = meas;
Y = species;
Mdl = fitcecoc(X,Y);
Mdl
is a ClassificationECOC
object.
Create a coder configurer for the ClassificationECOC
model by using learnerCoderConfigurer
. Specify the predictor data X
. The learnerCoderConfigurer
function uses the input X
to configure the coder attributes of the predict
function input.
configurer = learnerCoderConfigurer(Mdl,X)
configurer = ClassificationECOCCoderConfigurer with properties: Update Inputs: BinaryLearners: [1x1 ClassificationSVMCoderConfigurer] Prior: [1x1 LearnerCoderInput] Cost: [1x1 LearnerCoderInput] Predict Inputs: X: [1x1 LearnerCoderInput] Code Generation Parameters: NumOutputs: 1 OutputFileName: 'ClassificationECOCModel' Properties, Methods
configurer
is a ClassificationECOCCoderConfigurer
object, which is a coder configurer of a ClassificationECOC
object.
To generate C/C++ code, you must have access to a C/C++ compiler that is configured properly. MATLAB Coder locates and uses a supported, installed compiler. You can use mex
-setup
to view and change the default compiler. For more details, see Change Default Compiler.
Generate code for the predict
and update
functions of the ECOC classification model (Mdl
) with default settings.
generateCode(configurer)
generateCode creates these files in output folder: 'initialize.m', 'predict.m', 'update.m', 'ClassificationECOCModel.mat'
The generateCode
function completes these actions:
Generate the MATLAB files required to generate code, including the two entry-point functions predict.m
and update.m
for the predict
and update
functions of Mdl
, respectively.
Create a MEX function named ClassificationECOCModel
for the two entry-point functions.
Create the code for the MEX function in the codegen\mex\ClassificationECOCModel
folder.
Copy the MEX function to the current folder.
Display the contents of the predict.m
, update.m
, and initialize.m
files by using the type
function.
type predict.m
function varargout = predict(X,varargin) %#codegen % Autogenerated by MATLAB, 20-Aug-2020 18:27:44 [varargout{1:nargout}] = initialize('predict',X,varargin{:}); end
type update.m
function update(varargin) %#codegen % Autogenerated by MATLAB, 20-Aug-2020 18:27:44 initialize('update',varargin{:}); end
type initialize.m
function [varargout] = initialize(command,varargin) %#codegen % Autogenerated by MATLAB, 20-Aug-2020 18:27:44 coder.inline('always') persistent model if isempty(model) model = loadLearnerForCoder('ClassificationECOCModel.mat'); end switch(command) case 'update' % Update struct fields: BinaryLearners % Prior % Cost model = update(model,varargin{:}); case 'predict' % Predict Inputs: X X = varargin{1}; if nargin == 2 [varargout{1:nargout}] = predict(model,X); else PVPairs = cell(1,nargin-2); for i = 1:nargin-2 PVPairs{1,i} = varargin{i+1}; end [varargout{1:nargout}] = predict(model,X,PVPairs{:}); end end end
Train an error-correcting output codes (ECOC) model using SVM binary learners and create a coder configurer for the model. Use the properties of the coder configurer to specify coder attributes of the ECOC model parameters. Use the object function of the coder configurer to generate C code that predicts labels for new predictor data. Then retrain the model using different settings, and update parameters in the generated code without regenerating the code.
Train Model
Load Fisher's iris data set.
load fisheriris
X = meas;
Y = species;
Create an SVM binary learner template to use a Gaussian kernel function and to standardize predictor data.
t = templateSVM('KernelFunction','gaussian','Standardize',true);
Train a multiclass ECOC model using the template t
.
Mdl = fitcecoc(X,Y,'Learners',t);
Mdl
is a ClassificationECOC
object.
Create Coder Configurer
Create a coder configurer for the ClassificationECOC
model by using learnerCoderConfigurer
. Specify the predictor data X
. The learnerCoderConfigurer
function uses the input X
to configure the coder attributes of the predict
function input. Also, set the number of outputs to 2 so that the generated code returns the first two outputs of the predict
function, which are the predicted labels and negated average binary losses.
configurer = learnerCoderConfigurer(Mdl,X,'NumOutputs',2)
configurer = ClassificationECOCCoderConfigurer with properties: Update Inputs: BinaryLearners: [1x1 ClassificationSVMCoderConfigurer] Prior: [1x1 LearnerCoderInput] Cost: [1x1 LearnerCoderInput] Predict Inputs: X: [1x1 LearnerCoderInput] Code Generation Parameters: NumOutputs: 2 OutputFileName: 'ClassificationECOCModel' Properties, Methods
configurer
is a ClassificationECOCCoderConfigurer
object, which is a coder configurer of a ClassificationECOC
object. The display shows the tunable input arguments of predict
and update
: X
, BinaryLearners
, Prior
, and Cost
.
Specify Coder Attributes of Parameters
Specify the coder attributes of predict
arguments (predictor data and the name-value pair arguments 'Decoding'
and 'BinaryLoss'
) and update
arguments (support vectors of the SVM learners) so that you can use these arguments as the input arguments of predict
and update
in the generated code.
First, specify the coder attributes of X
so that the generated code accepts any number of observations. Modify the SizeVector
and VariableDimensions
attributes. The SizeVector
attribute specifies the upper bound of the predictor data size, and the VariableDimensions
attribute specifies whether each dimension of the predictor data has a variable size or fixed size.
configurer.X.SizeVector = [Inf 4]; configurer.X.VariableDimensions = [true false];
The size of the first dimension is the number of observations. In this case, the code specifies that the upper bound of the size is Inf
and the size is variable, meaning that X
can have any number of observations. This specification is convenient if you do not know the number of observations when generating code.
The size of the second dimension is the number of predictor variables. This value must be fixed for a machine learning model. X
contains 4 predictors, so the second value of the SizeVector
attribute must be 4 and the second value of the VariableDimensions
attribute must be false
.
Next, modify the coder attributes of BinaryLoss
and Decoding
to use the 'BinaryLoss'
and 'Decoding'
name-value pair arguments in the generated code. Display the coder attributes of BinaryLoss
.
configurer.BinaryLoss
ans = EnumeratedInput with properties: Value: 'hinge' SelectedOption: 'Built-in' BuiltInOptions: {1x7 cell} IsConstant: 1 Tunability: 0
To use a nondefault value in the generated code, you must specify the value before generating the code. Specify the Value
attribute of BinaryLoss
as 'exponential'
.
configurer.BinaryLoss.Value = 'exponential';
configurer.BinaryLoss
ans = EnumeratedInput with properties: Value: 'exponential' SelectedOption: 'Built-in' BuiltInOptions: {1x7 cell} IsConstant: 1 Tunability: 1
If you modify attribute values when Tunability
is false
(logical 0), the software sets the Tunability
to true
(logical 1).
Display the coder attributes of Decoding
.
configurer.Decoding
ans = EnumeratedInput with properties: Value: 'lossweighted' SelectedOption: 'Built-in' BuiltInOptions: {'lossweighted' 'lossbased'} IsConstant: 1 Tunability: 0
Specify the IsConstant
attribute of Decoding
as false
so that you can use all available values in BuiltInOptions
in the generated code.
configurer.Decoding.IsConstant = false; configurer.Decoding
ans = EnumeratedInput with properties: Value: [1x1 LearnerCoderInput] SelectedOption: 'NonConstant' BuiltInOptions: {'lossweighted' 'lossbased'} IsConstant: 0 Tunability: 1
The software changes the Value
attribute of Decoding
to a LearnerCoderInput
object so that you can use both 'lossweighted'
and 'lossbased
' as the value of 'Decoding'
. Also, the software sets the SelectedOption
to 'NonConstant'
and the Tunability
to true
.
Finally, modify the coder attributes of SupportVectors
in BinaryLearners
. Display the coder attributes of SupportVectors
.
configurer.BinaryLearners.SupportVectors
ans = LearnerCoderInput with properties: SizeVector: [54 4] VariableDimensions: [1 0] DataType: 'double' Tunability: 1
The default value of VariableDimensions
is [true false]
because each learner has a different number of support vectors. If you retrain the ECOC model using new data or different settings, the number of support vectors in the SVM learners can vary. Therefore, increase the upper bound of the number of support vectors.
configurer.BinaryLearners.SupportVectors.SizeVector = [150 4];
SizeVector attribute for Alpha has been modified to satisfy configuration constraints. SizeVector attribute for SupportVectorLabels has been modified to satisfy configuration constraints.
If you modify the coder attributes of SupportVectors
, then the software modifies the coder attributes of Alpha
and SupportVectorLabels
to satisfy configuration constraints. If the modification of the coder attributes of one parameter requires subsequent changes to other dependent parameters to satisfy configuration constraints, then the software changes the coder attributes of the dependent parameters.
Display the coder configurer.
configurer
configurer = ClassificationECOCCoderConfigurer with properties: Update Inputs: BinaryLearners: [1x1 ClassificationSVMCoderConfigurer] Prior: [1x1 LearnerCoderInput] Cost: [1x1 LearnerCoderInput] Predict Inputs: X: [1x1 LearnerCoderInput] BinaryLoss: [1x1 EnumeratedInput] Decoding: [1x1 EnumeratedInput] Code Generation Parameters: NumOutputs: 2 OutputFileName: 'ClassificationECOCModel' Properties, Methods
The display now includes BinaryLoss
and Decoding
as well.
Generate Code
To generate C/C++ code, you must have access to a C/C++ compiler that is configured properly. MATLAB Coder locates and uses a supported, installed compiler. You can use mex
-setup
to view and change the default compiler. For more details, see Change Default Compiler.
Generate code for the predict
and update
functions of the ECOC classification model (Mdl
).
generateCode(configurer)
generateCode creates these files in output folder: 'initialize.m', 'predict.m', 'update.m', 'ClassificationECOCModel.mat'
The generateCode
function completes these actions:
Generate the MATLAB files required to generate code, including the two entry-point functions predict.m
and update.m
for the predict
and update
functions of Mdl
, respectively.
Create a MEX function named ClassificationECOCModel
for the two entry-point functions.
Create the code for the MEX function in the codegen\mex\ClassificationECOCModel
folder.
Copy the MEX function to the current folder.
Verify Generated Code
Pass some predictor data to verify whether the predict
function of Mdl
and the predict
function in the MEX function return the same labels. To call an entry-point function in a MEX function that has more than one entry point, specify the function name as the first input argument. Because you specified 'Decoding'
as a tunable input argument by changing the IsConstant
attribute before generating the code, you also need to specify it in the call to the MEX function, even though 'lossweighted'
is the default value of 'Decoding'
.
[label,NegLoss] = predict(Mdl,X,'BinaryLoss','exponential'); [label_mex,NegLoss_mex] = ClassificationECOCModel('predict',X,'BinaryLoss','exponential','Decoding','lossweighted');
Compare label
to label_mex
by using isequal
.
isequal(label,label_mex)
ans = logical
1
isequal
returns logical 1 (true
) if all the inputs are equal. The comparison confirms that the predict
function of Mdl
and the predict
function in the MEX function return the same labels.
NegLoss_mex
might include round-off differences compared to NegLoss
. In this case, compare NegLoss_mex
to NegLoss
, allowing a small tolerance.
find(abs(NegLoss-NegLoss_mex) > 1e-8)
ans = 0x1 empty double column vector
The comparison confirms that NegLoss
and NegLoss_mex
are equal within the tolerance 1e–8
.
Retrain Model and Update Parameters in Generated Code
Retrain the model using a different setting. Specify 'KernelScale'
as 'auto'
so that the software selects an appropriate scale factor using a heuristic procedure.
t_new = templateSVM('KernelFunction','gaussian','Standardize',true,'KernelScale','auto'); retrainedMdl = fitcecoc(X,Y,'Learners',t_new);
Extract parameters to update by using validatedUpdateInputs
. This function detects the modified model parameters in retrainedMdl
and validates whether the modified parameter values satisfy the coder attributes of the parameters.
params = validatedUpdateInputs(configurer,retrainedMdl);
Update parameters in the generated code.
ClassificationECOCModel('update',params)
Verify Generated Code
Compare the outputs from the predict
function of retrainedMdl
to the outputs from the predict
function in the updated MEX function.
[label,NegLoss] = predict(retrainedMdl,X,'BinaryLoss','exponential','Decoding','lossbased'); [label_mex,NegLoss_mex] = ClassificationECOCModel('predict',X,'BinaryLoss','exponential','Decoding','lossbased'); isequal(label,label_mex)
ans = logical
1
find(abs(NegLoss-NegLoss_mex) > 1e-8)
ans = 0x1 empty double column vector
The comparison confirms that label
and label_mex
are equal, and NegLoss
and NegLoss_mex
are equal within the tolerance.
LearnerCoderInput
ObjectA coder configurer uses a LearnerCoderInput
object to
specify the coder attributes of predict
and update
input arguments.
A LearnerCoderInput
object has the following attributes to specify the
properties of an input argument array in the generated code.
Attribute Name | Description |
---|---|
SizeVector | Array size if the corresponding
Upper bound of the array
size if the corresponding |
VariableDimensions | Indicator specifying whether each dimension of the array has a
variable size or fixed size, specified as
|
DataType | Data type of the array |
Tunability | Indicator specifying whether or not
If you specify other attribute values when
|
After creating a coder configurer, you can modify the coder attributes by using dot
notation. For example, specify the coder attributes of the coefficients
Alpha
in BinaryLearners
of the coder configurer
configurer
:
configurer.BinaryLearners.Alpha.SizeVector = [100 1];
configurer.BinaryLearners.Alpha.VariableDimensions = [1 0];
configurer.BinaryLearners.Alpha.DataType = 'double';
Verbose
) as true
(default), then the software displays notification messages when you modify the coder
attributes of a machine learning model parameter and the modification changes the coder
attributes of other dependent parameters.EnumeratedInput
ObjectA coder configurer uses an EnumeratedInput
object to specify the coder attributes of predict
input arguments that have a finite set of available values.
An EnumeratedInput
object has the following attributes to specify the properties of an input argument array in the generated code.
Attribute Name | Description |
---|---|
Value | Value of the
The default value of |
SelectedOption | Status of the selected option, specified as
This attribute is read-only. |
BuiltInOptions | List of available character vectors for the corresponding This attribute is read-only. |
IsConstant | Indicator specifying whether or not the array value is a compile-time constant ( If you set this value to |
Tunability | Indicator specifying whether or not If you specify other attribute values when
|
After creating a coder configurer, you can modify the coder attributes by using dot
notation. For example, specify the coder attributes of BinaryLoss
of the
coder configurer
configurer
:
configurer.BinaryLoss.Value = 'linear';
ClassificationECOC
| ClassificationLinearCoderConfigurer
| ClassificationSVMCoderConfigurer
| CompactClassificationECOC
| learnerCoderConfigurer
| predict
| update
You have a modified version of this example. Do you want to open this example with your edits?