本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。

代码生成

为 Statistics and Machine Learning Toolbox™ 函数生成 C/C++ 代码和 MEX 函数

MATLAB® Coder™ 可从支持代码生成的 Statistics and Machine Learning Toolbox 函数中生成可读且可移植的 C 代码和 C++ 代码。例如,您可以利用代码生成,将经过训练的支持向量机 (SVM) 分类模型部署到不能运行 MATLAB 的硬件设备上,在这些硬件设备上对新观测值进行分类。

您可以通过多种方式为 Statistics and Machine Learning Toolbox 函数生成 C/C++ 代码。

  • 机器学习模型的对象函数(predictrandomknnsearchrangesearch)的代码生成 - 使用 saveCompactModelloadCompactModelcodegen。使用 saveCompactModel 保存经过训练的模型。定义一个入口函数,它使用 loadCompactModel 加载保存的模型并调用对象函数。然后使用 codegen 为入口函数生成代码。

  • SVM 模型或多类纠错输出编码 (ECOC) 分类模型(使用 SVM 二类学习器)的 predictupdate 函数的代码生成 - 使用 learnerCoderConfigurer 创建编码器配置器,然后使用 generateCode 生成代码。您可以在生成的 C/C++ 代码中更新模型参数,而无需重新生成代码。

  • 其他支持代码生成的函数 - 使用 codegen。定义一个入口函数,它调用支持代码生成的函数。然后使用 codegen 为入口函数生成 C/C++ 代码。

要了解代码生成,请参阅Introduction to Code Generation

函数

全部展开

saveCompactModelSave model object in file for code generation
loadCompactModelReconstruct model object from saved model for code generation

创建编码器配置器对象

learnerCoderConfigurerCreate coder configurer of machine learning model

使用编码器配置器对象

generateCodeGenerate C/C++ code using coder configurer
generateFilesGenerate MATLAB files for code generation using coder configurer
validatedUpdateInputsValidate and extract machine learning model parameters to update
updateUpdate model parameters for code generation

对象

RegressionSVMCoderConfigurerCoder configurer for support vector machine (SVM) regression model
ClassificationSVMCoderConfigurerCoder configurer for support vector machine (SVM) for one-class and binary classification
ClassificationECOCCoderConfigurerCoder configurer for multiclass model using binary learners

主题

支持代码生成的函数

Code Generation Support, Usage Notes, and Limitations

View code generation usage notes, limitations, and the list of code-generation-enabled Statistics and Machine Learning Toolbox functions.

代码生成工作流

Introduction to Code Generation

Learn how to generate C/C++ code for Statistics and Machine Learning Toolbox functions.

General Code Generation Workflow

Generate code for Statistics and Machine Learning Toolbox functions that do not use machine learning model objects.

Code Generation for Prediction of Machine Learning Model at Command Line

Generate code for the prediction of a classification or regression model at the command line.

Code Generation for Prediction of Machine Learning Model Using MATLAB Coder App

Generate code for the prediction of a classification or regression model by using the MATLAB Coder app.

Code Generation for Prediction and Update Using Coder Configurer

Generate code for the prediction of a model using a coder configurer, and update model parameters in the generated code.

Code Generation and Classification Learner App

Train a classification model using the Classification Learner app, and generate C/C++ code for prediction.

Code Generation for Nearest Neighbor Searcher

Generate code for finding nearest neighbors using a nearest neighbor searcher model.

Specify Variable-Size Arguments for Code Generation

Generate code that accepts input arguments whose size might change at run time.

Train SVM Classifier with Categorical Predictors and Generate C/C++ Code

Convert categorical predictors to numeric dummy variables before fitting an SVM classifier and generating code.

代码生成应用程序

Predict Class Labels Using MATLAB Function Block

Generate code from a Simulink® model that classifies data using an SVM model.

System Objects for Classification and Code Generation

Generate code from a System object™ for making predictions using a trained classification model, and use the System object in a Simulink model.

Predict Class Labels Using Stateflow

Generate code from a Stateflow® model that classifies data using a discriminant analysis classifier.

特色示例