Code Generation fordlarray
A deep learning array stores data with optional data format labels for custom training loops, and enables functions to compute and use derivatives through automatic differentiation. To learn more about custom training loops, automatic differentiation, and deep learning arrays, seeDeep Learning Custom Training Loops(Deep Learning Toolbox).
Code generation supports both formatted and unformatted deep learning arrays.dlarray
objects containinggpuArrays
are also supported for code generation. When you use deep learning arrays with CPU and GPU code generation, adhere to these restrictions:
Definedlarray
for Code Generation
For code generation, use thedlarray
(Deep Learning Toolbox)函数create deep learning arrays. For example, suppose you have a pretraineddlnetwork
(Deep Learning Toolbox)network object in themynet.mat
MAT-file. To predict the responses for this network, create an entry-point function in MATLAB®.
There are two possibilities:
Note
For code generation, thedlarray
input to thepredict
method of thedlnetwork
object must besingle
data type.
Design 1 (Not recommended)
In this design example, the input and output to the entry-point function,foo
are ofdlarray
types. This type of entry-point function is not recommended for code generation because in MATLAB,dlarray
enforces the order of labels'SCBTU'
. This behavior is replicated for MEX code generation. However, for standalone code generation such as static, dynamic libraries, or executables, the data format follows the specification of thefmt
argument of thedlarray
object. As a result, if the input or output of an entry-point function is adlarray
object and its order of labels is not'SCBTU'
, then the data layout will be different between the MATLAB environment and standalone code.
functiondlOut = foo(dlIn)persistentdlnet;ifisempty(dlnet) dlnet = coder.loadDeepLearningNetwork('mynet.mat');enddlOut = predict(dlnet, dlIn);end
Design 2 (Recommended)
In this design example, the input and output tofoo
are of primitive datatypes and thedlarray
object is created within the function. Theextractdata
(Deep Learning Toolbox)method of thedlarray
object returns the data in thedlarray
dlA
as the output offoo
. The outputa
has the same data type as the underlying data type indlA
.
When compared toDesign 1
, this entry-point design has the following advantages:
Easier integration with standalone code generation workflows such as static, dynamic libraries, or executables.
The data format of the output from the
extractdata
function has the same order ('SCBTU'
) in both the MATLAB environment and the generated code.Improves performance for MEX workflows.
Simplifies Simulink®workflows usingMATLAB Functionblocks as Simulink does not natively support
dlarray
objects.
functiona = foo(in) dlIn = dlarray(in,'SSC');persistentdlnet;ifisempty(dlnet) dlnet = coder.loadDeepLearningNetwork('mynet.mat');enddlA = predict(dlnet, dlIn); a = extractdata(dlA);end
To see an example ofdlnetwork
anddlarray
usage withMATLAB Coder™, seeGenerate Digit Images Using Variational Autoencoder on Intel CPUs.
dlarray
Object Functions with Code Generation Support
For code generation, you are restricted to the deep learning array object functions listed in this table.
|
Dimension labels for |
|
Extract data from |
|
Find dimensions with specified label |
|
Remove |
Deep Learning Toolbox Functions withdlarray
Code Generation Support
Deep Learning Operations
Function | Description |
---|---|
fullyconnect (Deep Learning Toolbox) |
The fully connect operation multiplies the input by a weight matrix and then adds a bias vector. |
sigmoid (Deep Learning Toolbox) |
The sigmoid activation operation applies the sigmoid function to the input data. |
softmax (Deep Learning Toolbox) |
The softmax activation operation applies the softmax function to the channel dimension of the input data. |
MATLAB Functions withdlarray
Code Generation Support
Unary Element-wise Functions
Function | Notes and Limitations |
---|---|
abs |
The output |
atan2 |
The output |
cos |
|
cosh |
|
cot |
|
csc |
|
exp |
|
log |
|
sec |
The output |
sign |
|
sin |
|
sinh |
|
sqrt |
|
tan |
The output |
tanh |
|
uplus ,+ |
|
uminus ,- |
Binary Element-wise Operators
Function | Notes and Limitations |
---|---|
minus ,- |
If the two |
plus ,+ |
|
power ,.^ |
|
rdivide ,./ |
|
times ,.* |
Reduction Functions
Function | Notes and Limitations |
---|---|
mean |
|
prod |
|
sum |
Extrema Functions
Function | Notes and Limitations |
---|---|
ceil |
The output |
eps |
|
fix |
The output |
floor |
The output |
max |
|
min |
|
round |
|
Other Math Operations
Function | Notes and Limitations |
---|---|
colon ,: |
|
mtimes ,* |
|
pagemtimes |
|
逻辑加工ations
Function | Notes and Limitations |
---|---|
and ,& |
If the two |
eq ,== |
If the two |
ge ,>= |
|
gt ,> |
|
le ,<= |
|
lt ,< |
|
ne ,~= |
|
or ,| |
If the two |
xor |
Size Manipulation Functions
Function | Notes and Limitations |
---|---|
reshape |
The output For code generation, the size dimensions must be fixed size. |
squeeze |
Two-dimensional |
Transposition Operations
Function | Notes and Limitations |
---|---|
ctranspose ,' |
If the input |
permute |
If the input For code generation, the dimension order must be fixed size. |
ipermute |
If the input For code generation, the dimension order must be fixed size. |
transpose ,.' |
If the input |
Concatenation Functions
Function | Notes and Limitations |
---|---|
cat |
The For code generation, the dimension order to |
horzcat |
|
vertcat |
Conversion Functions
Function | Notes and Limitations |
---|---|
cast |
|
double |
输出是一个 |
logical |
输出是一个dlarray that contains data of typelogical . |
single |
输出是一个dlarray that contains data of typesingle . |
Comparison Functions
Function | Notes and Limitations |
---|---|
isequal |
|
isequaln |
|
Data Type and Value Identification Functions
Size Identification Functions
Function | Notes and Limitations |
---|---|
iscolumn |
这个函数返回true for adlarray that is a column vector, where each dimension except the first is a singleton. For example, a 3-by-1-by-1dlarray is a column vector. |
ismatrix |
这个函数返回true fordlarray objects with only two dimensions and fordlarray objects where each dimension except the first two is a singleton. For example, a 3-by-4-by-1dlarray is a matrix. |
isrow |
这个函数返回true for adlarray that is a row vector, where each dimension except the second is a singleton. For example, a 1-by-3-by-1dlarray is a row vector. |
isscalar |
N/A |
isvector |
这个函数返回true for adlarray that is a row vector or column vector. Note thatisvector does not consider a 1-by-1-by-3dlarray to be a vector. |
length |
N/A |
ndims |
If the input |
numel |
N/A |
size |
If the input |
Creator Functions
Indexing
Code generation supports indexingdlarray
objects and exhibits the following behaviors:
If you set
dlY(idx1,...,idxn) = dlX
, thendlY
anddlX
must be assignment compatible.Size of the data must not change. Out-of-bounds assignment operation is not supported.
The assignment statement cannot add or drop
U
labels.
Code generation does not support deleting of parts of a
dlarray
object by usingdlX(idx1,…,idxn) = []
.
See Also
Objects
Related Examples
More About
- dlarray Limitations for Code Generation
- Define Custom Training Loops, Loss Functions, and Networks(Deep Learning Toolbox)
- Train Network Using Custom Training Loop(Deep Learning Toolbox)
- Make Predictions Using dlnetwork Object(Deep Learning Toolbox)