VOOZH about

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

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


  • Courses
  • Tutorials
  • Interview Prep

tf.keras.layers.GRU in TensorFlow

Last Updated : 15 Jun, 2026

tf.keras.layers.GRU is a TensorFlow layer that implements the Gated Recurrent Unit (GRU), a recurrent neural network designed for processing sequential data. It is commonly used in tasks such as speech recognition, machine translation, and time-series forecasting due to its ability to learn patterns across sequences efficiently.

  • Learns long-term dependencies with fewer parameters than LSTMs, enabling faster and more efficient training.
  • Supports options such as return_sequences, return_state, and dropout for flexible sequence modeling.

Syntax

The tf.keras.layers.GRU layer creates a Gated Recurrent Unit (GRU) for processing sequential data. It provides several parameters to control the layer's behavior, output, and training configuration.

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

Parameters

  • units: Number of neurons in the GRU layer.
  • activation: Activation function for the output.
  • return_sequences: Returns outputs for all time steps.
  • return_state: Returns the final hidden state.
  • dropout: Dropout rate for input connections.
  • stateful: Maintains state across batches.

Implementation

1. Import Required Libraries

Importing TensorFlow, Keras modules, and NumPy for creating and training the GRU model.

2. Create Dummy Sequential Data

Generating sample sequential data consisting of 100 sequences, each having 10 time steps and 5 features.

3. Build a GRU Model

Creating a sequential model with two GRU layers followed by a dense output layer for binary classification.

Output:

👁 Capture

4. Train the Model

Training the model on the generated dataset and monitor the loss and accuracy during training.

Output:

Epoch 1/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 6s 15ms/step - accuracy: 0.5487 - loss: 0.6960
.
.
.
Epoch 9/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - accuracy: 0.6135 - loss: 0.6825
Epoch 10/10
7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 16ms/step - accuracy: 0.6224 - loss: 0.6848
<keras.src.callbacks.history.History at 0x7968ee5983d0>

The return_sequences parameter returns outputs for all time steps, while return_state returns the final hidden state of the GRU layer.

Step 5: Extract Sequence Outputs and Hidden States

Use the return_sequences and return_state parameters to obtain outputs for all time steps along with the final hidden state.

Output:

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

Explanation:

  • The output contains 50 units for each time step (10) and batch (5).
  • The hidden state has 50 units per batch.

Advantages

  • Uses fewer parameters than LSTMs, resulting in faster training and lower memory usage.
  • Effectively learns patterns and dependencies in sequential data.
  • Integrates easily with TensorFlow and Keras for building sequence-based models.
  • Supports configurable options such as return_sequences, return_state, and dropout for different use cases.

Limitations

  • May be less effective than LSTMs for some complex sequence-learning tasks.
  • Training can become computationally expensive for very long sequences.
  • Performance is sensitive to the choice of hyperparameters and model configuration.
  • Like other deep learning models, its decision-making process can be difficult to interpret.
Comment