Main Content

saveCompactModel

(Removed) Save model object in file for code generation

saveCompactModel has been removed. Use saveLearnerForCoder instead. To update your code, simply replace instances of saveCompactModel with saveLearnerForCoder.

Description

To generate C/C++ code for the object functions (predict, random, knnsearch, or rangesearch) of machine learning models, use saveCompactModel, loadCompactModel, and codegen (MATLAB Coder). After training a machine learning model, save the model by using saveCompactModel. Define an entry-point function that loads the model by using loadCompactModel 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 saveCompactModel for the highlighted step.

Code generation workflow with step 2 highlighted. Step 1: Train model. Step 2: Save model. Step 3: Define entry-point function. Step 4: Generate code. Step 5: Verify generated code.

example

saveCompactModel(Mdl,filename) prepares a classification model, regression model, or nearest neighbor searcher (Mdl) for code generation and saves it in the MATLAB formatted binary file (MAT-file) named filename. You can pass filename to loadCompactModel to reconstruct the model object from the filename file.

Examples

collapse all

After training a machine learning model, save the model by using saveCompactModel. Define an entry-point function that loads the model by using loadCompactModel and calls the predict function of the trained model. Then use codegen (MATLAB Coder) to generate C/C++ code.

This example briefly explains the code generation workflow for the prediction of machine learning models at the command line. For more details, see Code Generation for Prediction of Machine Learning Model at Command Line. You can also generate code using the MATLAB Coder app. See Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App for details. To learn about the code generation for finding nearest neighbors using a nearest neighbor searcher model, see Code Generation for Nearest Neighbor Searcher.

Load Fisher's iris data set. Remove all observed setosa irises data so that X and Y contain data for two classes only.

load fisheriris
inds = ~strcmp(species,'setosa');
X = meas(inds,:);
Y = species(inds);

Train a support vector machine (SVM) classification model using the processed data set.

Mdl = fitcsvm(X,Y);

Mdl is a ClassificationSVM model.

Save the SVM classification model to the file SVMIris.mat by using saveCompactModel.

saveCompactModel(Mdl,'SVMIris');

Define an entry-point function named classifyIrises that does the following:

  • Accept iris flower measurements with columns corresponding to meas, and return predicted labels.

  • Load a trained SVM classification model.

  • Predict labels using the loaded classification model for the iris flower measurements.

function label = classifyIrises(X) %#codegen
%CLASSIFYIRISES Classify iris species using SVM Model
%   CLASSIFYIRISES classifies the iris flower measurements in X using the
%   compact SVM model in the file SVMIris.mat, and then returns class
%   labels in label.
CompactMdl = loadCompactModel('SVMIris');
label = predict(CompactMdl,X);
end

Add the %#codegen compiler directive (or pragma) to the entry-point function after the function signature to indicate that you intend to generate code for the MATLAB algorithm. Adding this directive instructs the MATLAB Code Analyzer to help you diagnose and fix violations that would result in errors during code generation.

Generate code for the entry-point function using codegen (MATLAB Coder). Because C and C++ are statically typed languages, you must determine the properties of all variables in the entry-point function at compile time. Pass X as the value of the -args option to specify that the generated code must accept an input that has the same data type and array size as the training data X. If the number of observations is unknown at compile time, you can also specify the input as variable-size by using coder.typeof (MATLAB Coder). For details, see Specify Variable-Size Arguments for Code Generation and Specify Properties of Entry-Point Function Inputs (MATLAB Coder).

codegen classifyIrises -args {X}
Code generation successful.

codegen generates the MEX function classifyIrises_mex with a platform-dependent extension.

Compare the labels classified using predict, classifyIrises, and classifyIrises_mex.

label1 = predict(Mdl,X);
label2 = classifyIrises(X);
label3 = classifyIrises_mex(X);
verify_label = isequal(label1,label2,label3)
verify_label = logical
   1

isequal returns logical 1 (true), which means all the inputs are equal. The labels classified all three ways are the same.

Input Arguments

collapse all

Machine learning model, specified as a full or compact model object, as given in the following tables of supported models.

File name, specified as a character vector or string scalar.

If the filename file exists, then saveCompactModel overwrites the file.

The extension of the filename file must be .mat. If filename has no extension, then saveCompactModel appends .mat.

If filename does not include a full path, then saveCompactModel saves the file to the current folder.

Example: 'SVMMdl'

Data Types: char | string

Algorithms

saveCompactModel 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 saveCompactModel 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 saveCompactModel function removes properties such as hyperparameter optimization properties, training solver information, and others.

loadCompactModel loads the model saved by saveCompactModel.

Alternative Functionality

Version History

Introduced in R2016b

collapse all

R2021b: saveCompactModel has been removed

saveCompactModel has been removed. Use saveLearnerForCoder instead.

saveLearnerForCoder and loadLearnerForCoder provide broader functionality, including fixed-point code generation for supported models.

This table shows how to update your code to use saveLearnerForCoder.

RemovedRecommended
saveCompactModel(Model,'MyModel');
saveLearnerForCoder(Model,'MyModel');