TensorFlow
`RNN`
Recurrent Neural Networks
Machine Learning
Deep Learning

How to get summary information on tensorflow `RNN`

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

When people ask for a summary of a TensorFlow RNN, they usually want one of two things: a structural summary of the model, or a clearer understanding of the sequence shapes flowing through it. In tf.keras, the standard answer is model.summary(), but recurrent layers add a few details that are easy to misread if you do not build the model first.

Build the Model Before Calling summary()

The summary output is only useful after Keras knows the input shape and has created the layer weights. For a Sequential or Functional model, that usually means declaring the input shape up front. For a subclassed model, it often means running one forward pass before asking for the summary.

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

That prints the layer names, output shapes, and parameter counts. If you skip the input shape on a model that has not been built yet, Keras cannot infer enough information to produce a full summary.

For subclassed models, the usual pattern is to call the model once with sample data.

python
1import tensorflow as tf
2
3class Classifier(tf.keras.Model):
4    def __init__(self):
5        super().__init__()
6        self.rnn = tf.keras.layers.LSTM(32)
7        self.out = tf.keras.layers.Dense(2)
8
9    def call(self, x):
10        return self.out(self.rnn(x))
11
12model = Classifier()
13_ = model(tf.random.normal((4, 15, 10)))
14model.summary()

The dummy call creates the variables and locks in the shapes needed for the report.

Read RNN Shapes Correctly

Most Keras recurrent layers expect input shaped as (batch, timesteps, features). That means a tensor such as (32, 20, 8) represents a batch of 32 sequences, each sequence containing 20 time steps, and each time step containing 8 features.

A summary becomes much easier to read once you map the output shape to the layer configuration.

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(12, 6))
4x = tf.keras.layers.GRU(10, return_sequences=True)(inputs)
5x = tf.keras.layers.GRU(4)(x)
6outputs = tf.keras.layers.Dense(3)(x)
7
8model = tf.keras.Model(inputs, outputs)
9model.summary()

In that example:

  • the first GRU returns a full sequence because return_sequences=True
  • the second GRU receives a sequence and returns only the final hidden state
  • the Dense layer runs on that final vector

If you turn off return_sequences too early, the next recurrent layer no longer receives a sequence and the model shape stops making sense.

Understand Why Parameter Counts Look Large

RNN summaries often surprise people because the parameter count is higher than expected. A recurrent layer does not just learn input weights. It also learns recurrent weights that connect one time step to the next.

For a simple recurrent layer, the learned weights include:

  • input-to-hidden weights
  • hidden-to-hidden recurrent weights
  • bias terms

For LSTM and GRU, the count is larger because the layer maintains multiple gates internally. A small change in the number of units can cause a large jump in total parameters.

You can inspect those weights directly when you need more than the printed table.

python
1import tensorflow as tf
2
3layer = tf.keras.layers.LSTM(8)
4_ = layer(tf.random.normal((2, 5, 3)))
5
6for weight in layer.weights:
7    print(weight.name, weight.shape)

That is useful when you want to verify that a layer was built with the expected unit count or input feature width.

Inspect More Than the Top-Level Summary

model.summary() is the first tool, not the only one. When debugging a recurrent network, it is often helpful to inspect input and output shapes directly and then compare them to the summary.

python
1import tensorflow as tf
2
3inputs = tf.keras.Input(shape=(7, 5), name="series")
4x = tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(6))(inputs)
5outputs = tf.keras.layers.Dense(1, activation="sigmoid")(x)
6
7model = tf.keras.Model(inputs, outputs)
8print("input:", model.input_shape)
9print("output:", model.output_shape)
10for layer in model.layers:
11    print(layer.name, getattr(layer, "output_shape", "unavailable"))

This approach helps when the model summary alone feels too compact, especially with bidirectional layers, masking, embeddings, or nested models.

If you need training-time inspection rather than architecture inspection, use TensorBoard metrics and traces. That is a different kind of summary from model.summary().

Common Pitfalls

A frequent mistake is calling summary() before the model is built. That usually happens with subclassed models or with Sequential models that never received an input shape.

Another common error is misunderstanding the recurrent input convention. In Keras, the shape is typically (batch, timesteps, features), not (timesteps, batch, features).

A third issue is forgetting return_sequences=True when stacking recurrent layers. The first recurrent layer then returns only one vector, and the next recurrent layer cannot consume it as a sequence.

Finally, developers sometimes expect summary() to explain runtime behavior such as exploding gradients, masking errors, or training instability. It will not. It only tells you what was built.

Summary

  • Use model.summary() to inspect RNN layer order, output shapes, and parameter counts.
  • Build the model first by declaring an input shape or running a sample forward pass.
  • Read recurrent inputs as (batch, timesteps, features).
  • Expect LSTM and GRU layers to have more parameters than a simple dense layer with the same unit count.
  • Use return_sequences=True when another recurrent layer still needs the full sequence.
  • Inspect individual layer weights and shapes when the top-level summary is not enough.

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.