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

TensorFlow · Class

layers.LSTM

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
unitsintPositive integer specifying the output dimensionality.
activationcallable or stringActivation function for the input transformation; defaults to tanh.
recurrent_activationcallable or stringActivation function for the recurrent transformation; defaults to sigmoid.
use_biasboolWhether to use a bias vector.
kernel_initializerInitializerInitializer for the input kernel.
recurrent_initializerInitializerInitializer for the recurrent kernel.
bias_initializerInitializerInitializer for the bias vector.
unit_forget_biasboolWhether to add one to the forget-gate bias at initialization.
kernel_regularizerRegularizerRegularizer for the input kernel.
recurrent_regularizerRegularizerRegularizer for the recurrent kernel.
bias_regularizerRegularizerRegularizer for the bias.
activity_regularizerRegularizerRegularizer for the layer activity.

Returns

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.

Key points

  • units must be a positive integer and determines the width of each output vector.
  • The default main activation is tanh.
  • The default recurrent activation is sigmoid.
  • dropout controls the fraction of input-transformation units dropped during training.
  • recurrent_dropout applies dropout to the recurrent transformation.
  • Implementation choice depends on runtime hardware and constraints.

Common mistakes

  • Setting activation to None removes the usual nonlinear activation and makes the layer's activation linear.
  • Masked sequences must be right-padded to satisfy the documented cuDNN implementation requirements.

Example: Return outputs for every timestep

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.

When to use it

Return only the final timestep output

Sequence-to-one prediction

Return an output at every timestep

Sequence labeling or stacked recurrent layers

Carry state across batches

Long-running or chunked sequences

Use the cuDNN-compatible configuration

GPU-accelerated training

Interview questions

QWhat do units and return_sequences control?

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.

QWhat input and output shapes does LSTM use?

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.

QWhen can LSTM use the fast cuDNN implementation?

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.LSTMCell
  • tf.keras.layers.RNN
  • tf.keras.layers.Bidirectional
  • tf.keras.layers.GRU

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