For a step-by-step description of the algorithm, see this tutorial.

layer_lstm(
object,
units,
activation = "tanh",
recurrent_activation = "sigmoid",
use_bias = TRUE,
return_sequences = FALSE,
return_state = FALSE,
go_backwards = FALSE,
stateful = FALSE,
time_major = FALSE,
unroll = FALSE,
kernel_initializer = "glorot_uniform",
recurrent_initializer = "orthogonal",
bias_initializer = "zeros",
unit_forget_bias = TRUE,
kernel_regularizer = NULL,
recurrent_regularizer = NULL,
bias_regularizer = NULL,
activity_regularizer = NULL,
kernel_constraint = NULL,
recurrent_constraint = NULL,
bias_constraint = NULL,
dropout = 0,
recurrent_dropout = 0,
input_shape = NULL,
batch_input_shape = NULL,
batch_size = NULL,
dtype = NULL,
name = NULL,
trainable = NULL,
weights = NULL
)

## Arguments

object What to call the new Layer instance with. Typically a keras Model, another Layer, or a tf.Tensor/KerasTensor. If object is missing, the Layer instance is returned, otherwise, layer(object) is returned. Positive integer, dimensionality of the output space. Activation function to use. Default: hyperbolic tangent (tanh). If you pass NULL, no activation is applied (ie. "linear" activation: a(x) = x). Activation function to use for the recurrent step. Boolean, whether the layer uses a bias vector. Boolean. Whether to return the last output in the output sequence, or the full sequence. Boolean (default FALSE). Whether to return the last state in addition to the output. Boolean (default FALSE). If TRUE, process the input sequence backwards and return the reversed sequence. Boolean (default FALSE). If TRUE, the last state for each sample at index i in a batch will be used as initial state for the sample of index i in the following batch. If True, the inputs and outputs will be in shape [timesteps, batch, feature], whereas in the False case, it will be [batch, timesteps, feature]. Using time_major = TRUE is a bit more efficient because it avoids transposes at the beginning and end of the RNN calculation. However, most TensorFlow data is batch-major, so by default this function accepts input and emits output in batch-major form. Boolean (default FALSE). If TRUE, the network will be unrolled, else a symbolic loop will be used. Unrolling can speed-up a RNN, although it tends to be more memory-intensive. Unrolling is only suitable for short sequences. Initializer for the kernel weights matrix, used for the linear transformation of the inputs. Initializer for the recurrent_kernel weights matrix, used for the linear transformation of the recurrent state. Initializer for the bias vector. Boolean. If TRUE, add 1 to the bias of the forget gate at initialization. Setting it to true will also force bias_initializer="zeros". This is recommended in Jozefowicz et al. Regularizer function applied to the kernel weights matrix. Regularizer function applied to the recurrent_kernel weights matrix. Regularizer function applied to the bias vector. Regularizer function applied to the output of the layer (its "activation").. Constraint function applied to the kernel weights matrix. Constraint function applied to the recurrent_kernel weights matrix. Constraint function applied to the bias vector. Float between 0 and 1. Fraction of the units to drop for the linear transformation of the inputs. Float between 0 and 1. Fraction of the units to drop for the linear transformation of the recurrent state. Dimensionality of the input (integer) not including the samples axis. This argument is required when using this layer as the first layer in a model. Shapes, including the batch size. For instance, batch_input_shape=c(10, 32) indicates that the expected input will be batches of 10 32-dimensional vectors. batch_input_shape=list(NULL, 32) indicates batches of an arbitrary number of 32-dimensional vectors. Fixed batch size for layer The data type expected by the input, as a string (float32, float64, int32...) An optional name string for the layer. Should be unique in a model (do not reuse the same name twice). It will be autogenerated if it isn't provided. Whether the layer weights will be updated during training. Initial weights for layer.

## Input shapes

N-D tensor with shape (batch_size, timesteps, ...), or (timesteps, batch_size, ...) when time_major = TRUE.

## Output shape

• if return_state: a list of tensors. The first tensor is the output. The remaining tensors are the last states, each with shape (batch_size, state_size), where state_size could be a high dimension tensor shape.

• if return_sequences: N-D tensor with shape [batch_size, timesteps, output_size], where output_size could be a high dimension tensor shape, or [timesteps, batch_size, output_size] when time_major is TRUE

• else, N-D tensor with shape [batch_size, output_size], where output_size could be a high dimension tensor shape.

This layer supports masking for input data with a variable number of timesteps. To introduce masks to your data, use layer_embedding() with the mask_zero parameter set to TRUE.

## Statefulness in RNNs

You can set RNN layers to be 'stateful', which means that the states computed for the samples in one batch will be reused as initial states for the samples in the next batch. This assumes a one-to-one mapping between samples in different successive batches.

For intuition behind statefulness, there is a helpful blog post here: https://philipperemy.github.io/keras-stateful-lstm/

To enable statefulness:

• Specify stateful = TRUE in the layer constructor.

• Specify a fixed batch size for your model. For sequential models, pass batch_input_shape = list(...) to the first layer in your model. For functional models with 1 or more Input layers, pass batch_shape = list(...) to all the first layers in your model. This is the expected shape of your inputs including the batch size. It should be a list of integers, e.g. list(32, 10, 100). For dimensions which can vary (are not known ahead of time), use NULL in place of an integer, e.g. list(32, NULL, NULL).

• Specify shuffle = FALSE when calling fit().

## References

Other recurrent layers: layer_cudnn_gru(), layer_cudnn_lstm(), layer_gru(), layer_rnn(), layer_simple_rnn()
Other recurrent layers: layer_cudnn_gru(), layer_cudnn_lstm(), layer_gru(), layer_rnn(), layer_simple_rnn()