Build a customized GRU recurrence by configuring activations, initializers, regularization, constraints, or gate orderin
Custom recurrent computation
TensorFlow · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
units | int | Number of units in the GRU state. |
activation | callable or string | Activation applied to the candidate state; defaults to tanh, or no activation when None. |
recurrent_activation | callable or string | Activation used for the recurrent gates; defaults to sigmoid. |
use_bias | bool | Whether to include a bias vector. |
kernel_initializer | Initializer | Initializer for the input kernel. |
recurrent_initializer | Initializer | Initializer for the recurrent kernel. |
bias_initializer | Initializer | Initializer for the bias. |
kernel_regularizer | Regularizer | Regularizer for the input kernel. |
recurrent_regularizer | Regularizer | Regularizer for the recurrent kernel. |
bias_regularizer | Regularizer | Regularizer for the bias. |
kernel_constraint | Constraint | Constraint for the input kernel. |
recurrent_constraint | Constraint | Constraint for the recurrent kernel. |
The cell returns the output for the current timestep and the updated recurrent state.
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.
Custom recurrent computation
Sequence processing
Stateful step-wise inference
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).
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.
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.GRUtf.keras.layers.RNNtf.keras.layers.SimpleRNNCelltf.keras.layers.LSTMCell