main content

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.

code generation workflow for the object function of a machine learning model. step 1: train a model. step 2 (highlighted): save the model. step 3: define an entry-point function. step 4: generate code. step 5: verify the generated code.

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.

fixed-point code generation workflow. step 1: train a model. step 2 (highlighted): save the model. step 3: define the fixed-point data types. step 4: define an entry-point function. step 5 (optional): optimize the fixed-point data types. step 6: generate code. step 7: verify the generated code.

example

savelearnerforcoder(mdl,filename) prepares a model (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

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 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 . you can also generate code using the matlab coder app. see for details. to learn about the code generation for finding nearest neighbors using a nearest neighbor searcher model, see .

train model

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 object, which is a linear svm model. the predictor coefficients in a linear svm model provide enough information to predict labels for new observations. removing the support vectors reduces memory usage in the generated code. remove the support vectors from the linear svm model by using the function.

mdl = discardsupportvectors(mdl);

save model

save the svm classification model to the file svmiris.mat by using savelearnerforcoder.

savelearnerforcoder(mdl,'svmiris');

define entry-point function

define an entry-point function named classifyiris 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 = classifyiris(x) %#codegen
%classifyiris classify iris species using svm model
%   classifyiris classifies the iris flower measurements in x using the svm
%   model in the file svmiris.mat, and then returns class labels in label.
mdl = loadlearnerforcoder('svmiris');
label = predict(mdl,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.

note: if you click the button located in the upper-right section of this example and open this example in matlab®, then matlab® opens the example folder. this folder includes the entry-point function file.

generate code

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 and (matlab coder).

codegen classifyiris -args {x}
code generation successful.

codegen generates the mex function classifyiris_mex with a platform-dependent extension.

verify generated code

compare the labels classified using predict, classifyiris, and classifyiris_mex.

label1 = predict(mdl,x);
label2 = classifyiris(x);
label3 = classifyiris_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.

after training a machine learning model, save the model using savelearnerforcoder. for fixed-point code generation, specify the fixed-point data types of the variables required for prediction by using the data type function generated by generatelearnerdatatypefcn. then, define an entry-point function that loads the model by using both loadlearnerforcoder and the specified fixed-point data types, and calls the predict function of the model. use codegen (matlab coder) to generate fixed-point c/c code for the entry-point function, and then verify the generated code.

before generating code using codegen, you can use buildinstrumentedmex (fixed-point designer) and showinstrumentationresults (fixed-point designer) to optimize the fixed-point data types to improve the performance of the fixed-point code. record minimum and maximum values of named and internal variables for prediction by using buildinstrumentedmex. view the instrumentation results using showinstrumentationresults; then, based on the results, tune the fixed-point data type properties of the variables. for details regarding this optional step, see fixed-point code generation for prediction of svm.

train model

load the ionosphere data set and train a binary svm classification model.

load ionosphere
mdl = fitcsvm(x,y,'kernelfunction','gaussian');

mdl is a classificationsvm model.

save model

save the svm classification model to the file mymdl.mat by using savelearnerforcoder.

savelearnerforcoder(mdl,'mymdl');

define fixed-point data types

use generatelearnerdatatypefcn to generate a function that defines the fixed-point data types of the variables required for prediction of the svm model.

generatelearnerdatatypefcn('mymdl',x)

generatelearnerdatatypefcn generates the mymdl_datatype function.

create a structure t that defines the fixed-point data types by using mymdl_datatype.

t = mymdl_datatype('fixed')
t = struct with fields:
               xdatatype: [0x0 embedded.fi]
           scoredatatype: [0x0 embedded.fi]
    innerproductdatatype: [0x0 embedded.fi]

the structure t includes the fields for the named and internal variables required to run the predict function. each field contains a fixed-point object, returned by fi (fixed-point designer). the fixed-point object specifies fixed-point data type properties, such as word length and fraction length. for example, display the fixed-point data type properties of the predictor data.

t.xdatatype
ans = 
[]
          datatypemode: fixed-point: binary point scaling
            signedness: signed
            wordlength: 16
        fractionlength: 14
        roundingmethod: floor
        overflowaction: wrap
           productmode: fullprecision
  maxproductwordlength: 128
               summode: fullprecision
      maxsumwordlength: 128

define entry-point function

define an entry-point function named myfixedpointpredict that does the following:

  • accept the predictor data x and the fixed-point data type structure t.

  • load a fixed-point version of a trained svm classification model by using both loadlearnerforcoder and the structure t.

  • predict labels and scores using the loaded model.

function [label,score] = myfixedpointpredict(x,t) %#codegen
mdl = loadlearnerforcoder('mymdl','datatype',t);
[label,score] = predict(mdl,x);
end

note: if you click the button located in the upper-right section of this example and open the example in matlab®, then matlab opens the example folder. this folder includes the entry-point function file.

generate code

the xdatatype field of the structure t specifies the fixed-point data type of the predictor data. convert x to the type specified in t.xdatatype by using the cast (fixed-point designer) function.

x_fx = cast(x,'like',t.xdatatype);

generate code for the entry-point function using codegen. specify x_fx and constant folded t as input arguments of the entry-point function.

codegen myfixedpointpredict -args {x_fx,coder.constant(t)}
code generation successful.

codegen generates the mex function myfixedpointpredict_mex with a platform-dependent extension.

verify generated code

pass predictor data to predict and myfixedpointpredict_mex to compare the outputs.

[labels,scores] = predict(mdl,x);
[labels_fx,scores_fx] = myfixedpointpredict_mex(x_fx,t);

compare the outputs from predict and myfixedpointpredict_mex.

verify_labels = isequal(labels,labels_fx)
verify_labels = logical
   1

isequal returns logical 1 (true), which means labels and labels_fx are equal.

if you are not satisfied with the comparison results and want to improve the precision of the generated code, you can tune the fixed-point data types and regenerate the code. for details, see tips in generatelearnerdatatypefcn, data type function, and fixed-point code generation for prediction of svm.

input arguments

machine learning model, specified as a full or compact model object, as given in the following tables of supported models. the tables also show whether each model supports fixed-point code generation.

  • classification model object

    modelfull/compact model objectsfixed-point code generation supportsingle-precision code generation support
    discriminant analysis classification, noyes
    multiclass model for support vector machines (svms) or other classifiersclassificationecoc, compactclassificationecocnoyes
    ensemble classifier, , yes (only for ensembles of decision trees)yes
    gaussian kernel model for binary classification of big dataclassificationkernelnoyes
    k-nearest neighbor classificationclassificationknnnoyes
    linear model for binary classification of high-dimensional dataclassificationlinearnoyes
    naive bayes classifier, compactclassificationnaivebayesnoyes
    neural network classifier, noyes
    svm for one-class and binary classificationclassificationsvm, compactclassificationsvmyesyes (see tips)
    binary decision tree for classification, compactclassificationtreeyesyes
    binary classification linear model for incremental learningnoyes
  • regression model object

    modelfull/compact model objectfixed-point code generation supportsingle-precision code generation support
    generalized linear model, noyes
    linear regression model for incremental learningnoyes
    linear regression model, noyes
    ensemble regression, , regressionbaggedensembleyesyes
    gaussian process regressionregressiongp, noyes (see tips)
    gaussian kernel regression for big datanoyes
    linear regression for high-dimensional dataregressionlinearnoyes
    neural network regression, noyes
    regression treeregressiontree, compactregressiontreeyesyes
    svm regressionregressionsvm, compactregressionsvmyesyes (see tips)
  • nearest neighbor searcher object

    modelmodel objectfixed-point code generation supportsingle-precision code generation support
    exhaustive nearest neighbor searchernono
    nearest neighbor searcher using kd-treenono
  • anomaly detection object

    modelmodel objectfixed-point code generation supportsingle-precision code generation support
    isolation forestnoyes

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

if the filename file exists, then savelearnerforcoder overwrites the file.

the extension of the filename file must be .mat. if filename has no extension, then savelearnerforcoder appends .mat.

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

example: 'svmmdl'

data types: char | string

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 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 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, and isolationforest, 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.

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

网站地图