Keras
machine learning
neural networks
deep learning
layer outputs

Keras, How to get the output of each layer?

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

Inspecting intermediate layer outputs in Keras is essential for debugging model behavior, feature extraction, and explainability workflows. The clean way is to build an auxiliary model that shares the original input and exposes selected layer outputs. This gives deterministic introspection without modifying training graph definitions.

Build a Model That Returns All Layer Outputs

You can create a new model from original input to each layer output.

python
1import tensorflow as tf
2from tensorflow.keras import layers, models
3
4base = models.Sequential([
5    layers.Input(shape=(28, 28, 1)),
6    layers.Conv2D(8, 3, activation='relu', name='conv1'),
7    layers.MaxPool2D(),
8    layers.Conv2D(16, 3, activation='relu', name='conv2'),
9    layers.Flatten(),
10    layers.Dense(10, activation='softmax', name='classifier')
11])
12
13inspection_model = tf.keras.Model(
14    inputs=base.input,
15    outputs=[layer.output for layer in base.layers]
16)
17
18x = tf.random.normal((1, 28, 28, 1))
19outputs = inspection_model.predict(x)
20for layer, out in zip(base.layers, outputs):
21    print(layer.name, out.shape)

This gives a full view of feature transformations across the network.

Inspect Only Selected Layers

For large models, pulling every layer output may be expensive. Select only layers relevant to your diagnostic goal.

python
1selected_names = ['conv1', 'conv2']
2selected_outputs = [base.get_layer(name).output for name in selected_names]
3selected_model = tf.keras.Model(inputs=base.input, outputs=selected_outputs)
4
5selected = selected_model.predict(x)
6for name, out in zip(selected_names, selected):
7    print(name, out.mean(), out.std())

Focused inspection reduces memory overhead and improves debugging speed.

Use Outputs for Feature Extraction

Intermediate outputs can feed downstream models or clustering pipelines.

python
feature_model = tf.keras.Model(inputs=base.input, outputs=base.get_layer('conv2').output)
features = feature_model.predict(tf.random.normal((4, 28, 28, 1)))
print(features.shape)

This is common in transfer learning and representation analysis.

Evaluate Layer Activation Health

Statistics such as mean and sparsity can reveal dead activations or exploding magnitudes.

python
1import numpy as np
2
3for name, out in zip(selected_names, selected):
4    sparsity = np.mean(out == 0)
5    print(f"{name}: mean={out.mean():.4f}, std={out.std():.4f}, sparsity={sparsity:.4f}")

A quick activation report helps catch normalization or initialization issues.

Practical Workflow Tips

Run inspection with inference mode to avoid dropout randomness when comparing outputs. Keep one deterministic sample batch for regression checks. If model has batch norm, ensure behavior matches your analysis context.

Store inspection scripts separately from training loops so diagnostics remain reusable and easy to run in CI.

Functional API Example with Named Branches

Branching models are common in production and require selective output inspection from multiple paths.

python
1inp = layers.Input(shape=(32,))
2x1 = layers.Dense(16, activation='relu', name='branch_a')(inp)
3x2 = layers.Dense(16, activation='relu', name='branch_b')(inp)
4merged = layers.Concatenate(name='merge')([x1, x2])
5out = layers.Dense(1, activation='sigmoid', name='final')(merged)
6
7model = tf.keras.Model(inp, out)
8probe = tf.keras.Model(inp, [model.get_layer('branch_a').output, model.get_layer('merge').output])
9
10sample = tf.random.normal((2, 32))
11a_out, m_out = probe(sample)
12print(a_out.shape, m_out.shape)

Named layers make targeted diagnostics much easier.

Visual Debugging of Feature Maps

For convolutional models, visualizing feature maps can reveal dead filters or activation collapse.

python
1import matplotlib.pyplot as plt
2
3feat_model = tf.keras.Model(base.input, base.get_layer('conv1').output)
4feat = feat_model.predict(tf.random.normal((1, 28, 28, 1)))[0]
5
6plt.imshow(feat[:, :, 0], cmap='viridis')
7plt.title('conv1 channel 0')
8plt.show()

Visual checks complement numeric summary metrics.

Keep Diagnostics Separate from Training Code

Do not interleave heavy inspection logic with training loops. Maintain standalone scripts or notebooks for introspection so training performance and reproducibility stay clean.

This separation also makes debugging workflows reusable for future model versions.

Practical Regression Checks

Store a small fixed input batch and compare intermediate outputs between model versions. Unexpected activation shifts can indicate data pipeline or model export regressions before accuracy drops become visible.

Common Pitfalls

  • Trying to access intermediate tensors without creating a proper auxiliary model.
  • Dumping every layer output for very large models and exhausting memory.
  • Comparing outputs from training and inference modes without awareness.
  • Forgetting that some layers change behavior depending on batch size or mode.

Summary

  • Create a separate Keras model that exposes desired layer outputs.
  • Inspect selected layers for efficient debugging.
  • Use intermediate activations for feature extraction and diagnostics.
  • Keep inspection runs deterministic for reliable comparisons.

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.