Build a recurrent loop with explicit cell-level execution
Custom timestep control
TensorFlow · Class
An LSTM cell that performs one timestep of recurrent computation. Wrap it in keras.layers.RNN to apply it across complete input sequences.
LSTMCell performs one timestep of an LSTM computation. Use it directly when controlling timestep execution or wrap it with keras.layers.RNN to process complete sequences and optionally expose the resulting states.
tf.keras.layers.LSTMCell(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, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, seed=None, **kwargs)| Name | Type | Description |
|---|---|---|
units | int | Positive integer specifying the output and state dimensionality. |
activation | activation | Activation function for the main transformation; defaults to tanh and may be None for linear behavior. |
recurrent_activation | activation | Activation function for the recurrent step; defaults to sigmoid. |
use_bias | bool | Whether to use a bias vector; defaults to True. |
kernel_initializer | initializer | Initializer for the input kernel weights. |
recurrent_initializer | initializer | Initializer for the recurrent kernel weights. |
bias_initializer | initializer | Initializer for the bias vector. |
unit_forget_bias | bool | Whether to add one to the forget-gate bias; defaults to True. |
kernel_regularizer, recurrent_regularizer, bias_regularizer | regularizer or None | Regularizers for kernel, recurrent kernel, and bias weights. |
kernel_constraint, recurrent_constraint, bias_constraint | constraint or None | Constraints for kernel, recurrent kernel, and bias weights. |
dropout | float | Fraction of input-transformation units to drop, between 0 and 1. |
recurrent_dropout | float | Fraction of recurrent-transformation units to drop, between 0 and 1. |
When called, returns the cell output and updated state tensors; the exact structure follows the Keras RNN cell interface.
import tensorflow as tf
cell = tf.keras.layers.LSTMCell(4)
rnn = tf.keras.layers.RNN(cell, return_state=True)
inputs = tf.random.normal((2, 3, 5))
output, hidden, carry = rnn(inputs)
print(output.shape, hidden.shape, carry.shape)This wraps one LSTMCell in an RNN layer, processes a batch of three-step sequences, and returns the final output plus both LSTM states.
Custom timestep control
Sequence processing
Stateful inference
Stacked recurrence
LSTMCell computes a single timestep, taking inputs shaped as (batch, features) and state tensors shaped as (batch, units). To process an entire sequence, place it inside keras.layers.RNN.
Follow-up: What shape does a full-sequence LSTM layer accept? keras.layers.LSTM consumes a 3D tensor shaped (batch, timesteps, feature).
The units argument determines the dimensionality of the output space and the recurrent state. The cell therefore returns state tensors whose feature dimension is units.
Follow-up: What are the two state components for an LSTM? The recurrent interface uses the hidden/output state and the cell-memory state.
The call receives inputs, states, and an optional training flag. Inputs are 2D, states are 2D with the batch and units dimensions, and training selects training or inference behavior when dropout is configured.
Follow-up: When is training behavior relevant? It is relevant only if dropout or recurrent_dropout is used.
tf.keras.layers.RNNtf.keras.layers.LSTMtf.keras.layers.GRUCelltf.keras.layers.SimpleRNNCelltf.keras.layers.StackedRNNCells