Main Content

dlarray

Deep learning array for customization

Since R2019b

Description

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.

Tip

For most deep learning tasks, you can use a pretrained neural network and adapt it to your own data. For an example showing how to use transfer learning to retrain a convolutional neural network to classify a new set of images, see Train Deep Learning Network to Classify New Images. Alternatively, you can create and train neural networks from scratch using the trainnet, trainNetwork, and trainingOptions functions.

If the trainingOptions function does not provide the training options that you need for your task, then you can create a custom training loop using automatic differentiation. To learn more, see Define Deep Learning Network for Custom Training Loops.

Creation

Description

example

dlX = dlarray(X) returns a dlarray object representing X. If X is a dlarray, dlX is a copy of X.

example

dlX = dlarray(X,fmt) formats the data in dlX with dimension labels according to the data format in fmt. Dimension labels help in passing deep learning data between functions. For more information on dimension labels, see Usage. If X is a formatted dlarray, then fmt replaces the existing format.

example

dlX = dlarray(v,dim) accepts a vector v and a single character format dim, and returns a column vector dlarray. The first dimension of dlX has the dimension label dim, and the second (singleton) dimension has the dimension label 'U'.

Input Arguments

expand all

Data array, specified as a numeric array of data type double or single, a logical array, a gpuArray object, or a dlarray object. X must be full, not sparse.

Example: rand(31*23,23)

Data Types: single | double | logical
Complex Number Support: Yes

Data format, specified as a string scalar or a character vector. Each character in the string must be one of the following dimension labels:

  • "S" — Spatial

  • "C" — Channel

  • "B" — Batch

  • "T" — Time

  • "U" — Unspecified

You can specify multiple dimensions labeled "S" or "U". You can use the labels "C", "B", and "T" at most once. The software ignores singleton trailing "U" dimensions located after the second dimension.

When you create a formatted dlarray object. The software automatically permutes the dimensions such that the format has dimensions in this order:

  • "S"

  • "C"

  • "B"

  • "T"

  • "U"

For example, if you specify a format of "TCB" (time, channel, batch), then the software automatically permutes the dimensions so that it has format "CBT" (channel, batch, time).

fmt must contain at least the same number of dimension labels as the number of dimensions of the input data. If you specify more than that number of dimension labels, dlarray creates empty (singleton) dimensions for the additional labels.

The following table indicates recommended data formats for common types of data.

Data Example
ShapeData Format
2-D images

h-by-w-by-c-by-n numeric array, where h, w, c and n are the height, width, number of channels of the images, and number of observations, respectively.

"SSCB"
3-D imagesh-by-w-by-d-by-c-by-n numeric array, where h, w, d, c and n are the height, width, depth, number of channels of the images, and number of image observations, respectively."SSSCB"
Vector sequences

c-by-s-by-n matrix, where c is the number of features of the sequence, s is the sequence length, and n is the number of sequence observations.

"CTB"
2-D image sequences

h-by-w-by-c-by-s-by-n array, where h, w, and c correspond to the height, width, and number of channels of the image, respectively, s is the sequence length, and n is the number of image sequence observations.

"SSCTB"
3-D image sequences

h-by-w-by-d-by-c-by-s-by-n array, where h, w, d, and c correspond to the height, width, depth, and number of channels of the image, respectively, s is the sequence length, and n is the number of image sequence observations.

"SSSCTB"
Featuresc-by-n array, where c is the number of features, and n is the number of observations."CB"

Data vector, specified as a numeric vector of data type double or single, logical vector, or dlarray vector object. Here, "vector" means any array with exactly one nonsingleton dimension.

Example: rand(100,1)

Dimension label, specified as a single character of the type allowed for fmt.

Example: "S"

Example: 'S'

Output Arguments

expand all

Deep learning array, returned as a dlarray object. dlX enables automatic differentiation using dlgradient and dlfeval. If you supply the fmt argument, dlX has labels.

  • If X is a numeric or logical array, dlX contains its data, possibly reordered because of the data format in fmt.

  • If X is a gpuArray, the data in dlX is also on the GPU. Subsequent calculations using dlX are performed on the GPU.

Usage

dlarray data formats enable you to execute the functions in the following table with assurance that the data has the appropriate shape.

FunctionOperationValidates Input DimensionAffects Size of Input Dimension
avgpoolCompute the average of the input data over moving rectangular (or cuboidal) spatial ('S') regions defined by a pool size parameter.'S''S'
batchnormNormalize the values contained in each channel ('C') of the input data.'C' 
crossentropyCompute the cross-entropy between estimates and target values, averaged by the size of the batch ('B') dimension.'S', 'C', 'B', 'T', 'U' (Estimates and target arrays must have the same sizes.)'S', 'C', 'B', 'T', 'U' (Output is an unformatted scalar.)
dlconvCompute the deep learning convolution of the input data using an array of filters, matching the number of spatial ('S') and (a function of the) channel ('C') dimensions of the input, and adding a constant bias.'S', 'C''S', 'C'
dltranspconvCompute the deep learning transposed convolution of the input data using an array of filters, matching the number of spatial ('S') and (a function of the) channel ('C') dimensions of the input, and adding a constant bias.'S', 'C''S', 'C'
fullyconnectCompute a weighted sum of the input data and apply a bias for each batch ('B') and time ('T') dimension.'S', 'C', 'U''S', 'C', 'B', 'T', 'U' (Output always has data format 'CB', 'CT', or 'CTB'.)
gru

Apply a gated recurrent unit calculation to the input data.

'S', 'C', 'T''C'
lstm

Apply a long short-term memory calculation to the input data.

'S', 'C', 'T''C'
maxpoolCompute the maximum of the input data over moving rectangular spatial ('S') regions defined by a pool size parameter.'S''S'
maxunpoolCompute the unpooling operation over the spatial ('S') dimensions.'S''S'
mseCompute the half mean squared error between estimates and target values, averaged by the size of the batch ('B') dimension.'S', 'C', 'B', 'T', 'U' (Estimates and target arrays must have the same sizes.)'S', 'C', 'B', 'T', 'U' (Output is an unformatted scalar.)
softmaxApply the softmax activation to each channel ('C') of the input data.'C' 

These functions require each dimension to have a label. You can specify the dimension label format by providing the first input as a formatted dlarray, or by using the 'DataFormat' name-value argument of the function.

dlarray enforces the dimension label ordering of 'SCBTU'. This enforcement eliminates ambiguous semantics in operations which implicitly match labels between inputs. dlarray also enforces that the dimension labels 'C', 'B', and 'T' can each appear at most once. The functions that use these dimension labels accept at most one dimension for each label.

dlarray provides functions for obtaining the data format associated with a dlarray (dims), removing the data format (stripdims), and obtaining the dimensions associated with specific dimension labels (finddim).

For more information on how a dlarray behaves with formats, see Notable dlarray Behaviors.

Object Functions

avgpoolPool data to average values over spatial dimensions
batchnormNormalize data across all observations for each channel independently
crossentropyCross-entropy loss for classification tasks
dimsDimension labels of dlarray
dlconvDeep learning convolution
dlgradientCompute gradients for custom training loops using automatic differentiation
dltranspconvDeep learning transposed convolution
extractdataExtract data from dlarray
finddimFind dimensions with specified label
fullyconnectSum all weighted input data and apply a bias
gruGated recurrent unit
leakyreluApply leaky rectified linear unit activation
lstmLong short-term memory
maxpoolPool data to maximum value
maxunpoolUnpool the output of a maximum pooling operation
mseHalf mean squared error
reluApply rectified linear unit activation
sigmoidApply sigmoid activation
softmaxApply softmax activation to channel dimension
stripdimsRemove dlarray data format

A dlarray also allows functions for numeric, matrix, and other operations. See the full list in List of Functions with dlarray Support.

Examples

collapse all

Create an unformatted dlarray from a matrix.

X = randn(3,5);
dlX = dlarray(X)
dlX = 
  3x5 dlarray

    0.5377    0.8622   -0.4336    2.7694    0.7254
    1.8339    0.3188    0.3426   -1.3499   -0.0631
   -2.2588   -1.3077    3.5784    3.0349    0.7147

Create a dlarray that has a data format containing the dimension labels 'S' and 'C'.

X = randn(3,5);
dlX = dlarray(X,'SC')
dlX = 
  3(S) x 5(C) dlarray

    0.5377    0.8622   -0.4336    2.7694    0.7254
    1.8339    0.3188    0.3426   -1.3499   -0.0631
   -2.2588   -1.3077    3.5784    3.0349    0.7147

If you specify the dimension labels in the opposite order, dlarray implicitly reorders the underlying data.

dlX = dlarray(X,'CS')
dlX = 
  5(S) x 3(C) dlarray

    0.5377    1.8339   -2.2588
    0.8622    0.3188   -1.3077
   -0.4336    0.3426    3.5784
    2.7694   -1.3499    3.0349
    0.7254   -0.0631    0.7147

Create a dlarray vector with the first dimension label 'T'. The second dimension label, which dlarray creates automatically, is 'U'.

X = randn(6,1);
dlX = dlarray(X,'T')
dlX = 
  6(T) x 1(U) dlarray

    0.5377
    1.8339
   -2.2588
    0.8622
    0.3188
   -1.3077

If you specify a row vector for X, dlarray implicitly reorders the result to be a column vector.

X = X';
dlX = dlarray(X,'T')
dlX = 
  6(T) x 1(U) dlarray

    0.5377
    1.8339
   -2.2588
    0.8622
    0.3188
   -1.3077

Tips

Extended Capabilities

Version History

Introduced in R2019b

expand all