Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

代码生成

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

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

您可以通过多种方式为这些函数生成 C/C++ 代码:

  • 对机器学习模型的对象函数使用 saveLearnerForCoderloadLearnerForCodercodegen (MATLAB Coder)

  • 对机器学习模型的 predictupdate 对象函数使用由 learnerCoderConfigurer 创建的编码器配置器。使用配置器配置代码生成选项,并在生成代码中更新模型参数。

  • 对于支持代码生成的其他函数,使用 codegen

您还可以生成定点 C/C++ 代码,用于预测一些机器学习模型。这种类型的代码生成需要 Fixed-Point Designer™。

要将机器学习模型的预测集成到 Simulink® 中,请使用 Statistics and Machine Learning Toolbox 库中的 MATLAB Function 模块或 Simulink 模块。

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

有关支持代码生成的函数的列表,请参阅函数列表(C/C++ 代码生成)

函数

全部展开

saveLearnerForCoderSave model object in file for code generation
loadLearnerForCoderReconstruct model object from saved model for code generation
generateLearnerDataTypeFcnGenerate function that defines data types for fixed-point 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

对象

全部展开

ClassificationTreeCoderConfigurerCoder configurer of binary decision tree model for multiclass classification
ClassificationSVMCoderConfigurerCoder configurer for support vector machine (SVM) for one-class and binary classification
ClassificationLinearCoderConfigurerCoder configurer for linear binary classification of high-dimensional data
ClassificationECOCCoderConfigurerCoder configurer for multiclass model using binary learners
RegressionTreeCoderConfigurerCoder configurer of binary decision tree model for regression
RegressionSVMCoderConfigurerCoder configurer for support vector machine (SVM) regression model
RegressionLinearCoderConfigurerCoder configurer for linear regression model with high-dimensional data

模块

ClassificationSVM PredictClassify observations using support vector machine (SVM) classifier for one-class and binary classification
RegressionSVM PredictPredict responses using support vector machine (SVM) regression model

主题

代码生成工作流

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.

Create Dummy Variables for Categorical Predictors and Generate C/C++ Code

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

Fixed-Point Code Generation for Prediction of SVM

Generate fixed-point code for the prediction of an SVM classification or regression model.

Code Generation for Probability Distribution Objects

Generate code that fits a probability distribution object to sample data and evaluates the fitted distribution object.

Generate Code to Classify Data in Table

Generate code for classifying data in a table containing numeric and categorical variables.

代码生成应用程序

Predict Responses Using RegressionSVM Predict Block

Train a support vector machine (SVM) regression model using the Regression Learner app, and then use the RegressionSVM Predict block for response prediction.

Predict Class Labels Using ClassificationSVM Predict Block

This example shows how to use the ClassificationSVM Predict block for label prediction in Simulink®.

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.

特色示例