Main Content

Generate function that defines data types for fixed-point code generation

To generate fixed-point C/C++ code for the `predict`

function
of a machine learning model, use `generateLearnerDataTypeFcn`

, `saveLearnerForCoder`

, `loadLearnerForCoder`

, and `codegen`

(MATLAB Coder).

After training a machine learning model, save the model using

`saveLearnerForCoder`

.Create a structure that defines fixed-point data types by using the function generated from

`generateLearnerDataTypeFcn`

.Define an entry-point function that loads the model by using both

`loadLearnerForCoder`

and the structure, and then calls the`predict`

function.Generate code using

`codegen`

, and then verify the generated code.

The `generateLearnerDataTypeFcn`

function requires Fixed-Point Designer™, and 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
`generateLearnerDataTypeFcn`

for the highlighted step.

`generateLearnerDataTypeFcn(`

generates a data type function
that defines fixed-point data types for the variables required to generate fixed-point C/C++
code for prediction of a machine learning model. `filename`

,`X`

)`filename`

stores the
machine learning model, and `X`

contains the predictor data for the
`predict`

function of the model.

Use the generated function to create a structure that defines fixed-point data types.
Then, use the structure as the input argument `T`

of `loadLearnerForCoder`

.

`generateLearnerDataTypeFcn(`

specifies additional options by using one or more name-value pair arguments. For example,
you can specify `filename`

,`X`

,`Name,Value`

)`'WordLength',32`

to use 32-bit word length for the
fixed-point data types.

To improve the precision of the generated fixed-point code, you can tune the fixed-point data types. Modify the fixed-point data types by updating the data type function (

`myMdl_datatype`

) and creating a new structure, and then regenerate the code using the new structure. You can update the`myMdl_datatype`

function in one of two ways:Regenerate the

`myMdl_datatype`

function by using`generateLearnerDataTypeFcn`

and its name-value pair arguments.Increase the word length by using the

`'WordLength'`

name-value pair argument.Decrease the safety margin by using the

`'PercentSafetyMargin'`

name-value pair argument.

If you increase the word length or decrease the safety margin, the software can propose a longer fraction length, and therefore, improve the precision of the generated code based on the given data set.

Manually modify the fixed-point data types in the function file (

`myMdl_datatype.m`

). For each variable, you can tune the word length and fraction length and specify fixed-point math settings using a`fimath`

(Fixed-Point Designer) object.

In the generated fixed-point code, a large number of operations or a large variable range can result in loss of precision, compared to the precision of the corresponding floating-point code. When training an SVM model, keep the following tips in mind to avoid loss of precision in the generated fixed-point code:

Data standardization (

`'Standardize'`

) — To avoid overflows in the model property values of support vectors in an SVM model, you can standardize the predictor data. Instead of using the`'Standardize'`

name-value pair argument when training the model, standardize the predictor data before passing the data to the fitting function and the`predict`

function so that the fixed-point code does not include the operations for the standardization.Kernel function (

`'KernelFunction'`

) — Using the Gaussian kernel or linear kernel is preferable to using a polynomial kernel. A polynomial kernel requires higher computational complexity than the other kernels, and the output of a polynomial kernel function is unbounded.Kernel scale (

`'KernelScale'`

) — Using a kernel scale requires additional operations if the value of`'KernelScale'`

is not 1.The prediction of a one-class classification problem might have loss of precision if the predicted class score values have a large range.

`loadLearnerForCoder`

| `saveLearnerForCoder`

| `buildInstrumentedMex`

(Fixed-Point Designer) | `fi`

(Fixed-Point Designer) | `showInstrumentationResults`

(Fixed-Point Designer) | `codegen`

(MATLAB Coder)