# saveLearnerForCoder

Save model object in file for code generation

## 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 the`discardSupportVectors`

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`discardSupportVectors`

function (for classification) or the`discardSupportVectors`

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`discardSupportVectors`

function. The default`SaveSupportVectors`

value of linear SVM learners is`false`

. 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 appropriate`compact`

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`

,`IsolationForest`

, and`OneClassSVM`

, the`saveLearnerForCoder`

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

## Version History

**Introduced in R2019b**

## See Also

`loadLearnerForCoder`

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