dlarray
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, see Deep Learning Custom Training Loops (Deep Learning Toolbox).
Code generation supports both formatted and unformatted deep learning arrays.
dlarray
objects containing gpuArrays
are also
supported for code generation. When you use deep learning arrays with CPU and GPU code
generation, adhere to these restrictions:
dlarray
for Code GenerationFor code generation, use the dlarray
(Deep Learning Toolbox)
function to create deep learning arrays. For example, suppose you have a pretrained
dlnetwork
(Deep Learning Toolbox) network object in the
mynet.mat
MATfile. To predict the responses for this network,
create an entrypoint function in MATLAB^{®}.
There are two possibilities:
Note
For code generation, the dlarray
input to the
predict
method of the dlnetwork
object must
be single
data type.
In this design example, the input and output to the entrypoint function,
foo
are of dlarray
types. This type of
entrypoint 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 the fmt
argument of the dlarray
object. As a result, if the input or output
of an entrypoint function is a dlarray
object and its order of
labels is not 'SCBTU'
, then the data layout will be different
between the MATLAB environment and standalone code.
function dlOut = foo(dlIn) persistent dlnet; if isempty(dlnet) dlnet = coder.loadDeepLearningNetwork('mynet.mat'); end dlOut = predict(dlnet, dlIn); end
In this design example, the input and output to foo
are of
primitive datatypes and the dlarray
object is created within the
function. The extractdata
(Deep Learning Toolbox) method of the dlarray
object returns
the data in the dlarray
dlA
as the output of foo
. The output
a
has the same data type as the underlying data type in
dlA
.
When compared to Design 1
, this entrypoint 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 using MATLAB Function blocks as
Simulink does not natively support dlarray
objects.
function a = foo(in) dlIn = dlarray(in, 'SSC'); persistent dlnet; if isempty(dlnet) dlnet = coder.loadDeepLearningNetwork('mynet.mat'); end dlA = predict(dlnet, dlIn); a = extractdata(dlA); end
To see an example of dlnetwork
and
dlarray
usage with MATLAB
Coder™, see Generate Digit Images Using Variational Autoencoder on Intel CPUs.
dlarray
Object Functions with Code Generation SupportFor 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 
dlarray
Code Generation SupportFunction  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. 
dlarray
Code Generation SupportFunction  Notes and Limitations 

abs  The output

cos  The output

cosh  
cot  
csc  
exp  
log 

sec  The output

sign  
sin  
sinh  
sqrt 

tan  The output

tanh  
uplus ,
+  
uminus ,
 
Function  Notes and Limitations 

ceil  The output

eps 

fix  The output

floor  The output

round 

Function  Notes and Limitations 

isequal 

isequaln 

Function  Notes and Limitations 

length  N/A 
ndims  If the input 
numel  N/A 
size  If the input 