Main Content

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.dlarrayobjects containinggpuArraysare also supported for code generation. When you use deep learning arrays with CPU and GPU code generation, adhere to these restrictions:

Definedlarrayfor 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.matMAT-file. To predict the responses for this network, create an entry-point function in MATLAB®.

There are two possibilities:

Note

For code generation, thedlarrayinput to thepredictmethod of thedlnetworkobject must besingledata type.

Design 1 (Not recommended)

In this design example, the input and output to the entry-point function,fooare ofdlarraytypes. This type of entry-point function is not recommended for code generation because in MATLAB,dlarrayenforces 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 thefmtargument of thedlarrayobject. As a result, if the input or output of an entry-point function is adlarrayobject 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 tofooare of primitive datatypes and thedlarrayobject is created within the function. Theextractdata(Deep Learning Toolbox)method of thedlarrayobject returns the data in thedlarraydlAas the output offoo. The outputahas 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 theextractdatafunction 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 supportdlarrayobjects.

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 ofdlnetworkanddlarrayusage withMATLAB Coder™, seeGenerate Digit Images Using Variational Autoencoder on Intel CPUs.

dlarrayObject 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 fordlarray

extractdata(Deep Learning Toolbox)

Extract data fromdlarray

finddim(Deep Learning Toolbox)

Find dimensions with specified label

stripdims(Deep Learning Toolbox)

Removedlarraylabels

Deep Learning Toolbox Functions withdlarrayCode 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 withdlarrayCode Generation Support

Unary Element-wise Functions

Function Notes and Limitations
abs

The outputdlarrayhas the same data format as the inputdlarray.

atan2

The outputdlarrayhas the same data format as the inputdlarray.

cos
cosh
cot
csc
exp
log

  • The outputdlarrayhas the same data format as the inputdlarray.

  • Becausedlarraydoes not support complex numbers, the inputdlarraymust have nonnegative values.

sec

The outputdlarrayhas the same data format as the inputdlarray.

sign
sin
sinh
sqrt

  • The outputdlarrayhas the same data format as the inputdlarray.

  • Becausedlarraydoes not support complex numbers, the inputdlarraymust have nonnegative values.

tan

The outputdlarrayhas the same data format as the inputdlarray.

tanh
uplus,+
uminus,-

Binary Element-wise Operators

Function Notes and Limitations
minus,-

If the twodlarrayinputs are formatted, then the outputdlarrayis formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, seeImplicit Expansion with Data Formats(Deep Learning Toolbox).

plus,+
power,.^
rdivide,./
times,.*

Reduction Functions

Function Notes and Limitations
mean
  • The outputdlarrayhas the same data format as the inputdlarray.

  • The'omitnan'option is not supported.

  • If the inputdlarrayis on the GPU, the'native'option is not supported.

prod
  • The outputdlarrayhas the same data format as the inputdlarray.

  • The'omitnan'option is not supported.

sum

Extrema Functions

Function Notes and Limitations
ceil

The outputdlarrayhas the same data format as the inputdlarray.

eps

  • The outputdlarrayhas the same data format as the inputdlarray.

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

fix

The outputdlarrayhas the same data format as the inputdlarray.

floor

The outputdlarrayhas the same data format as the inputdlarray.

max
  • When you find the maximum or minimum elements of a singledlarray, the outputdlarrayhas the same data format as the inputdlarray.

  • When you find the maximum or minimum elements between two formatteddlarrayinputs, the outputdlarrayhas a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, seeImplicit Expansion with Data Formats(Deep Learning Toolbox).

  • The index output argument is not traced and cannot be used with automatic differentiation. For more information, seeUse Automatic Differentiation In Deep Learning Toolbox(Deep Learning Toolbox).

min
round

  • Only the syntaxY = round(X)是支持万博1manbetx的。

  • The outputdlarrayhas the same data format as the inputdlarray.

Other Math Operations

Function Notes and Limitations
colon,:
  • The supported operations are:

    • a:b

    • a:b:c

    For information on indexing into adlarray, seeIndexing(Deep Learning Toolbox).

  • All inputs must be real scalars. The outputdlarrayis unformatted.

mtimes,*
  • One input can be a formatteddlarrayonly when the other input is an unformatted scalar. In this case, the outputdlarrayhas the same data format as the formatteddlarrayinput.

  • Multiplying adlarraywith a non-dlarraysparse matrix is supported only when both inputs are non-scalar.

pagemtimes
  • One input can be a formatteddlarrayonly when the other input is unformatted, with scalar pages. In this case, the outputdlarrayhas the same data format as the formatteddlarrayinput.

  • For code generation, each transpose option ofpagemtimesmust be constant.

逻辑加工ations

Function Notes and Limitations
and,&

If the twodlarrayinputs are formatted, then the outputdlarrayis formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, seeImplicit Expansion with Data Formats(Deep Learning Toolbox).

eq,==

If the twodlarrayinputs are formatted, then the outputdlarrayis formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, seeImplicit Expansion with Data Formats(Deep Learning Toolbox).

ge,>=
gt,>
le,<=
lt,<
ne,~=
or,|

If the twodlarrayinputs are formatted, then the outputdlarrayis formatted with a combination of both of their data formats. The function uses implicit expansion to combine the inputs. For more information, seeImplicit Expansion with Data Formats(Deep Learning Toolbox).

xor

Size Manipulation Functions

Function Notes and Limitations
reshape

The outputdlarrayis unformatted, even if the inputdlarrayis formatted.

For code generation, the size dimensions must be fixed size.

squeeze

Two-dimensionaldlarrayobjects are unaffected bysqueeze. If the inputdlarrayis formatted, the function removes dimension labels belonging to singleton dimensions. If the inputdlarrayhas more than two dimensions and its third and above dimensions are singleton, then the function discards these dimensions and their labels.

Transposition Operations

Function Notes and Limitations
ctranspose,'

If the inputdlarrayis formatted, then the labels of both dimensions must be the same. The function performs transposition implicitly, and transposes directly only if necessary for other operations.

permute

If the inputdlarrayis formatted, then the permutation must be among only those dimensions that have the same label. The function performs permutations implicitly, and permutes directly only if necessary for other operations.

For code generation, the dimension order must be fixed size.

ipermute

If the inputdlarrayis formatted, then the permutation must be among only those dimensions that have the same label. The function performs permutations implicitly, and permutes directly only if necessary for other operations.

For code generation, the dimension order must be fixed size.

transpose,.'

If the inputdlarrayis formatted, then the labels of both dimensions must be the same. The function performs transposition implicitly, and transposes directly only if necessary for other operations.

Concatenation Functions

Function Notes and Limitations
cat

Thedlarrayinputs must have matching formats or be unformatted. Mixed formatted and unformatted inputs are supported. If anydlarrayinputs are formatted, then the outputdlarrayis formatted with the same data format.

For code generation, the dimension order tocatfunction must be fixed size.

horzcat
vertcat

Conversion Functions

Function Notes and Limitations
cast
  • cast(dlA,newdatatype)copies the data in thedlarraydlAinto adlarrayof the underlying data typenewdatatype. Thenewdatatypeoption must be'double','single', or'logical'. The outputdlarrayis formatted with the same data format asdlA.

  • cast(A,'like',Y)returns an array of the same type asY. IfYis adlarray, then the output is adlarraythat has the same underlying data type asY. IfYis on the GPU, then the output is on the GPU. If bothAandYaredlarrayobjects, then the outputdlarrayis formatted with the same data format as the inputA.

double

输出是一个dlarraythat contains data of typedouble.

logical 输出是一个dlarraythat contains data of typelogical.
single 输出是一个dlarraythat contains data of typesingle.

Comparison Functions

Function Notes and Limitations
isequal

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

  • Twodlarrayinputs 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.

  • Twodlarrayinputs are equal if the numeric data they represent are equal (treatingNaNs as equal) and if they both are either formatted with the same data format or unformatted.

Data Type and Value Identification Functions

Function Notes and Limitations
isfloat

The software applies the function to the underlying data of an inputdlarray.

islogical
isnumeric
isreal

Becausedlarraydoes not support complex numbers, this function always returnstruefor adlarrayinput.

Size Identification Functions

Function Notes and Limitations
iscolumn 这个函数返回truefor adlarraythat is a column vector, where each dimension except the first is a singleton. For example, a 3-by-1-by-1dlarrayis a column vector.
ismatrix 这个函数返回truefordlarrayobjects with only two dimensions and fordlarrayobjects where each dimension except the first two is a singleton. For example, a 3-by-4-by-1dlarrayis a matrix.
isrow 这个函数返回truefor adlarraythat is a row vector, where each dimension except the second is a singleton. For example, a 1-by-3-by-1dlarrayis a row vector.
isscalar N/A
isvector 这个函数返回truefor adlarraythat is a row vector or column vector. Note thatisvectordoes not consider a 1-by-1-by-3dlarrayto be a vector.
length N/A
ndims

If the inputdlarraydlXis formatted, thenndims(dlX)returns the number of dimension labels, even if some of the labeled dimensions are trailing singleton dimensions.

numel N/A
size

If the inputdlarraydlXis formatted, thensize(dlX)返回一个向量的长度等于的数量dimension labels, even if some of the labeled dimensions are trailing singleton dimensions.

Creator Functions

Function Notes and Limitations
false Only the'like'syntax is supported fordlarray.
inf
nan
ones
rand
true
zeros

Indexing

Code generation supports indexingdlarrayobjects and exhibits the following behaviors:

  • If you setdlY(idx1,...,idxn) = dlX, thendlYanddlXmust be assignment compatible.

    • Size of the data must not change. Out-of-bounds assignment operation is not supported.

    • The assignment statement cannot add or dropUlabels.

  • Code generation does not support deleting of parts of adlarrayobject by usingdlX(idx1,…,idxn) = [].

See Also

Objects

Related Examples

More About