main content

reconstruct model object from saved model for code generation -凯发k8网页登录

reconstruct model object from saved model 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.

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.

code generation workflow for the object function of a machine learning model. step 1: train a model. step 2: save the model. step 3 (highlighted): 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 loadlearnerforcoder for the highlighted step.

fixed-point code generation workflow. step 1: train a model. step 2: save the model. step 3: define the fixed-point data types. step 4 (highlighted): 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

mdl = loadlearnerforcoder(filename) reconstructs a model (mdl) from the model stored in the matlab formatted binary file (mat-file) named filename. you must create the filename file by using savelearnerforcoder.

example

mdl = loadlearnerforcoder(filename,'datatype','single') reconstructs a single-precision model (mdl) from the model stored in the matlab formatted binary file (mat-file) named filename.

example

mdl = loadlearnerforcoder(filename,'datatype',t) returns a fixed-point version of the model stored in 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

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 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 single-precision 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.

train model

load the fisheriris data set. create x as a numeric matrix that contains four petal measurements for 150 irises. create y as a cell array of character vectors that contains the corresponding iris species.

load fisheriris
x = meas;
y = species;

train a naive bayes classifier using predictors x and class labels y.

mdl = fitcnb(x,y);

mdl is a trained classificationnaivebayes classifier.

save model

save the naive bayes classification model to the file naivebayesiris.mat by using savelearnerforcoder.

savelearnerforcoder(mdl,'naivebayesiris');

define entry-point function

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

  • accept iris flower measurements with columns corresponding to petal measurements, and return predicted labels.

  • load a trained naive bayes classification model.

  • predict labels using the single-precision loaded classification model for the iris flower measurements.

type classifyirissingle.m
function label = classifyirissingle(x) %#codegen
% classifyirissingle classify iris species using single-precision naive
% bayes model
% classifyirissingle classifies the iris flower measurements in x using the
% single-precision naive bayes model in the file naivebayesiris.mat, and
% then returns the predicted labels in label.
mdl = loadlearnerforcoder('naivebayesiris','datatype','single');
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).

xpred = single(x);
codegen classifyirissingle -args xpred
code generation successful.

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

verify generated code

compare the labels classified using predict, classifyirissingle, and classifyirissingle_mex.

label1 = predict(mdl,x);
label2 = classifyirissingle(x);
label3 = classifyirissingle_mex(xpred);
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. if the generated mex function classifyirissingle_mex and the function predict do not produce the same classification results, you can compute the percentage of incorrectly classified labels.

sum(strcmp(label3,label1)==0)/numel(label1)*100
ans = 0

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

name of the mat-file that contains the structure array representing a model object, specified as a character vector or string scalar. you must create the filename file using savelearnerforcoder. loadlearnerforcoder reconstructs the model stored in the filename file at compile time.

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

if filename does not include a full path, then loadlearnerforcoder loads the file from the current folder.

the following tables show the models you can save using savelearnerforcoder and whether each model supports fixed-point and single-precision 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

example: 'mdl'

data types: char | string

fixed-point data types, specified as a structure. this argument is for fixed-point c/c code generation.

create t using a function generated by generatelearnerdatatypefcn. for details about the generated function and the structure t, see generatelearnerdatatypefcn and data type function.

you can use this argument when the model in the filename file is an svm model, a decision tree model, and an ensemble of decision trees.

data types: struct

output arguments

machine learning model, returned as one of these model objects:

limitations

  • when mdl is compactlinearmodelsuppose you train a linear model by using and specifying as a structure with an anonymous function handle for the robustwgtfun field, use savelearnerforcoder to save the model, and then use loadlearnerforcoder 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 is compactclassificationsvm or compactclassificationecocif you use savelearnerforcoder to save a model that is equipped to predict posterior probabilities, and use loadlearnerforcoder to load the model, then loadlearnerforcoder cannot restore the scoretransform property into the matlab workspace. however, loadlearnerforcoder can load the model, including the scoretransform 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 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.

extended capabilities

version history

introduced in r2019b

网站地图