Encode a temporal input into a final representation
Sequence classification
Keras · Class
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.
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.
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)| Name | Type | Description |
|---|---|---|
units | int | Positive integer specifying the output dimensionality. |
activation | str, callable, or None | Activation used for the input transformation; defaults to tanh. |
recurrent_activation | str, callable, or None | Activation used during the recurrent step; defaults to sigmoid. |
use_bias | bool | Whether to include a bias vector. |
dropout | float | Fraction of input transformations to drop. |
recurrent_dropout | float | Fraction of recurrent transformations to drop. |
return_sequences | bool | Whether to return outputs for all timesteps. |
return_state | bool | Whether to return the final state along with the output. |
go_backwards | bool | Whether to process the sequence in reverse order. |
stateful | bool | Whether to reuse state across batches. |
unroll | bool | Whether to unroll the recurrent computation. |
reset_after | bool | GRU gate-order convention; the default is the cuDNN-compatible after-reset form. |
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.
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.
Sequence classification
Sequence-to-sequence modeling
Recommendation and history encoding
Bidirectional sequence processing
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.
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'.
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.GRUCellkeras.layers.LSTMkeras.layers.Bidirectionalkeras.layers.RNN