TensorFlow
recurrent neural network
\`RNN\`
machine learning
neural networks

TensorFlow simple recurrent neural network

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

A simple recurrent neural network, or Simple RNN, is the most basic recurrent layer in TensorFlow and Keras. It is useful when you want to learn how sequence models work, build a lightweight baseline, or process short ordered sequences before deciding whether a more capable layer such as GRU or LSTM is necessary.

What a Simple RNN Learns

A dense layer treats an input sample as one fixed feature vector. A recurrent layer is different: it consumes a sequence one timestep at a time and carries a hidden state forward. That hidden state acts as a compressed memory of what the model has seen so far.

In TensorFlow, the basic layer is tf.keras.layers.SimpleRNN:

python
1import tensorflow as tf
2
3layer = tf.keras.layers.SimpleRNN(8)
4print(layer)

The value 8 is the number of recurrent units, which also controls the size of the hidden representation the layer carries between timesteps.

This memory is what makes recurrent models different from ordinary feed-forward models. If the order of observations matters, a recurrent layer can learn patterns that a plain dense network would miss.

Get the Input Shape Right

The most common source of confusion is shape. A Simple RNN expects three dimensions:

  • batch size
  • timesteps
  • features per timestep

If each training example contains 6 timesteps and each timestep has 2 numeric features, the layer input shape is (6, 2).

python
1import numpy as np
2import tensorflow as tf
3
4x = np.random.rand(10, 6, 2).astype("float32")
5
6model = tf.keras.Sequential([
7    tf.keras.layers.Input(shape=(6, 2)),
8    tf.keras.layers.SimpleRNN(8),
9    tf.keras.layers.Dense(1)
10])
11
12output = model(x)
13print(output.shape)

This model reads one sequence per sample and produces one output per sample. If you accidentally supply a two-dimensional array shaped like ordinary tabular data, Keras will complain because the timestep dimension is missing.

Build a Minimal Sequence Model

A small end-to-end example makes the idea concrete. Here is a toy binary classification model using sequence inputs:

python
1import numpy as np
2import tensorflow as tf
3
4x_train = np.random.rand(100, 5, 3).astype("float32")
5y_train = np.random.randint(0, 2, size=(100, 1)).astype("float32")
6
7model = tf.keras.Sequential([
8    tf.keras.layers.Input(shape=(5, 3)),
9    tf.keras.layers.SimpleRNN(16, activation="tanh"),
10    tf.keras.layers.Dense(1, activation="sigmoid")
11])
12
13model.compile(
14    optimizer="adam",
15    loss="binary_crossentropy",
16    metrics=["accuracy"]
17)
18
19model.fit(x_train, y_train, epochs=3, batch_size=8, verbose=0)

This is only toy data, but it shows the essential workflow: sequences go in, the recurrent layer summarizes them, and the final dense layer maps that summary to a prediction.

By default, SimpleRNN returns the final output after the last timestep. That is often what you want for classification tasks where one prediction summarizes the whole sequence.

Return the Whole Sequence When Needed

Sometimes you do not want only the final summary. If a later layer needs access to every timestep output, enable return_sequences=True.

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(5, 3)),
5    tf.keras.layers.SimpleRNN(8, return_sequences=True),
6    tf.keras.layers.SimpleRNN(4),
7    tf.keras.layers.Dense(1)
8])

Now the first recurrent layer emits an output at every timestep, which makes stacking recurrent layers possible. This setting is not automatically better; it is only necessary when the next layer needs the full sequence rather than a single final state.

When a Simple RNN Is and Is Not Enough

Simple RNNs are good for:

  • learning recurrent model basics
  • quick sequence baselines
  • short and relatively simple temporal dependencies
  • small examples where interpretability matters more than raw performance

They are less effective on long sequences because information can fade as it is passed forward many times. That is why GRU and LSTM layers are more common in practical work with longer dependencies.

So a Simple RNN is often a good first step, but not always the final model you ship.

Common Pitfalls

The biggest mistake is forgetting that recurrent layers need three-dimensional input. Most early errors come from feeding tabular shape into a sequence layer.

Another issue is assuming return_sequences=True should always be enabled. It should be enabled only when the next stage of the model actually needs per-timestep outputs.

Developers also expect a Simple RNN to remember long histories as well as an LSTM or GRU. It usually will not. If the task depends on long-range context, the simple layer is often too weak.

Finally, toy random data is fine for learning the API, but real sequence work usually needs meaningful normalization, train-validation separation, and carefully chosen sequence lengths.

Summary

  • 'tf.keras.layers.SimpleRNN is the basic recurrent layer in TensorFlow and Keras.'
  • It expects input shaped as batch, timesteps, and features.
  • By default it returns one final sequence summary, but return_sequences=True returns outputs for every timestep.
  • Simple RNNs are useful for learning and for short-sequence baselines.
  • For longer dependencies, GRU or LSTM layers are often better next steps.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.