save model object in file for code generation -凯发k8网页登录
save model object in file for code generation
since r2019b
description
to generate c/c code for the object functions of machine learning
models (including predict
, random
,
knnsearch
, rangesearch
,
isanomaly
, and incremental learning functions),
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.
this flow chart shows the code generation workflow for the object functions of
machine learning models. use savelearnerforcoder
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 savelearnerforcoder
for the highlighted step.
savelearnerforcoder(
prepares a model (mdl
,filename
)mdl
) for code generation and
saves it in the matlab formatted binary file (mat-file) named
filename
. you can pass
filename
to loadlearnerforcoder
to reconstruct the model
object from the filename
file.
examples
input arguments
tips
before saving the model using the
savelearnerforcoder
function, you can remove support vectors from a linear svm model or an ecoc model with linear svm learners by using thediscardsupportvectors
function. the predictor coefficients in a linear svm model provide enough information to predict labels and responses for new observations, and removing the support vectors reduces memory usage in the generated code.if
mdl
is a linear svm model, and the model has both predictor coefficients and support vectors, then you can remove the support vectors from the model by using the function (for classification) or thediscardsupportvectors
function (for regression). by default, an svm model with a linear kernel includes both predictor coefficients and support vectors.if
mdl
is an ecoc model with linear svm learners, and the learners have both predictor coefficients and support vectors, then you can remove the support vectors from the learners by using the function. the defaultsavesupportvectors
value of linear svm learners isfalse
. therefore, by default, an ecoc model does not include support vectors for the learners.
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
, thesavelearnerforcoder
function removes properties such as hyperparameter optimization properties, training solver information, and others.
loadlearnerforcoder
loads the model saved by savelearnerforcoder
.
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 svm for one-class and binary classification linear model for binary classification multiclass model for svms and linear models binary decision tree for regression support vector machine (svm) regression linear regression 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, see code generation for prediction and update using coder configurer.
version history
introduced in r2019b
see also
loadlearnerforcoder
| codegen
(matlab coder) | generatelearnerdatatypefcn