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

Keras · Class

layers.SimpleRNN

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.

How it works

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.

Syntax

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
unitsintNumber of recurrent units.
activationstr or callableActivation function; defaults to tanh.
use_biasboolWhether to use a bias vector.
kernel_initializerinitializerInitializer for the input kernel.
recurrent_initializerinitializerInitializer for the recurrent kernel.
bias_initializerinitializerInitializer for the bias vector.
kernel_regularizerregularizerRegularizer for the input kernel.
recurrent_regularizerregularizerRegularizer for the recurrent kernel.
bias_regularizerregularizerRegularizer for the bias.
activity_regularizerregularizerActivity regularizer.
kernel_constraintconstraintConstraint for the input kernel.
recurrent_constraintconstraintConstraint for the recurrent kernel.

Returns

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.

Key points

  • SimpleRNN is a fully connected recurrent layer whose prior output becomes the next recurrent input.
  • The default activation is tanh, and activation can be replaced with another configured function.
  • dropout specifies the fraction of input-transformation units dropped during training; it defaults to 0.
  • return_sequences chooses between one final output and outputs for all timesteps.
  • return_state adds the final recurrent state to the returned results and is disabled by default.
  • The sequence input must be three-dimensional: batch size, timestep count, and feature count.

Common mistakes

  • Setting go_backwards=True reverses the processing direction and the returned sequence.
  • stateful=True carries each sample's final state into the same batch index of the next batch, so batch ordering and boundaries must be controlled.
  • The training argument controls training-versus-inference behavior at call time, which matters when dropout is enabled.

Example: Sequence regression with SimpleRNN

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.

When to use it

Classify or regress from the final recurrent output

Sequence-to-one prediction

Produce an output at every timestep with return_sequences

Sequence-to-sequence modeling

Carry recurrent state between batches when ordered batches represent contiguous data

Stateful streaming

Interview questions

QWhat computation pattern does SimpleRNN implement, and what is its default output?

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.

QWhat do return_sequences and return_state control?

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.

QHow do activation, bias, and dropout settings affect SimpleRNN?

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

Source: SimpleRNN layer - Keras. Checked 2026-10-15.