Preserve every timestep with return_sequences
Sequence labeling or per-timestep regression
Keras · Class
Keras LSTM is a recurrent layer for 3D sequence inputs. It can produce the last timestep, the complete sequence, and optionally the final hidden and cell states.
LSTM consumes a 3D sequence tensor and maintains recurrent state while processing its timesteps. It can emit only the final output or the full output sequence, optionally return its final hidden and cell states, and automatically select a cuDNN or backend-native implementation when available.
keras.layers.LSTM(units, activation="tanh", recurrent_activation="sigmoid", use_bias=True, kernel_initializer="glorot_uniform", recurrent_initializer="orthogonal", bias_initializer="zeros", unit_forget_bias=True, kernel_regularizer=None, recurrent_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, seed=None, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False, use_cudnn="auto", **kwargs)| Name | Type | Description |
|---|---|---|
units | int | Positive integer specifying output dimensionality. |
activation | str or callable | Activation used for the input transformation; defaults to tanh. |
recurrent_activation | str or callable | Activation used for the recurrent step; defaults to sigmoid. |
return_sequences | bool | Whether to return every timestep's output. |
return_state | bool | Whether to return the final hidden and cell states as well. |
dropout | float | Fraction of input units dropped during training. |
recurrent_dropout | float | Fraction of recurrent units dropped during training. |
For a 3D input, the layer returns the final output by default, or the complete output sequence when return_sequences=True. With return_state=True, it additionally returns the final hidden and cell states.
import keras
inputs = keras.Input(shape=(None, 8))
x = keras.layers.LSTM(16, return_sequences=True)(inputs)
outputs = keras.layers.Dense(1)(x)
model = keras.Model(inputs, outputs)
model.summary()Builds a model that applies the LSTM to every timestep and maps each resulting vector to one value.
Sequence labeling or per-timestep regression
Classification or regression from an entire sequence
Encoder-decoder architectures
Variable-length sequences with padding
The layer expects a 3D tensor shaped as batch, timesteps, and feature. With return_sequences disabled it returns the final output; with it enabled, it returns an output for every timestep.
Follow-up: How would you obtain the recurrent states? Set return_state=True; the layer then returns its output together with the final hidden and cell states.
units is a positive integer that determines the output dimensionality. The default activation is tanh and the default recurrent activation is sigmoid.
Follow-up: What is the role of recurrent_activation? It supplies the activation used for the recurrent step, independently of the main input transformation activation.
The layer chooses between a cuDNN-based implementation and a backend-native implementation according to available hardware and constraints. A cuDNN-compatible configuration includes recurrent_dropout equal to zero.
Follow-up: Why might a valid model run on the native path? Hardware availability or another configuration constraint can make the cuDNN implementation unavailable.
LSTMCellRNNGRUBidirectionalMasking