reconstruct model object from saved model for code generation -凯发k8网页登录
reconstruct model object from saved model for code generation
since r2019b
syntax
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.
for functions that support single-precision c/c code generation, use savelearnerforcoder
, loadlearnerforcoder
, and
codegen
(matlab coder); specify the name-value argument
'datatype','single'
when you call 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 model (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.
examples
input arguments
output arguments
limitations
when
mdl
iscompactlinearmodel
— suppose you train a linear model by using and specifying 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 the property 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
, 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.
extended capabilities
version history
introduced in r2019b
see also
savelearnerforcoder
| codegen
(matlab coder) | generatelearnerdatatypefcn