Main Content

代码生成简介

马铃薯®编码器™从支持代码生成的统计信息和计算机学习工具箱™功能生成可读和便携式的C和C ++代码。万博1manbetx您可以将生成的代码集成到项目中作为源代码,静态库或动态库。您还可以使用MATLAB环境中生成的代码加速MATLAB代码的计算密集型部分。

Generating C/C++ code requiresMatlab编码器and has the following limitations:

  • You cannot call any function at the top level when generating code by usingCodegen.(MATLAB Coder)。Instead, call the function within anentry-point函数,然后从入口点函数生成代码。入学点函数,也称为顶层orprimary函数,是您为代码生成定义的函数。入口点函数中的所有功能必须支持代码生成。万博1manbetx

  • Matlab编码器限制也适用于代码生成的统计信息和机器学习工具箱。有关详细信息,请参阅马铃薯Language Features Supported for C/C++ Code Generation(MATLAB Coder)

  • Code generation in Statistics and Machine Learning Toolbox does not support sparse matrices and categorical arrays. Code generation supports numeric tables for most prediction functions.

  • For the code generation usage notes and limitations for each function, see the Code Generation section on the function reference page.

有关支持代码生成的统计信息和机器学习工具箱功能,请参阅万博1manbetx功能列表(C / C ++代码生成)

代码生成Workflows

You can generate C/C++ code for the Statistics and Machine Learning Toolbox functions in several ways.

  • 一般代码生成工作流,用于机器学习模型的对象功能

    Define anentry-point调用支持代码生成的函数的函数,通过使用,为入口功能生成C / C ++代码万博1manbetxCodegen.(MATLAB Coder),然后验证生成的代码。入学点函数,也称为顶层orprimary函数,是您为代码生成定义的函数。因为您无法使用顶级使用任何功能Codegen.,您必须定义一个入口点函数。入口点函数中的所有功能必须支持代码生成。万博1manbetx

    有关详细信息,请参阅一般代码生成工作流程

  • 代码生成工作流程object function (predict随机knnsearch., 要么rangesearch.)机器学习模型

    使用培训模型saveLearnerForCoder那and define an entry-point function that loads the saved model by usingloadlearnerforcoder.并调用对象函数。然后通过使用生成代码进行入口点功能Codegen.(MATLAB Coder)那and verify the generated code. The input arguments of the entry-point function cannot be classification or regression model objects. Therefore, you need to work around this limitation by usingsaveLearnerForCoderandloadlearnerforcoder.

    您还可以生成单精度C / C ++代码,以预测分类和回归的机器学习模型。对于单精度代码生成,请指定名称值对参数'Datatype','single'作为额外的输入loadlearnerforcoder.功能。

    有关详细信息,请参阅these examples

    You can also generate fixed-point C/C++ code for the prediction of a support vector machine (SVM) model, a decision tree model, and an ensemble of decision trees for classification and regression. This type of code generation requires Fixed-Point Designer™.

    固定点代码生成需要一个附加步骤,该步骤定义预测所需的变量的定点数据类型。使用由生成的数据类型功能创建固定点数据类型结构generateLearnerDataTypeFcn,并使用该结构作为输入参数loadlearnerforcoder.在一个入学点函数中。您还可以在生成代码之前优化固定点数据类型。

    有关详细信息,请参阅Fixed-Point Code Generation for Prediction of SVM

  • 代码生成工作流程predictand更新使用SVM或线性二进制学习者,树模型,SVM模型,线性模型或多款纠错输出代码(ECOC)分类模型的功能

    Create a coder configurer by usinglearnerCoderConfigurer那generate code by usinggenerateCode,然后验证生成的代码。你可以参看igure code generation options and specify the coder attributes of the model parameters using object properties. After you retrain the model with new data or settings, you can update model parameters in the generated C/C++ code without having to regenerate the code. This feature reduces the effort required to regenerate, redeploy, and reverify C/C++ code.

    有关详细信息,请参阅代码生成for Prediction and Update Using Coder Configurer

代码生成应用程序

将计算机学习模型的预测集成到Simulink中万博1manbetx®那use a MATLAB Function block or the Simulink blocks in the Statistics and Machine Learning Toolbox library. For details, see these examples:

Code generation for the Statistics and Machine Learning Toolbox functions also works with other toolboxes such as System object™ and Stateflow®,如上所述:

For more applications of code generation, see these examples:

也可以看看

||||(MATLAB Coder)

Related Topics