VOOZH about

URL: https://www.geeksforgeeks.org/deep-learning/tf-keras-layers-lstm-in-tensorflow/

⇱ tf.keras.layers.LSTM in TensorFlow - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

tf.keras.layers.LSTM in TensorFlow

Last Updated : 23 Jul, 2025

The tf.keras.layers.LSTM layer is a built-in TensorFlow layer designed to handle sequential data efficiently. It is widely used for applications like:

  • Text Generation
  • Machine Translation
  • Stock Price Prediction
  • Speech Recognition
  • Time-Series Forecasting

Long-Short Term Memory (LSTMs) address the limitations of standard Recurrent Neural Networks (RNNs) by incorporating gates (forget, input, and output gates), which help in retaining important information over long sequences.

Syntax of tf.keras.layers.LSTM

tf.keras.layers.LSTM(
units,
activation='tanh',
recurrent_activation='sigmoid',
return_sequences=False,
return_state=False,
dropout=0.0,
recurrent_dropout=0.0,
stateful=False,
unroll=False
)

Parameters of tf.keras.layers.LSTM:

  • units – Number of LSTM cells (neurons) in the layer.
  • activation – Activation function (default: 'tanh').
  • recurrent_activation – Activation for the recurrent step (default: 'sigmoid').
  • return_sequences – If True, returns sequences instead of just the last output.
  • return_state – If True, returns the hidden state and cell state along with the output.
  • go_backwards – If True, processes input in reverse order.
  • stateful – If True, maintains state across batches.
  • dropout – Dropout rate for input connections.
  • recurrent_dropout – Dropout rate for recurrent connections.
  • kernel_initializer – Weight initialization strategy.

How to Use tf.keras.layers.LSTM in TensorFlow?

Let's learn to use LSTMs in TensorFlow, covering key parameters like return_sequences and return_state. You'll also understand how LSTMs process sequences and retain long-term dependencies through hidden and cell states.

1. Import Required Libraries

2. Create Dummy Sequential Data

3. Build an LSTM Model

Output:

πŸ‘ Capture

4. Train the Model

Output:

Epoch 1/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 4s 14ms/step - accuracy: 0.5260 - loss: 0.6946
.
.
.
Epoch 10/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 14ms/step - accuracy: 0.5830 - loss: 0.6830
<keras.src.callbacks.history.History at 0x7968ee53b250>

Understanding return_sequences and return_state

  • return_sequences=True β†’ Returns the output for each time step instead of just the final one.
  • return_state=True β†’ Returns the hidden state and cell state along with the output.

Example:

Output:

(5, 10, 50) (5, 50) (5, 50)

This means:

  • The output contains 50 units for each time step (10) and batch (5).
  • The hidden and cell states have 50 units per batch.

TensorFlow’s tf.keras.layers.LSTM is a powerful tool for handling sequential data, providing flexibility with return states, bidirectional processing, and dropout regularization. Whether you're working on NLP, finance, or speech recognition, LSTMs are essential for capturing long-term dependencies.

Comment