Main Content

Reconstruct model object from saved model for code generation

To generate C/C++ code for the object functions (`predict`

,
`random`

, `knnsearch`

, or
`rangesearch`

) of machine learning models, use `saveLearnerForCoder`

, `loadLearnerForCoder`

, and
`codegen`

(MATLAB Coder). After training a machine
learning model, save the model by using `saveLearnerForCoder`

. Define
an entry-point function that loads the model by using
`loadLearnerForCoder`

and calls an object function. Then use
`codegen`

or the MATLAB^{®}
Coder™ app to generate C/C++ code. Generating C/C++ code requires MATLAB
Coder.

To generate single-precision C/C++ code for the object function
`predict`

of machine learning models, use `saveLearnerForCoder`

, `loadLearnerForCoder`

, and
`codegen`

(MATLAB Coder). For single-precision code
generation, specify the name-value pair argument `'DataType','single'`

as an additional input to the `loadLearnerForCoder`

function.

This flow chart shows the code generation workflow for the object functions of machine
learning models. Use `loadLearnerForCoder`

for the highlighted step.

Fixed-point C/C++ code generation requires an additional step that defines the
fixed-point data types of the variables required for prediction. Create a fixed-point
data type structure by using the data type function generated by `generateLearnerDataTypeFcn`

, and use the structure as an input argument
of `loadLearnerForCoder`

in an entry-point function. Generating
fixed-point C/C++ code requires MATLAB
Coder and Fixed-Point Designer™.

This flow chart shows the fixed-point code generation workflow for the
`predict`

function of a machine learning model. Use
`loadLearnerForCoder`

for the highlighted step.

reconstructs a classification model, regression model, or nearest neighbor searcher
(`Mdl`

= loadLearnerForCoder(`filename`

)`Mdl`

) from the model stored in the MATLAB formatted binary file (MAT-file) named `filename`

.
You must create the `filename`

file by using `saveLearnerForCoder`

.

returns a fixed-point version of the model stored in `Mdl`

= loadLearnerForCoder(`filename`

,'DataType',`T`

)`filename`

.
The structure `T`

contains the fields that specify the
fixed-point data types for the variables required to use the
`predict`

function of the model. Create
`T`

using the function generated by `generateLearnerDataTypeFcn`

.

Use this syntax in an entry-point function, and use `codegen`

to generate fixed-point code for the entry-point function. You can use this syntax
only when generating code.

For single-precision code generation for a Gaussian process regression (GPR) model, create the model by using

`fitrgp(X,Y,'Standardize',1)`

.

`saveLearnerForCoder`

prepares a machine
learning model (`Mdl`

) for code generation. The function removes some
properties that are not required for prediction.

For a model that has a corresponding compact model, the

`saveLearnerForCoder`

function applies the appropriate`compact`

function to the model before saving it.For a model that does not have a corresponding compact model, such as

`ClassificationKNN`

,`ClassificationLinear`

,`RegressionLinear`

,`ExhaustiveSearcher`

, and`KDTreeSearcher`

, the`saveLearnerForCoder`

function removes properties such as hyperparameter optimization properties, training solver information, and others.

`loadLearnerForCoder`

loads the model saved by
`saveLearnerForCoder`

.

Use a coder configurer created by

`learnerCoderConfigurer`

for the models listed in this table.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`

After training a machine learning model, create a coder configurer of the model. Use the object functions and properties of the configurer to configure code generation options and to generate code for the

`predict`

and`update`

functions of the model. If you generate code using a coder configurer, you can update model parameters in the generated code without having to regenerate the code. For details, see Code Generation for Prediction and Update Using Coder Configurer.

`generateLearnerDataTypeFcn`

| `saveLearnerForCoder`

| `codegen`

(MATLAB Coder)

- Introduction to Code Generation
- Code Generation for Prediction of Machine Learning Model at Command Line
- Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App
- Code Generation for Nearest Neighbor Searcher
- Fixed-Point Code Generation for Prediction of SVM
- Specify Variable-Size Arguments for Code Generation