generate function that defines data types for fixed-凯发k8网页登录
generate function that defines data types for fixed-point code generation
since r2019b
description
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 thepredict
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.
examples
input arguments
more about
tips
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 themymdl_datatype
function in one of two ways:regenerate the
mymdl_datatype
function by usinggeneratelearnerdatatypefcn
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 (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 thepredict
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.
you can generate a lookup table that approximates a score transformation function of a trained classifier by using a (fixed-point designer) object and its function (fixed-point designer). then use the lookup table for fixed-point code generation. this approach requires fewer calculations for score transformation in the generated code than the default approach, which uses the cordic-based algorithm. therefore, using a lookup table yields relatively high-speed performance and relatively low memory requirements. the supported score transformation functions include
'doublelogit'
,'logit'
, and'symmetriclogit'
. for an example, see use lookup table to approximate score transformation. (since r2023a)
version history
introduced in r2019bsee also
loadlearnerforcoder
| savelearnerforcoder
| buildinstrumentedmex
(fixed-point designer) | showinstrumentationresults
(fixed-point designer) | codegen
(matlab coder) | fi
(fixed-point designer)