Classify or regress from the final recurrent output
Sequence-to-one prediction
Keras · Class
SimpleRNN is a fully connected recurrent layer that feeds each output back as the next input. It consumes 3D sequence tensors and can return the final output, the full sequence, and optionally the final state.
SimpleRNN processes a sequence one timestep at a time, feeding each output back as the next recurrent input. It accepts 3D sequence tensors, applies the configured input and recurrent transformations, and can return either the final output or the complete output sequence, optionally along with the final state.
keras.layers.SimpleRNN(units, activation="tanh", 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, return_sequences=False, return_state=False, go_backwards=False, stateful=False, unroll=False, seed=None, **kwargs)| Name | Type | Description |
|---|---|---|
units | int | Number of recurrent units. |
activation | str or callable | Activation function; defaults to tanh. |
use_bias | bool | Whether to use 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 vector. |
kernel_regularizer | regularizer | Regularizer for the input kernel. |
recurrent_regularizer | regularizer | Regularizer for the recurrent kernel. |
bias_regularizer | regularizer | Regularizer for the bias. |
activity_regularizer | regularizer | Activity regularizer. |
kernel_constraint | constraint | Constraint for the input kernel. |
recurrent_constraint | constraint | Constraint for the recurrent kernel. |
For a 3D input sequence, returns the last output by default; with return_sequences=True, returns the full output sequence. With return_state=True, also returns the last state.
import keras
from keras import layers
model = keras.Sequential([
layers.Input(shape=(20, 8)),
layers.SimpleRNN(16),
layers.Dense(1),
])
model.compile(optimizer="adam", loss="mse")
model.predict(keras.ops.zeros((4, 20, 8)))Builds a model that reads 20-step sequences with 8 features and reduces each sequence to a 16-unit recurrent output before regression.
Sequence-to-one prediction
Sequence-to-sequence modeling
Stateful streaming
SimpleRNN is a fully connected recurrent layer: its output is fed back as the next input. The layer receives sequence data shaped as batch, timesteps, and features, and by default returns the final timestep output.
Follow-up: How would you preserve every timestep? Set return_sequences=True, which changes the output from the final output to the full output sequence.
return_sequences=True returns an output for every timestep, while the default False returns only the last output. return_state=True additionally returns the final recurrent state.
Follow-up: When is return_state useful? It lets downstream code inspect or reuse the terminal recurrent state separately from the layer output.
The layer uses tanh by default, includes a bias by default, and applies dropout separately to input and recurrent transformations through dropout and recurrent_dropout. The dropout arguments are fractions and default to zero.
Follow-up: What should be checked when behavior differs between training and inference? Pass the training mode appropriately, because the layer's training argument is forwarded to the cell and affects dropout.
keras.layers.RNNkeras.layers.GRUkeras.layers.LSTMkeras.layers.Bidirectional