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

Keras · Class

LSTM

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.

How it works

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.

Syntax

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 output dimensionality.
activationstr or callableActivation used for the input transformation; defaults to tanh.
recurrent_activationstr or callableActivation used for the recurrent step; defaults to sigmoid.
return_sequencesboolWhether to return every timestep's output.
return_stateboolWhether to return the final hidden and cell states as well.
dropoutfloatFraction of input units dropped during training.
recurrent_dropoutfloatFraction of recurrent units dropped during training.

Returns

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.

Key points

  • units sets the size of each emitted feature vector and must be a positive integer.
  • return_sequences chooses between one final output and outputs aligned with all timesteps.
  • Inputs use batch, time, and feature axes.
  • A mask can mark individual timesteps so the layer can ignore them.
  • The training flag controls training-versus-inference behavior when dropout is configured.

Common mistakes

  • The accelerated cuDNN path has specific requirements; configurations such as nonzero recurrent dropout can prevent its use.
  • Keep recurrent_dropout at zero when cuDNN acceleration is desired.

Example: Sequence-to-sequence LSTM

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.

When to use it

Preserve every timestep with return_sequences

Sequence labeling or per-timestep regression

Use the final output

Classification or regression from an entire sequence

Pass final recurrent states

Encoder-decoder architectures

Apply timestep masking

Variable-length sequences with padding

Interview questions

QWhat input shape does LSTM accept, and what does return_sequences control?

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.

QWhich core arguments determine an LSTM's representation and nonlinearities?

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.

QHow does Keras select the LSTM implementation?

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.

  • LSTMCell
  • RNN
  • GRU
  • Bidirectional
  • Masking

Source: LSTM layer - Keras. Checked 2026-10-15.