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.
Create an SVM binary learner template to use a Gaussian kernel function and to standardize predictor data.
Train a multiclass ECOC model using the template 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 =
ClassificationECOCCoderConfigurer with properties:
Update Inputs:
BinaryLearners: [1×1 ClassificationSVMCoderConfigurer]
Prior: [1×1 LearnerCoderInput]
Cost: [1×1 LearnerCoderInput]
Predict Inputs:
X: [1×1 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.
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.
ans =
EnumeratedInput with properties:
Value: 'hinge'
SelectedOption: 'Built-in'
BuiltInOptions: {'hamming' 'linear' 'quadratic' 'exponential' 'binodeviance' 'hinge' 'logit'}
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'.
ans =
EnumeratedInput with properties:
Value: 'exponential'
SelectedOption: 'Built-in'
BuiltInOptions: {'hamming' 'linear' 'quadratic' 'exponential' 'binodeviance' 'hinge' 'logit'}
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.
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.
ans =
EnumeratedInput with properties:
Value: [1×1 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.
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.
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 =
ClassificationECOCCoderConfigurer with properties:
Update Inputs:
BinaryLearners: [1×1 ClassificationSVMCoderConfigurer]
Prior: [1×1 LearnerCoderInput]
Cost: [1×1 LearnerCoderInput]
Predict Inputs:
X: [1×1 LearnerCoderInput]
BinaryLoss: [1×1 EnumeratedInput]
Decoding: [1×1 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 creates these files in output folder:
'initialize.m', 'predict.m', 'update.m', 'ClassificationECOCModel.mat'
Code generation successful.
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'.
Compare label to label_mex by using isequal.
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.
ans =
0×1 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.
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.
Update parameters in the generated code.
Verify Generated Code
Compare the outputs from the predict function of retrainedMdl to the outputs from the predict function in the updated MEX function.
ans =
0×1 empty double column vector
The comparison confirms that label and label_mex are equal, and NegLoss and NegLoss_mex are equal within the tolerance.