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

TensorFlow · Class

layers.SimpleRNN

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
unitsintPositive integer: number of units in the recurrent layer.
activationstring or callable or NoneActivation function; defaults to tanh, or no activation when None.
use_biasboolWhether to use a bias vector; defaults to True.
kernel_initializerinitializerInitializer for the input kernel.
recurrent_initializerinitializerInitializer for the recurrent kernel; defaults to orthogonal.
bias_initializerinitializerInitializer for the bias vector.
kernel_regularizerregularizer or NoneRegularizer for the input kernel.
recurrent_regularizerregularizer or NoneRegularizer for the recurrent kernel.
bias_regularizerregularizer or NoneRegularizer for the bias vector.
activity_regularizerregularizer or NoneRegularizer for the layer output.
kernel_constraintconstraint or NoneConstraint applied to the input kernel.
recurrent_constraintconstraint or NoneConstraint applied to the recurrent kernel.

Returns

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.

Key points

  • SimpleRNN is a fully connected recurrent layer whose output becomes recurrent input at the next timestep.
  • return_sequences selects between the final timestep output and outputs for every timestep.
  • The return_sequences option is Boolean and defaults to False.
  • The default activation function is hyperbolic tangent, tanh.
  • dropout and recurrent_dropout are fractions constrained to the interval from zero through one.
  • The training argument controls whether the layer operates in training or inference mode.

Common mistakes

  • Setting unroll=True may improve speed, but increases memory use.
  • Passing activation=None makes the recurrent transformation linear instead of applying tanh.
  • dropout applies to the input transformation, not to the recurrent transformation; use recurrent_dropout for the latter.

Example: Return the sequence and 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.

When to use it

Summarize a sequence with the final output

Sequence classification

Produce one representation per timestep

Sequence labeling or forecasting

Carry recurrent state across batches

Stateful streaming workloads

Interview questions

QWhat does SimpleRNN do?

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.

QHow do return_sequences and return_state affect outputs?

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.

QWhich defaults and regularization controls matter when configuring the layer?

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.SimpleRNNCell
  • tf.keras.layers.RNN
  • tf.keras.layers.GRU
  • tf.keras.layers.LSTM

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