Main Content

Code Generation for 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:

Define dlarray for Code Generation

For 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 MAT-file. To predict the responses for this network, create an entry-point 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.

Design 1 (Not recommended)

In this design example, the input and output to the entry-point function, foo are of dlarray 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 the fmt argument of the dlarray object. As a result, if the input or output of an entry-point 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

Design 2 (Recommended)

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 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 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 GPU Coder™, see Generate Digit Images on NVIDIA GPU Using Variational Autoencoder.

dlarray Object Functions with Code Generation Support

For code generation, you are restricted to the deep learning array object functions listed in this table.

dims (Deep Learning Toolbox)

Dimension labels for dlarray

extractdata (Deep Learning Toolbox)

Extract data from dlarray

finddim (Deep Learning Toolbox)

Find dimensions with specified label

stripdims (Deep Learning Toolbox)

Remove dlarray labels

Deep Learning Toolbox Functions with dlarray Code Generation Support

Deep Learning Operations

FunctionDescription
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 with dlarray Code Generation Support

Unary Element-wise Functions

FunctionNotes and Limitations
abs

The output dlarray has the same data format as the input dlarray.

cos

The output dlarray has the same data format as the input dlarray.

cosh
cot
csc
exp
log

  • The output dlarray has the same data format as the input dlarray.

  • Because dlarray does not support complex numbers, the input dlarray must have nonnegative values.

sec

The output dlarray has the same data format as the input dlarray.

sign
sin
sinh
sqrt

  • The output dlarray has the same data format as the input dlarray.

  • Because dlarray does not support complex numbers, the input dlarray must have nonnegative values.

tan

The output dlarray has the same data format as the input dlarray.

tanh
uplus, +
uminus, -

Extrema Functions

FunctionNotes and Limitations
ceil

The output dlarray has the same data format as the input dlarray.

eps

  • The output dlarray has the same data format as the input dlarray.

  • Use eps(ones(‘like’, x)) to get a scalar epsilon value based on the data type of a dlarray x.

fix

The output dlarray has the same data format as the input dlarray.

floor

The output dlarray has the same data format as the input dlarray.

round

  • Only the syntax Y = round(X) is supported.

  • The output dlarray has the same data format as the input dlarray.

Conversion Functions

FunctionNotes and Limitations
double

The output is a dlarray that contains data of type double.

logicalThe output is a dlarray that contains data of type logical.
singleThe output is a dlarray that contains data of type single.

Comparison Functions

FunctionNotes and Limitations
isequal

  • The syntax with more than two input arguments is not supported.

  • Two dlarray inputs are equal if the numeric data they represent are equal and if they both are either formatted with the same data format or unformatted.

isequaln

  • The syntax with more than two input arguments is not supported.

  • Two dlarray inputs are equal if the numeric data they represent are equal (treating NaNs as equal) and if they both are either formatted with the same data format or unformatted.

Data Type and Value Identification Functions

FunctionNotes and Limitations
isfloat

The software applies the function to the underlying data of an input dlarray.

islogical
isnumeric
isreal

Because dlarray does not support complex numbers, this function always returns true for a dlarray input.

Size Identification Functions

FunctionNotes and Limitations
lengthN/A
ndims

If the input dlarray dlX is formatted, then ndims(dlX) returns the number of dimension labels, even if some of the labeled dimensions are trailing singleton dimensions.

numelN/A
size

If the input dlarray dlX is formatted, then size(dlX) returns a vector of length equal to the number of dimension labels, even if some of the labeled dimensions are trailing singleton dimensions.

Creator Functions

FunctionNotes and Limitations
falseOnly the 'like' syntax is supported for dlarray.
inf
nan
ones
rand
true
zeros

See Also

Objects

Related Examples

More About