loadLearnerForCoder
Syntax
Description
To generate C/C++ code for the object functions of machine learning models (includingpredict
,random
,knnsearch
,rangesearch
,isanomaly
, and incremental learning functions), usesaveLearnerForCoder
,loadLearnerForCoder
, andcodegen
(MATLAB Coder). After training a machine learning model, save the model by usingsaveLearnerForCoder
. Define an entry-point function that loads the model by usingloadLearnerForCoder
and calls an object function. Then usecodegen
or theMATLAB®Coder™app to generate C/C++ code. Generating C/C++ code requiresMATLAB Coder.
For functions that support single-precision C/C++ code generation, usesaveLearnerForCoder
,loadLearnerForCoder
, andcodegen
(MATLAB Coder); specify the name-value argument'DataType','single'
when you call theloadLearnerForCoder
函数。
This flow chart shows the code generation workflow for the object functions of machine learning models. UseloadLearnerForCoder
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 bygenerateLearnerDataTypeFcn
, and use the structure as an input argument ofloadLearnerForCoder
in an entry-point function. Generating fixed-point C/C++ code requiresMATLAB Coderand Fixed-Point Designer™.
This flow chart shows the fixed-point code generation workflow for thepredict
function of a machine learning model. UseloadLearnerForCoder
for the highlighted step.
reconstructs a model (Mdl
= loadLearnerForCoder(filename
)Mdl
) from the model stored in the MATLAB formatted binary file (MAT-file) namedfilename
. You must create thefilename
file by usingsaveLearnerForCoder
.
returns a fixed-point version of the model stored inMdl
= loadLearnerForCoder(filename
,'DataType',T
)filename
. The structureT
contains the fields that specify the fixed-point data types for the variables required to use thepredict
function of the model. CreateT
using the function generated bygenerateLearnerDataTypeFcn
.
Use this syntax in an entry-point function, and usecodegen
to generate fixed-point code for the entry-point function. You can use this syntax only when generating code.
Examples
Input Arguments
Output Arguments
Limitations
When
Mdl
isCompactLinearModel
—Suppose you train a linear model by usingfitlm
and specifying'RobustOpts'
as a structure with an anonymous function handle for theRobustWgtFun
field, usesaveLearnerForCoder
to save the model, and then useloadLearnerForCoder
to load the model. In this case,loadLearnerForCoder
cannot restore theRobustproperty into the MATLAB Workspace. However,loadLearnerForCoder
can load the model at compile time within an entry-point function for code generation.When
Mdl
isCompactClassificationSVM
orCompactClassificationECOC
—If you usesaveLearnerForCoder
to save a model that is equipped to predict posterior probabilities, and useloadLearnerForCoder
to load the model, thenloadLearnerForCoder
cannot restore theScoreTransform
property into the MATLAB Workspace. However,loadLearnerForCoder
can load the model, including theScoreTransform
property, within an entry-point function at compile time for code generation.
Tips
For single-precision code generation for a Gaussian process regression (GPR) model or a support vector machine (SVM) model, use standardized data by specifying
'Standardize',true
when you train the model.
Algorithms
saveLearnerForCoder
prepares a machine learning model (Mdl
) for code generation. The function removes some unnecessary properties.
For a model that has a corresponding compact model, the
saveLearnerForCoder
function applies the appropriatecompact
function to the model before saving it.For a model that does not have a corresponding compact model, such as
ClassificationKNN
,ClassificationKernel
,ClassificationLinear
,RegressionKernel
,RegressionLinear
,ExhaustiveSearcher
,KDTreeSearcher
, andIsolationForest
,saveLearnerForCoder
function removes properties such as hyperparameter optimization properties, training solver information, and others.
loadLearnerForCoder
loads the model saved bysaveLearnerForCoder
.
Alternative Functionality
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
andupdate
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, seeCode Generation for Prediction and Update Using Coder Configurer.
Extended Capabilities
Version History
Introduced in R2019b
See Also
saveLearnerForCoder
|codegen
(MATLAB Coder)|generateLearnerDataTypeFcn
Topics
- 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
- Code Generation for Anomaly Detection
- Fixed-Point Code Generation for Prediction of SVM
- Specify Variable-Size Arguments for Code Generation