Return only the final timestep output
Sequence-to-one prediction
TensorFlow · Class
A Long Short-Term Memory layer for processing sequence data. It can return the final output, the full output sequence, and optionally its recurrent states.
LSTM consumes a batch of sequences and updates its recurrent state across timesteps. By default it returns the final output, while options can expose the complete sequence or the final hidden and cell states; it can select a cuDNN-backed implementation when the documented conditions are met.
tf.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 the output dimensionality. |
activation | callable or string | Activation function for the input transformation; defaults to tanh. |
recurrent_activation | callable or string | Activation function for the recurrent transformation; defaults to sigmoid. |
use_bias | bool | Whether to use a bias vector. |
kernel_initializer | Initializer | Initializer for the input kernel. |
recurrent_initializer | Initializer | Initializer for the recurrent kernel. |
bias_initializer | Initializer | Initializer for the bias vector. |
unit_forget_bias | bool | Whether to add one to the forget-gate bias at initialization. |
kernel_regularizer | Regularizer | Regularizer for the input kernel. |
recurrent_regularizer | Regularizer | Regularizer for the recurrent kernel. |
bias_regularizer | Regularizer | Regularizer for the bias. |
activity_regularizer | Regularizer | Regularizer for the layer activity. |
For the default configuration, a tensor containing the final output with shape (batch, units). With return_sequences=True, the output has shape (batch, timesteps, units). With return_state=True, the output is accompanied by the hidden state and cell state tensors.
import tensorflow as tf
layer = tf.keras.layers.LSTM(16, return_sequences=True)
x = tf.random.normal((4, 10, 8))
y = layer(x)
print(y.shape) # (4, 10, 16)Creates an LSTM with 16 output units and returns one output vector for each of 10 input time steps.
Sequence-to-one prediction
Sequence labeling or stacked recurrent layers
Long-running or chunked sequences
GPU-accelerated training
The units argument sets the dimensionality of the output space. With return_sequences=False, the layer returns only the output from the final timestep; with return_sequences=True, it returns an output for every timestep.
Follow-up: How do you expose recurrent state? Set return_state=True; an LSTM returns two state tensors in addition to its output: the hidden state and cell state.
The layer expects a 3D input shaped as batch, timesteps, and features. The feature dimension must match the input feature size used by the layer's kernel.
Follow-up: What changes when return_sequences=True? The output includes the time dimension and has shape batch, timesteps, units rather than batch, units.
On a TensorFlow backend, the layer can use a fast cuDNN implementation when activation is tanh, recurrent_activation is sigmoid, dropout and recurrent_dropout are zero, unroll is false, use_bias is true, and masking is right-padded.
Follow-up: Why might performance differ across environments? The layer chooses between cuDNN-based and backend-native implementations based on available hardware and constraints.
tf.keras.layers.LSTMCelltf.keras.layers.RNNtf.keras.layers.Bidirectionaltf.keras.layers.GRU