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

Keras · Class

layers.GRU

keras.layers.GRU processes sequence data with gated recurrent updates and produces either the final output or outputs for every timestep. It supports configurable activations, dropout, state handling, direction, and recurrent-kernel conventions.

How it works

GRU is a recurrent layer that reads a sequence and updates a hidden representation at each timestep. Configure its output behavior with return_sequences and return_state, and optionally process sequences backward, preserve state across calls, or use dropout during training.

Syntax

keras.layers.GRU(units, activation="tanh", recurrent_activation="sigmoid", use_bias=True, kernel_initializer="glorot_uniform", recurrent_initializer="orthogonal", bias_initializer="zeros", 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, reset_after=True, use_cudnn="auto", **kwargs)

Parameters

NameTypeDescription
unitsintPositive integer specifying the output dimensionality.
activationstr, callable, or NoneActivation used for the input transformation; defaults to tanh.
recurrent_activationstr, callable, or NoneActivation used during the recurrent step; defaults to sigmoid.
use_biasboolWhether to include a bias vector.
dropoutfloatFraction of input transformations to drop.
recurrent_dropoutfloatFraction of recurrent transformations to drop.
return_sequencesboolWhether to return outputs for all timesteps.
return_stateboolWhether to return the final state along with the output.
go_backwardsboolWhether to process the sequence in reverse order.
statefulboolWhether to reuse state across batches.
unrollboolWhether to unroll the recurrent computation.
reset_afterboolGRU gate-order convention; the default is the cuDNN-compatible after-reset form.

Returns

The GRU output; with return_sequences=True it contains outputs for all timesteps, and with return_state=True the final state is returned as an additional value.

Key points

  • units must be a positive integer and determines the width of the GRU output.
  • Setting activation to None disables the main activation function.
  • The recurrent-step activation defaults to sigmoid.
  • use_bias controls whether the layer includes a bias vector.

Common mistakes

  • For the cuDNN-compatible GRU convention, keep reset_after=True and recurrent_activation='sigmoid'.
  • The alternate GRU convention is compatible with the GPU-only CuDNNGRU implementation and supports CPU inference.

Example: Return outputs for every timestep

import keras
import numpy as np

x = np.random.random((2, 5, 3)).astype("float32")
gru = keras.layers.GRU(4, return_sequences=True)
y = gru(x)
print(y.shape)  # (2, 5, 4)

Creates a GRU with four output units and returns an output for every timestep.

When to use it

Encode a temporal input into a final representation

Sequence classification

Return one representation per timestep

Sequence-to-sequence modeling

Summarize ordered user or item histories

Recommendation and history encoding

Combine forward and backward GRU context

Bidirectional sequence processing

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 the final output; setting it to True returns an output at every timestep.

Follow-up: How would you obtain the final hidden state separately? Set return_state=True; the layer then returns its output together with the final state.

QWhat are the GRU activation defaults?

The layer provides activation for the main transformation and recurrent_activation for the recurrent step. Their defaults are tanh and sigmoid, respectively.

Follow-up: Which settings select the documented cuDNN-compatible convention? Use reset_after=True and recurrent_activation='sigmoid'.

QHow do dropout and recurrent_dropout differ?

dropout drops part of the input transformation, while recurrent_dropout drops part of the recurrent transformation. Both are fractions between zero and one and default to zero.

Follow-up: When does dropout affect computation? It is used during training rather than inference.

  • keras.layers.GRUCell
  • keras.layers.LSTM
  • keras.layers.Bidirectional
  • keras.layers.RNN

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