tensorflow
RNN
machine learning
neural networks
tutorial

Minimal `RNN` example in tensorflow

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

A minimal RNN example is useful because recurrent layers make sense only when the input shape is right. In TensorFlow, the easiest way to learn the mechanics is to build a tiny tf.keras sequence model with a very small synthetic dataset and inspect how samples, time steps, and features flow through the layer.

RNN Inputs Are Three-Dimensional

Unlike a dense layer, an RNN does not expect a flat feature vector. It expects input shaped like:

  • batch size
  • time steps
  • features per time step

If each sample contains 4 time steps and 1 numeric feature at each step, the layer input shape is (4, 1). The batch dimension is omitted from input_shape because Keras handles it automatically.

That shape rule is the first thing to check whenever an RNN example does not work.

Build a Minimal Binary Classifier

The example below trains a tiny recurrent network to classify short sequences as positive or negative.

python
1import numpy as np
2import tensorflow as tf
3
4x_train = np.array(
5    [
6        [[1.0], [1.0], [1.0], [1.0]],
7        [[1.0], [0.0], [1.0], [0.0]],
8        [[-1.0], [-1.0], [-1.0], [-1.0]],
9        [[-1.0], [0.0], [-1.0], [0.0]],
10    ],
11    dtype="float32",
12)
13
14y_train = np.array([1, 1, 0, 0], dtype="float32")
15
16model = tf.keras.Sequential(
17    [
18        tf.keras.layers.Input(shape=(4, 1)),
19        tf.keras.layers.SimpleRNN(8),
20        tf.keras.layers.Dense(1, activation="sigmoid"),
21    ]
22)
23
24model.compile(
25    optimizer="adam",
26    loss="binary_crossentropy",
27    metrics=["accuracy"],
28)
29
30model.fit(x_train, y_train, epochs=20, verbose=0)
31
32predictions = model.predict(x_train, verbose=0)
33print(predictions)

This is intentionally small, but it shows the full pattern:

  • sequence input
  • recurrent layer
  • output layer
  • training loop
  • prediction

What the RNN Layer Is Learning

SimpleRNN(8) means the model keeps an internal state vector of size 8 while scanning the sequence one step at a time. At each step, the layer combines:

  • the current input value
  • the state carried from the previous step

After the last time step, the final state becomes the summary that the dense output layer uses for classification.

This is what makes recurrent models different from ordinary feed-forward models. Order matters. The same values in a different temporal arrangement can lead to a different internal state.

Predict on a Single Sequence

At prediction time, the model still expects a batch. Even if you want to classify one sequence, the input must keep the outer batch dimension.

python
sample = np.array([[[1.0], [0.5], [0.5], [0.0]]], dtype="float32")
score = model.predict(sample, verbose=0)
print(score)

That outer list creates a batch of size 1. Without it, the tensor rank is wrong and Keras will complain.

When to Move Beyond SimpleRNN

SimpleRNN is a good teaching layer because it is easy to understand. For real tasks with longer sequences, developers often switch to LSTM or GRU because those architectures handle longer dependencies more reliably.

The nice part is that once the input shape is correct, changing the recurrent layer is easy:

python
1model = tf.keras.Sequential(
2    [
3        tf.keras.layers.Input(shape=(4, 1)),
4        tf.keras.layers.GRU(8),
5        tf.keras.layers.Dense(1, activation="sigmoid"),
6    ]
7)

That is one reason a minimal example is valuable. It teaches the shape contract first, which is the hardest part for most beginners.

Common Pitfalls

  • Passing 2D input to an RNN instead of a 3D tensor shaped as batch, time, and features.
  • Forgetting that input_shape excludes the batch dimension.
  • Sending one sample to predict() without wrapping it in a batch.
  • Expecting a tiny SimpleRNN example to solve long-sequence problems well without switching to GRU or LSTM.
  • Debugging the optimizer before checking whether the input tensor shape is correct.

Summary

  • A minimal TensorFlow RNN example should focus on sequence shape before model complexity.
  • RNN inputs are three-dimensional: batch, time steps, and features.
  • 'SimpleRNN is a good starting point for learning how recurrent layers consume sequence data.'
  • Prediction still requires a batch dimension, even for one sample.
  • Once the shape contract is clear, upgrading to GRU or LSTM is straightforward.

Course illustration
Course illustration

All Rights Reserved.