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

TensorFlow · Class

layers.GRU

tf.keras.layers.GRU is a gated recurrent layer for sequence data. It can return the final timestep output or the complete output sequence, and can optionally expose its recurrent state.

How it works

GRU is a recurrent Keras layer that consumes sequences shaped as batches of timesteps and features. It maintains one recurrent state while processing timesteps, and can return either only the final output or the full output sequence; configuration such as activations, dropout, direction, and state handling controls its behavior.

Syntax

tf.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 defining the output dimensionality and recurrent state size.
activationactivation function or NoneActivation applied to the layer output; defaults to tanh.
recurrent_activationactivation function or NoneActivation used for recurrent updates; defaults to sigmoid.
use_biasboolWhether to include bias parameters.
kernel_initializerinitializerInitializer for the input kernel.
recurrent_initializerinitializerInitializer for the recurrent kernel.
bias_initializerinitializerInitializer for the bias.
*_regularizerregularizer or NoneRegularizers for kernel, recurrent kernel, bias, or activity.
*_constraintconstraint or NoneConstraints for kernel, recurrent kernel, or bias.
dropoutfloatFraction of input transformations to drop.
recurrent_dropoutfloatFraction of recurrent transformations to drop.
seedint or NoneRandom seed for dropout behavior.

Returns

A tensor containing the final output by default, or a sequence of outputs when return_sequences=True; with return_state=True, the recurrent state is returned as an additional result.

Key points

  • GRU is a gated recurrent unit layer based on the architecture attributed to Cho and collaborators.
  • TensorFlow documents two GRU implementation conventions.
  • The default convention applies the reset gate before the recurrent matrix multiplication.
  • The reset-after convention stores separate biases for the input and recurrent transformations.
  • Select the reset-after convention with reset_after=True and a sigmoid recurrent activation.
  • With the default output mode, the layer returns one units-sized vector per batch element.
  • return_sequences=True changes the result to one units-sized vector at every timestep.

Common mistakes

  • Masked inputs must be right-padded when relying on the cuDNN implementation.
  • The cuDNN implementation requires eager execution in the outermost context.
  • The reset-after variant supports the cuDNN-compatible convention and CPU inference.

Example: Sequence outputs from a GRU

import tensorflow as tf

layer = tf.keras.layers.GRU(32, return_sequences=True)
x = tf.random.normal((4, 10, 8))
y = layer(x)
print(y.shape)  # (4, 10, 32)

Creates a GRU that produces one 32-dimensional output for every timestep and applies it to a batch of 10-step sequences.

When to use it

Final-state sequence encoding

Encode a variable-length or fixed-length feature sequence into one vector for classification or regression.

Per-timestep representations

Preserve a representation at each timestep for sequence labeling or downstream recurrent processing.

Sequential recommendation

Process ordered histories such as user interactions before predicting a subsequent item.

Bidirectional sequence modeling

Build a two-direction sequence encoder by wrapping the GRU with Bidirectional.

Interview questions

QWhat output shapes does GRU produce?

The layer expects sequence input with batch, timestep, and feature dimensions. By default it returns the output associated with the final timestep; setting return_sequences=True returns an output for every timestep.

Follow-up: How can the recurrent state be exposed? Set return_state=True; the GRU returns its output together with one state tensor, unlike an LSTM's two state tensors.

QWhat does reset_after control?

A GRU has two documented implementation conventions that differ in where the reset gate is applied relative to matrix multiplication. reset_after=True selects the convention compatible with cuDNN and uses separate kernel and recurrent biases.

Follow-up: Which settings enable the cuDNN path? Keep tanh and sigmoid activations, zero dropout values, unroll=False, use_bias=True, reset_after=True, right-padded masks, and outermost eager execution.

QWhen can GRU use cuDNN?

The layer can use a cuDNN implementation when its documented configuration requirements are satisfied. Those requirements include the default activations, no dropout, no unrolling, bias enabled, reset_after enabled, suitable masking, and eager execution.

Follow-up: What trade-off does unroll=False reflect? It preserves the configuration required for the optimized implementation, whereas unrolling changes how the recurrent computation is expanded.

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

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