validatedUpdateInputs
Validate and extract machine learning model parameters to update
Description
Generate C/C++ code for the predict
and update
functions of
a machine learning model by using a coder configurer object. Create this object by using
learnerCoderConfigurer
and its object function generateCode
. After
retraining the model with new data or settings, you can update model parameters in the
generated code without having to regenerate the code. Use
validatedUpdateInputs
to validate and extract the model parameters to
update. This function helps you identify potential problems before you update the model
parameters in the generated code. You can use the output of
validatedUpdateInputs
, the validated parameters, as an input argument
of the update
function to update model parameters.
This flow chart shows the code generation workflow using a coder configurer. Use
validatedUpdateInputs
for the highlighted step.
returns the validated machine learning model parameters to update.
params
= validatedUpdateInputs(configurer
,retrainedMdl
)validatedUpdateInputs
detects the modified model parameters in
retrainedMdl
and validates whether they satisfy the coder attributes
stored in configurer
.
Examples
Update Parameters of SVM Classification Model in Generated Code
Train an SVM model using a partial data set and create a coder configurer for the model. Use the properties of the coder configurer to specify coder attributes of the SVM 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 the whole data set and update parameters in the generated code without regenerating the code.
Train Model
Load the ionosphere
data set. This data set has 34 predictors and 351 binary responses for radar returns, either bad ('b'
) or good ('g'
).
load ionosphere
Train a binary SVM classification model using the first 50 observations and a Gaussian kernel function with an automatic kernel scale.
Mdl = fitcsvm(X(1:50,:),Y(1:50), ... 'KernelFunction','gaussian','KernelScale','auto');
Mdl
is a ClassificationSVM
object.
Create Coder Configurer
Create a coder configurer for the ClassificationSVM
model by using learnerCoderConfigurer
. Specify the predictor data X
in matrix format. Note that the learnerCoderConfigurer
function does not support the table format for predictor data. 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 predicted labels and scores.
configurer = learnerCoderConfigurer(Mdl,X(1:50,:),'NumOutputs',2);
configurer
is a ClassificationSVMCoderConfigurer
object, which is a coder configurer of a ClassificationSVM
object.
Specify Coder Attributes of Parameters
Specify the coder attributes of the SVM classification model parameters so that you can update the parameters in the generated code after retraining the model. This example specifies the coder attributes of predictor data that you want to pass to the generated code and the coder attributes of the support vectors of the SVM model.
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 34]; 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 34 predictors, so the value of the SizeVector
attribute must be 34 and the value of the VariableDimensions
attribute must be false
.
If you retrain the SVM model using new data or different settings, the number of support vectors can vary. Therefore, specify the coder attributes of SupportVectors
so that you can update the support vectors in the generated code.
configurer.SupportVectors.SizeVector = [250 34];
SizeVector attribute for Alpha has been modified to satisfy configuration constraints. SizeVector attribute for SupportVectorLabels has been modified to satisfy configuration constraints.
configurer.SupportVectors.VariableDimensions = [true false];
VariableDimensions attribute for Alpha has been modified to satisfy configuration constraints. VariableDimensions 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.
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.
Use generateCode
to generate code for the predict
and update
functions of the SVM classification model (Mdl
) with default settings.
generateCode(configurer)
generateCode creates these files in output folder: 'initialize.m', 'predict.m', 'update.m', 'ClassificationSVMModel.mat' Code generation successful.
generateCode
generates 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. Then generateCode
creates a MEX function named ClassificationSVMModel
for the two entry-point functions in the codegen\mex\ClassificationSVMModel
folder and copies 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.
[label,score] = predict(Mdl,X);
[label_mex,score_mex] = ClassificationSVMModel('predict',X);
Compare label
and 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.
score_mex
might include round-off differences compared with score
. In this case, compare score_mex
and score
, allowing a small tolerance.
find(abs(score-score_mex) > 1e-8)
ans = 0x1 empty double column vector
The comparison confirms that score
and score_mex
are equal within the tolerance 1e–8
.
Retrain Model and Update Parameters in Generated Code
Retrain the model using the entire data set.
retrainedMdl = fitcsvm(X,Y, ... 'KernelFunction','gaussian','KernelScale','auto');
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.
ClassificationSVMModel('update',params)
Verify Generated Code
Compare the outputs from the predict
function of retrainedMdl
and the predict
function in the updated MEX function.
[label,score] = predict(retrainedMdl,X);
[label_mex,score_mex] = ClassificationSVMModel('predict',X);
isequal(label,label_mex)
ans = logical
1
find(abs(score-score_mex) > 1e-8)
ans = 0x1 empty double column vector
The comparison confirms that labels
and labels_mex
are equal, and the score values are equal within the tolerance.
Update Parameters of ECOC Classification Model in Generated Code
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'
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: {'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'
.
configurer.BinaryLoss.Value = 'exponential';
configurer.BinaryLoss
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
.
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'
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' 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
andupdate.m
for thepredict
andupdate
functions ofMdl
, 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.
Update Parameters of SVM Regression Model in Generated Code
Train a support vector machine (SVM) model using a partial data set and create a coder configurer for the model. Use the properties of the coder configurer to specify coder attributes of the SVM model parameters. Use the object function of the coder configurer to generate C code that predicts responses for new predictor data. Then retrain the model using the whole data set and update parameters in the generated code without regenerating the code.
Train Model
Load the carsmall
data set.
load carsmall
X = [Horsepower,Weight];
Y = MPG;
Train an SVM regression model using the first 50 observations and a Gaussian kernel function with an automatic kernel scale.
Mdl = fitrsvm(X(1:50,:),Y(1:50), ... 'KernelFunction','gaussian','KernelScale','auto');
Mdl
is a RegressionSVM
object.
Create Coder Configurer
Create a coder configurer for the RegressionSVM
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(1:50,:));
Warning: Default response is removed to support learnerCoderConfigurer. The model will predict NaNs for observations with missing values.
configurer
is a RegressionSVMCoderConfigurer
object, which is a coder configurer of a RegressionSVM
object.
Specify Coder Attributes of Parameters
Specify the coder attributes of the SVM regression model parameters so that you can update the parameters in the generated code after retraining the model. This example specifies the coder attributes of predictor data that you want to pass to the generated code and the coder attributes of the support vectors of the SVM regression model.
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 2]; 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 two predictors, so the value of the SizeVector
attribute must be two and the value of the VariableDimensions
attribute must be false
.
If you retrain the SVM model using new data or different settings, the number of support vectors can vary. Therefore, specify the coder attributes of SupportVectors
so that you can update the support vectors in the generated code.
configurer.SupportVectors.SizeVector = [250 2];
SizeVector attribute for Alpha has been modified to satisfy configuration constraints.
configurer.SupportVectors.VariableDimensions = [true false];
VariableDimensions attribute for Alpha has been modified to satisfy configuration constraints.
If you modify the coder attributes of SupportVectors
, then the software modifies the coder attributes of Alpha
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.
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.
Use generateCode
to generate code for the predict
and update
functions of the SVM regression model (Mdl
) with default settings.
generateCode(configurer)
generateCode creates these files in output folder: 'initialize.m', 'predict.m', 'update.m', 'RegressionSVMModel.mat' Code generation successful.
generateCode
generates 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. Then generateCode
creates a MEX function named RegressionSVMModel
for the two entry-point functions in the codegen\mex\RegressionSVMModel
folder and copies 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 predicted responses. 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.
yfit = predict(Mdl,X);
yfit_mex = RegressionSVMModel('predict',X);
yfit_mex
might include round-off differences compared with yfit
. In this case, compare yfit
and yfit_mex
, allowing a small tolerance.
find(abs(yfit-yfit_mex) > 1e-6)
ans = 0x1 empty double column vector
The comparison confirms that yfit
and yfit_mex
are equal within the tolerance 1e–6
.
Retrain Model and Update Parameters in Generated Code
Retrain the model using the entire data set.
retrainedMdl = fitrsvm(X,Y, ... 'KernelFunction','gaussian','KernelScale','auto');
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);
Warning: Default response is removed to support learnerCoderConfigurer. The model will predict NaNs for observations with missing values.
Update parameters in the generated code.
RegressionSVMModel('update',params)
Verify Generated Code
Compare the outputs from the predict
function of retrainedMdl
and the predict
function in the updated MEX function.
yfit = predict(retrainedMdl,X);
yfit_mex = RegressionSVMModel('predict',X);
find(abs(yfit-yfit_mex) > 1e-6)
ans = 0x1 empty double column vector
The comparison confirms that yfit
and yfit_mex
are equal within the tolerance 1e-6
.
Update Parameters of Regression Tree Model in Generated Code
Train a regression tree using a partial data set and create a coder configurer for the model. Use the properties of the coder configurer to specify coder attributes of the model parameters. Use the object function of the coder configurer to generate C code that predicts responses for new predictor data. Then retrain the model using the entire data set, and update parameters in the generated code without regenerating the code.
Train Model
Load the carbig
data set, and train a regression tree model using half of the observations.
load carbig X = [Displacement Horsepower Weight]; Y = MPG; rng('default') % For reproducibility n = length(Y); idxTrain = randsample(n,n/2); XTrain = X(idxTrain,:); YTrain = Y(idxTrain); Mdl = fitrtree(XTrain,YTrain);
Mdl
is a RegressionTree
object.
Create Coder Configurer
Create a coder configurer for the RegressionTree
model by using learnerCoderConfigurer
. Specify the predictor data XTrain
. The learnerCoderConfigurer
function uses the input XTrain
to configure the coder attributes of the predict
function input. Also, set the number of outputs to 2 so that the generated code returns predicted responses and node numbers for the predictions.
configurer = learnerCoderConfigurer(Mdl,XTrain,'NumOutputs',2);
configurer
is a RegressionTreeCoderConfigurer
object, which is a coder configurer of a RegressionTree
object.
Specify Coder Attributes of Parameters
Specify the coder attributes of the regression tree model parameters so that you can update the parameters in the generated code after retraining the model.
Specify the coder attributes of the X
property of configurer
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 3]; configurer.X.VariableDimensions
ans = 1x2 logical array
1 0
The size of the first dimension is the number of observations. Setting the value of the SizeVector
attribute to Inf
causes the software to change the value of the VariableDimensions
attribute to 1
. In other words, the upper bound of the size is Inf
and the size is variable, meaning that the predictor data 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. Because the predictor data contains 3 predictors, the value of the SizeVector
attribute must be 3
and the value of the VariableDimensions
attribute must be 0
.
If you retrain the tree model using new data or different settings, the number of nodes in the tree can vary. Therefore, specify the first dimension of the SizeVector
attribute of one of these properties so that you can update the number of nodes in the generated code: Children
, CutPoint
, CutPredictorIndex
, or NodeMean
. The software then modifies the other properties automatically.
For example, set the first value of the SizeVector
attribute of the NodeMean
property to Inf
. The software modifies the SizeVector
and VariableDimensions
attributes of Children
, CutPoint
, and CutPredictorIndex
to match the new upper bound on the number of nodes in the tree. Additionally, the first value of the VariableDimensions
attribute of NodeMean
changes to 1
.
configurer.NodeMean.SizeVector = [Inf 1];
SizeVector attribute for Children has been modified to satisfy configuration constraints. SizeVector attribute for CutPoint has been modified to satisfy configuration constraints. SizeVector attribute for CutPredictorIndex has been modified to satisfy configuration constraints. VariableDimensions attribute for Children has been modified to satisfy configuration constraints. VariableDimensions attribute for CutPoint has been modified to satisfy configuration constraints. VariableDimensions attribute for CutPredictorIndex has been modified to satisfy configuration constraints.
configurer.NodeMean.VariableDimensions
ans = 1x2 logical array
1 0
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 regression tree model (Mdl
).
generateCode(configurer)
generateCode creates these files in output folder: 'initialize.m', 'predict.m', 'update.m', 'RegressionTreeModel.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
andupdate.m
for thepredict
andupdate
functions ofMdl
, respectively.Create a MEX function named
RegressionTreeModel
for the two entry-point functions.Create the code for the MEX function in the
codegen\mex\RegressionTreeModel
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 predicted responses. 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.
[Yfit,node] = predict(Mdl,XTrain);
[Yfit_mex,node_mex] = RegressionTreeModel('predict',XTrain);
Compare Yfit
to Yfit_mex
and node
to node_mex
.
max(abs(Yfit-Yfit_mex),[],'all')
ans = 0
isequal(node,node_mex)
ans = logical
1
In general, Yfit_mex
might include round-off differences compared to Yfit
. In this case, the comparison confirms that Yfit
and Yfit_mex
are equal.
isequal
returns logical 1 (true
) if all the input arguments are equal. The comparison confirms that the predict
function of Mdl
and the predict
function in the MEX function return the same node numbers.
Retrain Model and Update Parameters in Generated Code
Retrain the model using the entire data set.
retrainedMdl = fitrtree(X,Y);
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.
RegressionTreeModel('update',params)
Verify Generated Code
Compare the output arguments from the predict
function of retrainedMdl
and the predict
function in the updated MEX function.
[Yfit,node] = predict(retrainedMdl,X); [Yfit_mex,node_mex] = RegressionTreeModel('predict',X); max(abs(Yfit-Yfit_mex),[],'all')
ans = 0
isequal(node,node_mex)
ans = logical
1
The comparison confirms that the predicted responses and node numbers are equal.
Input Arguments
configurer
— Coder configurer
coder configurer object
Coder configurer of a machine learning model, specified as a coder configurer object
created by using learnerCoderConfigurer
.
Model | Coder Configurer Object |
---|---|
Binary decision tree for multiclass classification | ClassificationTreeCoderConfigurer |
SVM for one-class and binary classification | ClassificationSVMCoderConfigurer |
Linear model for binary classification | ClassificationLinearCoderConfigurer |
Multiclass model for SVMs and linear models | ClassificationECOCCoderConfigurer |
Binary decision tree for regression | RegressionTreeCoderConfigurer |
Support vector machine (SVM) regression | RegressionSVMCoderConfigurer |
Linear regression | RegressionLinearCoderConfigurer |
retrainedMdl
— Retrained machine learning model
full model object | compact model object
Retrained machine learning model, specified as a full or compact model object, as given in this table of supported models.
Model | Full/Compact Model Object | Training Function |
---|---|---|
Binary decision tree for multiclass classification | ClassificationTree , CompactClassificationTree | fitctree |
SVM for one-class and binary classification | ClassificationSVM , CompactClassificationSVM | fitcsvm |
Linear model for binary classification | ClassificationLinear | fitclinear |
Multiclass model for SVMs and linear models | ClassificationECOC , CompactClassificationECOC | fitcecoc |
Binary decision tree for regression | RegressionTree , CompactRegressionTree | fitrtree |
Support vector machine (SVM) regression | RegressionSVM , CompactRegressionSVM | fitrsvm |
Linear regression | RegressionLinear | fitrlinear |
Output Arguments
params
— Validated parameters to update
structure
Validated parameters to update in the machine learning model, specified as a
structure with a field for each parameter extracted from
retrainedMdl
.
The model parameters in params
include all parameters listed in
the UpdateInputs
property of configurer
, which
is the list of tunable model parameters.
You can use params
as an input argument of update
to
update model parameters.
Tips
validatedUpdateInputs
returns an error message if you modify any of the name-value pair arguments listed in this table when you retrain the modelretrainedMdl
. In this case, you cannot useupdate
to update the parameters. You must generate C/C++ code again.Model Arguments Not Supported for Update Binary decision tree for multiclass classification Arguments of fitctree
—ClassNames
,ScoreTransform
SVM for one-class and binary classification Arguments of fitcsvm
—ClassNames
,KernelFunction
,PolynomialOrder
,ScoreTransform
,Standardize
Linear model for binary classification Arguments of fitclinear
—ClassNames
,ScoreTransform
Multiclass model for SVMs and linear models Arguments of
fitcecoc
—ClassNames
,Coding
If you specify the binary learners in
fitcecoc
as template objects (seeLearners
), then for each binary learner, you cannot modify the following:Arguments of
templateSVM
—KernelFunction
,PolynomialOrder
,Standardize
Arguments of
templateLinear
—Learner
Binary decision tree for regression Arguments of fitrtree
—ResponseTransform
SVM regression Arguments of fitrsvm
—KernelFunction
,PolynomialOrder
,ResponseTransform
,Standardize
Linear regression Arguments of fitrlinear
—ResponseTransform
validatedUpdateInputs
displays a warning message if the machine learning model parameters inconfigurer
andretrainedMdl
are identical.
Version History
Introduced in R2018b
Comando de MATLAB
Ha hecho clic en un enlace que corresponde a este comando de MATLAB:
Ejecute el comando introduciéndolo en la ventana de comandos de MATLAB. Los navegadores web no admiten comandos de MATLAB.
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)