Summarize a sequence with the final output
Sequence classification
TensorFlow · Class
tf.keras.layers.SimpleRNN is a fully connected recurrent layer that feeds each output back into the next timestep. It can emit the final output, the full sequence, and optionally the final recurrent state.
SimpleRNN processes a sequence with a fully connected recurrent cell, feeding each timestep's output back as the next recurrent input. It applies the configured activation and can return either only the last output or the complete timestep sequence, optionally alongside the final state.
tf.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 | Positive integer: number of units in the recurrent layer. |
activation | string or callable or None | Activation function; defaults to tanh, or no activation when None. |
use_bias | bool | Whether to use a bias vector; defaults to True. |
kernel_initializer | initializer | Initializer for the input kernel. |
recurrent_initializer | initializer | Initializer for the recurrent kernel; defaults to orthogonal. |
bias_initializer | initializer | Initializer for the bias vector. |
kernel_regularizer | regularizer or None | Regularizer for the input kernel. |
recurrent_regularizer | regularizer or None | Regularizer for the recurrent kernel. |
bias_regularizer | regularizer or None | Regularizer for the bias vector. |
activity_regularizer | regularizer or None | Regularizer for the layer output. |
kernel_constraint | constraint or None | Constraint applied to the input kernel. |
recurrent_constraint | constraint or None | Constraint applied to the recurrent kernel. |
By default, a tensor containing the final timestep output with shape (batch_size, units). With return_sequences=True, returns the full output sequence; with return_state=True, also returns the final state.
import numpy as np
import tensorflow as tf
x = np.random.random((32, 10, 8)).astype("float32")
layer = tf.keras.layers.SimpleRNN(4, return_sequences=True, return_state=True)
sequence, state = layer(x)
print(sequence.shape) # (32, 10, 4)
print(state.shape) # (32, 4)Creates a four-unit SimpleRNN, returns one output per timestep plus the final state, and runs it on a batch of ten-step, eight-feature sequences.
Sequence classification
Sequence labeling or forecasting
Stateful streaming workloads
The layer is a fully connected recurrent neural network. At each timestep it uses the current input and recurrent state, applies the configured activation, and feeds the resulting output into the next timestep.
Follow-up: How do you obtain every timestep output rather than only the final one? Set return_sequences=True; the layer then returns a sequence with one output vector per timestep.
With return_sequences=False, the layer returns the output for the final timestep, shaped as (batch_size, units). With return_sequences=True, it returns the full sequence instead.
Follow-up: What additional result can be requested? Setting return_state=True also returns the final recurrent state.
The default activation is tanh, dropout defaults to zero, and the default recurrent initializer is orthogonal. The layer accepts separate dropout controls for input and recurrent transformations.
Follow-up: What trade-off does unroll=True introduce? It can speed up execution but generally consumes more memory.
tf.keras.layers.SimpleRNNCelltf.keras.layers.RNNtf.keras.layers.GRUtf.keras.layers.LSTM