BiLSTMLayer - Bidirectional long short-term memory (BiLSTM) layer for recurrent neural network

        (RNN) - MATLAB ([original](https://in.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.bilstmlayer.html)) ([raw](?raw))

Bidirectional long short-term memory (BiLSTM) layer for recurrent neural network (RNN)

Description

A bidirectional LSTM (BiLSTM) layer is an RNN layer that learns bidirectional long-term dependencies between time steps of time-series or sequence data. These dependencies can be useful when you want the RNN to learn from the complete time series at each time step.

Creation

Syntax

Description

`layer` = bilstmLayer(`numHiddenUnits`) creates a bidirectional LSTM layer and sets the NumHiddenUnits property.

Properties

expand all

BiLSTM

Number of hidden units (also known as the hidden size), specified as a positive integer.

The number of hidden units corresponds to the amount of information that the layer remembers between time steps (the hidden state). The hidden state can contain information from all the previous time steps, regardless of the sequence length. If the number of hidden units is too large, then the layer can overfit to the training data. The hidden state does not limit the number of time steps that the layer processes in an iteration.

The layer outputs data with 2*NumHiddenUnits channels.

To set this property, use the numHiddenUnits argument when you create the BiLSTMLayer object. After you create a BiLSTMLayer object, this property is read-only.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Output mode, specified as one of these values:

The BiLSTMLayer object stores this property as a character vector.

To set this property, use the corresponding name-value argument when you create the BiLSTMLayer object. After you create a BiLSTMLayer object, this property is read-only.

This property is read-only.

Flag for state inputs to the layer, specified as 0 (false) or 1 (true).

If the HasStateInputs property is 0 (false), then the layer has one input with the name"in", which corresponds to the input data. In this case, the layer uses the HiddenState and CellState properties for the layer operation.

If the HasStateInputs property is 1 (true), then the layer has three inputs with the names"in", "hidden", and "cell", which correspond to the input data, hidden state, and cell state, respectively. In this case, the layer uses the values passed to these inputs for the layer operation. If HasStateInputs is 1 (true), then the HiddenState andCellState properties must be empty.

This property is read-only.

Flag for state outputs from the layer, specified as0 (false) or1 (true).

If the HasStateOutputs property is 0 (false), then the layer has one output with the name"out", which corresponds to the output data.

If the HasStateOutputs property is 1 (true), then the layer has three outputs with the names"out", "hidden", and"cell", which correspond to the output data, hidden state, and cell state, respectively. In this case, the layer also outputs the state values that it computes.

This property is read-only.

Input size, specified as a positive integer or "auto". IfInputSize is "auto", then the software automatically assigns the input size at training time.

If InputSize is "auto", then theBiLSTMLayer object stores this property as a character vector.

Data Types: double | char | string

Activations

Activation function to update the cell and hidden state, specified as one of these values:

The software uses this option as the function σc in the calculations to update the cell and hidden state.

The BiLSTMLayer object stores this property as a character vector.

Activation function to apply to the gates, specified as one of these values:

The software uses this option as the function σg in the calculations for the layer gates.

The BiLSTMLayer object stores this property as a character vector.

To set this property, use the corresponding name-value argument when you create the BiLSTMLayer object. After you create a BiLSTMLayer object, this property is read-only.

State

Cell state to use in the layer operation, specified as a2*NumHiddenUnits-by-1 numeric vector. This value corresponds to the initial cell state when data is passed to the layer.

After setting this property manually, calls to theresetState function set the cell state to this value.

If HasStateInputs istrue, then the CellState property must be empty.

Data Types: single | double

Hidden state to use in the layer operation, specified as a2*NumHiddenUnits-by-1 numeric vector. This value corresponds to the initial hidden state when data is passed to the layer.

After setting this property manually, calls to theresetState function set the hidden state to this value.

If HasStateInputs istrue, then the HiddenState property must be empty.

Data Types: single | double

Parameters and Initialization

Function to initialize the input weights, specified as one of the following:

The layer only initializes the input weights when theInputWeights property is empty.

Data Types: char | string | function_handle

Function to initialize the recurrent weights, specified as one of the following:

The layer only initializes the recurrent weights when theRecurrentWeights property is empty.

Data Types: char | string | function_handle

Function to initialize the bias, specified as one of these values:

The layer only initializes the bias when the Bias property is empty.

The BiLSTMLayer object stores this property as a character vector or a function handle.

Data Types: char | string | function_handle

Input weights, specified as a matrix.

The input weight matrix is a concatenation of the eight input weight matrices for the components (gates) in the bidirectional LSTM layer. The eight matrices are concatenated vertically in the following order:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

The input weights are learnable parameters. When you train a neural network using the trainnet function, if InputWeights is nonempty, then the software uses theInputWeights property as the initial value. If InputWeights is empty, then the software uses the initializer specified by InputWeightsInitializer.

At training time, InputWeights is an 8*NumHiddenUnits-by-InputSize matrix.

Data Types: single | double

Recurrent weights, specified as a matrix.

The recurrent weight matrix is a concatenation of the eight recurrent weight matrices for the components (gates) in the bidirectional LSTM layer. The eight matrices are concatenated vertically in the following order:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

The recurrent weights are learnable parameters. When you train an RNN using the trainnet function, if RecurrentWeights is nonempty, then the software uses theRecurrentWeights property as the initial value. IfRecurrentWeights is empty, then the software uses the initializer specified by RecurrentWeightsInitializer.

At training time, RecurrentWeights is an8*NumHiddenUnits-by-NumHiddenUnits matrix.

Data Types: single | double

Layer biases, specified as a numeric vector.

The bias vector is a concatenation of the eight bias vectors for the components (gates) in the bidirectional LSTM layer. The eight vectors are concatenated vertically in the following order:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

The layer biases are learnable parameters. When you train a neural network, if Bias is nonempty, then the trainnet function uses the Bias property as the initial value. IfBias is empty, then software uses the initializer specified by BiasInitializer.

At training time, Bias is an8*NumHiddenUnits-by-1 numeric vector.

Data Types: single | double

Learning Rate and Regularization

Learning rate factor for the input weights, specified as a numeric scalar or a 1-by-8 numeric vector.

The software multiplies this factor by the global learning rate to determine the learning rate factor for the input weights of the layer. For example, ifInputWeightsLearnRateFactor is 2, then the learning rate factor for the input weights of the layer is twice the current global learning rate. The software determines the global learning rate based on the settings you specify with thetrainingOptions function.

To control the value of the learning rate factor for the four individual matrices in InputWeights, assign a 1-by-8 vector, where the entries correspond to the learning rate factor of the following:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

To specify the same value for all the matrices, specify a nonnegative scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Learning rate factor for the recurrent weights, specified as a numeric scalar or a 1-by-8 numeric vector.

The software multiplies this factor by the global learning rate to determine the learning rate for the recurrent weights of the layer. For example, ifRecurrentWeightsLearnRateFactor is 2, then the learning rate for the recurrent weights of the layer is twice the current global learning rate. The software determines the global learning rate based on the settings you specify using thetrainingOptions function.

To control the value of the learn rate for the four individual matrices in RecurrentWeights, assign a 1-by-8 vector, where the entries correspond to the learning rate factor of the following:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

To specify the same value for all the matrices, specify a nonnegative scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Learning rate factor for the biases, specified as a nonnegative scalar or a 1-by-8 numeric vector.

The software multiplies this factor by the global learning rate to determine the learning rate for the biases in this layer. For example, if BiasLearnRateFactor is 2, then the learning rate for the biases in the layer is twice the current global learning rate. The software determines the global learning rate based on the settings you specify using the trainingOptions function.

To control the value of the learning rate factor for the four individual matrices in Bias, assign a 1-by-8 vector, where the entries correspond to the learning rate factor of the following:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

To specify the same value for all the matrices, specify a nonnegative scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

L2 regularization factor for the input weights, specified as a numeric scalar or a 1-by-8 numeric vector.

The software multiplies this factor by the global_L2_ regularization factor to determine the_L2_ regularization factor for the input weights of the layer. For example, if InputWeightsL2Factor is 2, then the L2 regularization factor for the input weights of the layer is twice the current global L2 regularization factor. The software determines the L2 regularization factor based on the settings you specify using the trainingOptions function.

To control the value of the L2 regularization factor for the four individual matrices in InputWeights, assign a 1-by-8 vector, where the entries correspond to the L2 regularization factor of the following:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

To specify the same value for all the matrices, specify a nonnegative scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

L2 regularization factor for the recurrent weights, specified as a numeric scalar or a 1-by-8 numeric vector.

The software multiplies this factor by the global_L2_ regularization factor to determine the_L2_ regularization factor for the recurrent weights of the layer. For example, if RecurrentWeightsL2Factor is2, then the L2 regularization factor for the recurrent weights of the layer is twice the current global_L2_ regularization factor. The software determines the L2 regularization factor based on the settings you specify using the trainingOptions function.

To control the value of the L2 regularization factor for the four individual matrices in RecurrentWeights, assign a 1-by-8 vector, where the entries correspond to the L2 regularization factor of the following:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

To specify the same value for all the matrices, specify a nonnegative scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

L2 regularization factor for the biases, specified as a nonnegative scalar.

The software multiplies this factor by the global L2 regularization factor to determine the L2 regularization for the biases in this layer. For example, if BiasL2Factor is 2, then the L2 regularization for the biases in this layer is twice the global L2 regularization factor. The software determines the global L2 regularization factor based on the settings you specify using the trainingOptions function.

To control the value of the L2 regularization factor for the four individual matrices in Bias, assign a 1-by-8 vector, where the entries correspond to the L2 regularization factor of the following:

  1. Input gate (Forward)
  2. Forget gate (Forward)
  3. Cell candidate (Forward)
  4. Output gate (Forward)
  5. Input gate (Backward)
  6. Forget gate (Backward)
  7. Cell candidate (Backward)
  8. Output gate (Backward)

To specify the same value for all the matrices, specify a nonnegative scalar.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Layer

Data Types: char | string

This property is read-only.

Number of inputs to the layer.

If the HasStateInputs property is 0 (false), then the layer has one input with the name"in", which corresponds to the input data. In this case, the layer uses the HiddenState and CellState properties for the layer operation.

If the HasStateInputs property is 1 (true), then the layer has three inputs with the names"in", "hidden", and "cell", which correspond to the input data, hidden state, and cell state, respectively. In this case, the layer uses the values passed to these inputs for the layer operation. If HasStateInputs is 1 (true), then the HiddenState andCellState properties must be empty.

Data Types: double

This property is read-only.

Input names of the layer.

If the HasStateInputs property is 0 (false), then the layer has one input with the name"in", which corresponds to the input data. In this case, the layer uses the HiddenState and CellState properties for the layer operation.

If the HasStateInputs property is 1 (true), then the layer has three inputs with the names"in", "hidden", and "cell", which correspond to the input data, hidden state, and cell state, respectively. In this case, the layer uses the values passed to these inputs for the layer operation. If HasStateInputs is 1 (true), then the HiddenState andCellState properties must be empty.

The BiLSTMLayer object stores this property as a cell array of character vectors.

This property is read-only.

Number of outputs to the layer.

If the HasStateOutputs property is 0 (false), then the layer has one output with the name"out", which corresponds to the output data.

If the HasStateOutputs property is 1 (true), then the layer has three outputs with the names"out", "hidden", and"cell", which correspond to the output data, hidden state, and cell state, respectively. In this case, the layer also outputs the state values that it computes.

Data Types: double

This property is read-only.

Output names of the layer.

If the HasStateOutputs property is 0 (false), then the layer has one output with the name"out", which corresponds to the output data.

If the HasStateOutputs property is 1 (true), then the layer has three outputs with the names"out", "hidden", and"cell", which correspond to the output data, hidden state, and cell state, respectively. In this case, the layer also outputs the state values that it computes.

The BiLSTMLayer object stores this property as a cell array of character vectors.

Examples

collapse all

Create a bidirectional LSTM layer with the name bilstm1 and 100 hidden units.

layer = bilstmLayer(100,Name="bilstm1")

layer = BiLSTMLayer with properties:

                   Name: 'bilstm1'
             InputNames: {'in'}
            OutputNames: {'out'}
              NumInputs: 1
             NumOutputs: 1
         HasStateInputs: 0
        HasStateOutputs: 0

Hyperparameters InputSize: 'auto' NumHiddenUnits: 100 OutputMode: 'sequence' StateActivationFunction: 'tanh' GateActivationFunction: 'sigmoid'

Learnable Parameters InputWeights: [] RecurrentWeights: [] Bias: []

State Parameters HiddenState: [] CellState: []

Show all properties

Include a bidirectional LSTM layer in a Layer array.

inputSize = 12; numHiddenUnits = 100; numClasses = 9;

layers = [ ... sequenceInputLayer(inputSize) bilstmLayer(numHiddenUnits) fullyConnectedLayer(numClasses) softmaxLayer]

layers = 4×1 Layer array with layers:

 1   ''   Sequence Input    Sequence input with 12 dimensions
 2   ''   BiLSTM            BiLSTM with 100 hidden units
 3   ''   Fully Connected   9 fully connected layer
 4   ''   Softmax           softmax

Algorithms

expand all

Layers in a layer array or layer graph pass data to subsequent layers as formatted dlarray objects. The format of a dlarray object is a string of characters in which each character describes the corresponding dimension of the data. The format consists of one or more of these characters:

For example, you can describe 2-D image data that is represented as a 4-D array, where the first two dimensions correspond to the spatial dimensions of the images, the third dimension corresponds to the channels of the images, and the fourth dimension corresponds to the batch dimension, as having the format "SSCB" (spatial, spatial, channel, batch).

You can interact with these dlarray objects in automatic differentiation workflows, such as those for developing a custom layer, using a functionLayer object, or using the forward and predict functions withdlnetwork objects.

This table shows the supported input formats of BiLSTMLayer objects and the corresponding output format. If the software passes the output of the layer to a custom layer that does not inherit from the nnet.layer.Formattable class, or aFunctionLayer object with the Formattable property set to 0 (false), then the layer receives an unformatted dlarray object with dimensions ordered according to the formats in this table. The formats listed here are only a subset. The layer may support additional formats such as formats with additional "S" (spatial) or"U" (unspecified) dimensions.

Input Format OutputMode Output Format
"CB" (channel, batch) "sequence" "CB" (channel, batch)
"last"
"CBT" (channel, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SB" (spatial, batch) "sequence" "CB" (channel, batch)
"last"

In dlnetwork objects, BiLSTMLayer objects also support these input and output format combinations.

Input Format OutputMode Output Format
"SCB" (spatial, channel, batch) "sequence" "CB" (channel, batch)
"last"
"SSCB" (spatial, spatial, channel, batch) "sequence"
"last"
"SSSCB" (spatial, spatial, spatial, channel, batch) "sequence"
"last"
"SCBT" (spatial, channel, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SSCBT" (spatial, spatial, channel, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SSSCBT" (spatial, spatial, spatial, channel, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SC" (spatial, channel) "sequence" "CU" (channel, unspecified)
"last"
"SSC" (spatial, spatial, channel) "sequence"
"last"
"SSSC" (spatial, spatial, spatial, channel) "sequence"
"last"
"CT" (channel, time) "sequence" "CT" (channel, time)
"last" "CU" (channel, unspecified)
"SCT" (spatial, channel, time) "sequence" "CT" (channel, time)
"last" "CU" (channel, unspecified)
"SSCT" (spatial, spatial, channel, time) "sequence" "CT" (channel, time)
"last" "CU" (channel, unspecified)
"SSSCT" (spatial, spatial, channel, time) "sequence" "CT" (channel, time)
"last" "CU" (channel, unspecified)
"SSB" (spatial, spatial, batch) "sequence" "CB" (channel, batch)
"last"
"SSSB" (spatial, spatial, spatial, batch) "sequence"
"last"
"BT" (batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SBT" (spatial, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SSBT" (spatial, spatial, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)
"SSSBT" (spatial, spatial, spatial, batch, time) "sequence" "CBT" (channel, batch, time)
"last" "CB" (channel, batch)

If the HasStateInputs property is 1 (true), then the layer has two additional inputs with the names"hidden" and "cell", which correspond to the hidden state and cell state, respectively. These additional inputs expect input format"CB" (channel, batch).

If the HasStateOutputs property is 1 (true), then the layer has two additional outputs with names"hidden" and "cell", which correspond to the hidden state and cell state, respectively. These additional outputs have output format"CB" (channel, batch).

References

[1] Glorot, Xavier, and Yoshua Bengio. "Understanding the Difficulty of Training Deep Feedforward Neural Networks." In Proceedings of the Thirteenth International Conference on Artificial Intelligence and Statistics, 249–356. Sardinia, Italy: AISTATS, 2010. https://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf

[2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. "Delving Deep into Rectifiers: Surpassing Human-Level Performance on ImageNet Classification." In 2015 IEEE International Conference on Computer Vision (ICCV), 1026–34. Santiago, Chile: IEEE, 2015. https://doi.org/10.1109/ICCV.2015.123

[3] Saxe, Andrew M., James L. McClelland, and Surya Ganguli. "Exact Solutions to the Nonlinear Dynamics of Learning in Deep Linear Neural Networks.” Preprint, submitted February 19, 2014. https://arxiv.org/abs/1312.6120.

Extended Capabilities

expand all

Usage notes and limitations:

Usage notes and limitations:

Version History

Introduced in R2018a

expand all

Starting in R2019a, the software, by default, initializes the layer input weights of this layer using the Glorot initializer. This behavior helps stabilize training and usually reduces the training time of deep neural networks.

In previous releases, the software, by default, initializes the layer input weights using the by sampling from a normal distribution with zero mean and variance 0.01. To reproduce this behavior, set the InputWeightsInitializer option of the layer to"narrow-normal".

Starting in R2019a, the software, by default, initializes the layer recurrent weights of this layer with Q, the orthogonal matrix given by the QR decomposition of Z = Q R for a random matrix Z sampled from a unit normal distribution. This behavior helps stabilize training and usually reduces the training time of deep neural networks.

In previous releases, the software, by default, initializes the layer recurrent weights using the by sampling from a normal distribution with zero mean and variance 0.01. To reproduce this behavior, set the RecurrentWeightsInitializer option of the layer to "narrow-normal".