Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TensorFlow · Class

layers.LSTMCell

An LSTM cell that performs one timestep of recurrent computation. Wrap it in keras.layers.RNN to apply it across complete input sequences.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
unitsintPositive integer specifying the output and state dimensionality.
activationactivationActivation function for the main transformation; defaults to tanh and may be None for linear behavior.
recurrent_activationactivationActivation function for the recurrent step; defaults to sigmoid.
use_biasboolWhether to use a bias vector; defaults to True.
kernel_initializerinitializerInitializer for the input kernel weights.
recurrent_initializerinitializerInitializer for the recurrent kernel weights.
bias_initializerinitializerInitializer for the bias vector.
unit_forget_biasboolWhether to add one to the forget-gate bias; defaults to True.
kernel_regularizer, recurrent_regularizer, bias_regularizerregularizer or NoneRegularizers for kernel, recurrent kernel, and bias weights.
kernel_constraint, recurrent_constraint, bias_constraintconstraint or NoneConstraints for kernel, recurrent kernel, and bias weights.
dropoutfloatFraction of input-transformation units to drop, between 0 and 1.
recurrent_dropoutfloatFraction of recurrent-transformation units to drop, between 0 and 1.

Returns

When called, returns the cell output and updated state tensors; the exact structure follows the Keras RNN cell interface.

Key points

  • LSTMCell is the cell-level counterpart of the LSTM layer.
  • Each invocation handles one timestep rather than iterating over a whole sequence.
  • The per-step input is a rank-two tensor with batch size and feature dimensions.
  • The call consumes the previous timestep's state alongside the current input.
  • The main activation defaults to tanh.
  • The recurrent activation defaults to sigmoid.
  • The dropout setting controls input-side unit dropping during the linear transformation.

Common mistakes

  • Do not pass a complete sequence directly to the cell; use keras.layers.RNN when sequence iteration is needed.
  • The training argument matters only when either dropout setting is nonzero.

Example: Run an LSTM cell over sequences

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.

When to use it

Build a recurrent loop with explicit cell-level execution

Custom timestep control

Wrap the cell in keras.layers.RNN for batched sequences

Sequence processing

Carry hidden and memory states between successive calls

Stateful inference

Combine multiple cells with StackedRNNCells

Stacked recurrence

Interview questions

QHow does LSTMCell differ from keras.layers.LSTM?

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).

QWhat does units control?

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.

QWhat are the call arguments and their shapes?

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.RNN
  • tf.keras.layers.LSTM
  • tf.keras.layers.GRUCell
  • tf.keras.layers.SimpleRNNCell
  • tf.keras.layers.StackedRNNCells

Source: tf.keras.layers.LSTMCell | TensorFlow v2.16.1. Checked 2026-10-15.