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

TensorFlow · Class

layers.GRUCell

tf.keras.layers.GRUCell implements one timestep of a gated recurrent unit. Wrap it with tf.keras.layers.RNN when a sequence-level recurrent layer is needed.

How it works

GRUCell performs one timestep of gated recurrent computation. It receives a batch of feature vectors and the previous state, applies input and recurrent transformations with configurable activations, dropout, and reset-gate ordering, then produces the timestep output and updated state.

Syntax

tf.keras.layers.GRUCell(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, kernel_constraint=None, recurrent_constraint=None, bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, reset_after=True, seed=None, **kwargs)

Parameters

NameTypeDescription
unitsintNumber of units in the GRU state.
activationcallable or stringActivation applied to the candidate state; defaults to tanh, or no activation when None.
recurrent_activationcallable or stringActivation used for the recurrent gates; defaults to sigmoid.
use_biasboolWhether to include a bias vector.
kernel_initializerInitializerInitializer for the input kernel.
recurrent_initializerInitializerInitializer for the recurrent kernel.
bias_initializerInitializerInitializer for the bias.
kernel_regularizerRegularizerRegularizer for the input kernel.
recurrent_regularizerRegularizerRegularizer for the recurrent kernel.
bias_regularizerRegularizerRegularizer for the bias.
kernel_constraintConstraintConstraint for the input kernel.
recurrent_constraintConstraintConstraint for the recurrent kernel.

Returns

The cell returns the output for the current timestep and the updated recurrent state.

Key points

  • GRUCell is the recurrent cell used to implement a GRU layer.
  • Each invocation handles one timestep rather than an entire temporal sequence.
  • At call time, inputs contain one batch of feature vectors.
  • The state argument carries the preceding timestep's hidden representation.
  • dropout and recurrent_dropout specify fractions of input and recurrent transformations to drop.
  • reset_after chooses the reset-gate ordering used by the cell.
  • seed controls the randomness used by dropout.

Common mistakes

  • GRUCell is a single-timestep cell, not a complete sequence processor; use tf.keras.layers.RNN to iterate it over a sequence.
  • The training argument affects behavior only when input or recurrent dropout is enabled.
  • The previous state must have batch-by-units shape, matching the cell's units.

Example: Run a GRU cell through an RNN wrapper

import tensorflow as tf

cell = tf.keras.layers.GRUCell(4)
rnn = tf.keras.layers.RNN(cell, return_state=True)
inputs = tf.random.normal((2, 3, 5))
output, state = rnn(inputs)
print(output.shape, state.shape)

Wrap the cell in RNN to process a batch of three-step sequences and return the final output plus state.

When to use it

Build a customized GRU recurrence by configuring activations, initializers, regularization, constraints, or gate orderin

Custom recurrent computation

Use the cell inside tf.keras.layers.RNN to process sequences timestep by timestep.

Sequence processing

Feed one timestep at a time while carrying the returned state to the next call.

Stateful step-wise inference

Interview questions

QHow does GRUCell differ from tf.keras.layers.GRU?

GRUCell computes one timestep, whereas the GRU layer handles a complete sequence. To process multiple timesteps with the cell, place it inside tf.keras.layers.RNN.

Follow-up: What shape does the cell receive? It takes inputs shaped (batch, features) and a previous state shaped (batch, units).

QWhat does reset_after control?

The reset_after option selects whether the reset gate is applied after or before matrix multiplication. Its default is True, which is the cuDNN-compatible convention.

Follow-up: Which settings support the cuDNN implementation? The GRU documentation lists tanh and sigmoid activations, zero dropout, no unrolling, bias enabled, reset_after=True, suitable padding, and eager execution.

QWhat are the call inputs and state shapes?

The inputs argument is a 2D tensor with shape (batch, features), while states is a 2D tensor with shape (batch, units). The training flag selects training or inference behavior when dropout is configured.

Follow-up: Why wrap the cell in RNN? The wrapper supplies sequence iteration and manages recurrent state across timesteps.

  • tf.keras.layers.GRU
  • tf.keras.layers.RNN
  • tf.keras.layers.SimpleRNNCell
  • tf.keras.layers.LSTMCell

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