main content

update model parameters for code generation -凯发k8网页登录

update model parameters for code generation

since r2018b

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 . then you can use the update function to update model parameters in the generated code without having to regenerate the code. this feature reduces the effort required to regenerate, redeploy, and reverify c/c code when you retrain a model with new data or settings.

this flow chart shows the code generation workflow using a coder configurer. use update for the highlighted step.

if you do not generate code, then you do not need to use the update function. when you retrain a model in matlab®, the returned model already includes modified parameters.

updatedmdl = update(mdl,params) returns an updated version of mdl that contains new parameters in params.

after retraining a model, use the validatedupdateinputs function to detect modified parameters in the retrained model and validate whether the modified parameter values satisfy the coder attributes of the parameters. use the output of validatedupdateinputs, the validated parameters, as the input params to update model parameters.

examples

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. 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 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 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 . 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.

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 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'
  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'
code generation successful.

the 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 . 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.

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,:));

configurer is a 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 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 . 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.

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.

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 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 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 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 . 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

machine learning model, specified as a model object, as given in this table of supported models.

modelmodel object
binary decision tree for multiclass classificationcompactclassificationtree
svm for one-class and binary classificationcompactclassificationsvm
linear model for binary classificationclassificationlinear
multiclass model for svms and linear modelscompactclassificationecoc
binary decision tree for regressioncompactregressiontree
support vector machine (svm) regressioncompactregressionsvm
linear regressionregressionlinear

for the code generation usage notes and limitations of a machine learning model, see the code generation section of the model object page.

parameters to update in the machine learning model, specified as a structure with a field for each parameter to update.

create params by using the function. this function detects modified parameters in the retrained model, validates whether the modified parameter values satisfy the coder attributes of the parameters, and returns the parameters to update as a structure.

the set of parameters that you can update varies depending on the machine learning model, as described in this table.

modelparameters to update
binary decision tree for multiclass classificationchildren, classprobability, cost, cutpoint, cutpredictorindex, prior
svm for one-class and binary classification
  • if mdl is a one-class svm classification model, then params cannot include cost or prior.

linear model for binary classificationbeta, bias, cost, prior
multiclass model for svms and linear models

binarylearners, cost, prior

binary decision tree for regressionchildren, cutpoint, cutpredictorindex, nodemean
svm regression
linear regressionbeta, bias

output arguments

updated machine learning model, returned as a model object that is the same type of object as mdl. the output updatedmdl is an updated version of the input mdl that contains new parameters in params.

tips

algorithms

in the coder configurer workflow, the mdl input argument of update is a model returned by loadlearnerforcoder. this model and the updatedmdl object are reduced classification or regression models that primarily contain properties required for prediction.

extended capabilities

version history

introduced in r2018b

网站地图