TensorFlow
Keras
machine learning
deep learning
neural networks

Obtaining output of an Intermediate layer in TensorFlow/Keras

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

Sometimes the final prediction is not the thing you need. You may want the activations from a convolution block for visualization, the output of an encoder for feature extraction, or the values inside a dense layer to debug shape problems. In TensorFlow and Keras, the standard way to get those values is to build another Model whose outputs are the intermediate tensors you care about.

This is a normal workflow, not a hack. Keras models are graphs of tensors, so any intermediate layer output can be exposed through a second model that shares the same original inputs.

Build A Feature-Extraction Model

Assume you have a normal model:

python
1import numpy as np
2import tensorflow as tf
3from tensorflow import keras
4from tensorflow.keras import layers
5
6inputs = keras.Input(shape=(8,), name="features")
7x = layers.Dense(16, activation="relu", name="dense_1")(inputs)
8x = layers.Dense(8, activation="relu", name="dense_2")(x)
9outputs = layers.Dense(1, name="prediction")(x)
10
11model = keras.Model(inputs=inputs, outputs=outputs)

To extract the output of dense_2, create a second model that shares the same input but stops at that layer:

python
1intermediate_model = keras.Model(
2    inputs=model.input,
3    outputs=model.get_layer("dense_2").output,
4)
5
6sample = np.ones((2, 8), dtype="float32")
7activations = intermediate_model.predict(sample, verbose=0)
8print(activations.shape)

That is the canonical Keras solution. The original model and the intermediate model share weights, so you are not copying parameters or retraining anything.

Return Multiple Intermediate Layers

You can request several intermediate outputs at once:

python
1multi_output_model = keras.Model(
2    inputs=model.input,
3    outputs=[
4        model.get_layer("dense_1").output,
5        model.get_layer("dense_2").output,
6        model.output,
7    ],
8)
9
10dense_1_out, dense_2_out, final_out = multi_output_model(sample, training=False)
11print(dense_1_out.shape, dense_2_out.shape, final_out.shape)

This is useful for debugging, comparing transformations across layers, or building tools that visualize how information changes through the network.

Sequential Models Need To Be Built

A common failure mode is trying to access model.input too early. With a functional model created from keras.Input, the input tensor exists immediately. With some sequential models, it does not exist until the model has either been given an explicit input shape or been called once.

python
1seq = keras.Sequential(
2    [
3        layers.Input(shape=(8,)),
4        layers.Dense(16, activation="relu", name="dense_a"),
5        layers.Dense(4, activation="relu", name="dense_b"),
6    ]
7)
8
9extractor = keras.Model(inputs=seq.input, outputs=seq.get_layer("dense_b").output)
10print(extractor(np.ones((1, 8), dtype="float32")))

If you define a sequential model without an input and try to inspect it before the graph is built, Keras may raise an error because the symbolic tensors do not exist yet.

Use The Right Execution Mode

For inspection code, calling the extractor directly is often simpler than using predict:

python
features = intermediate_model(sample, training=False)
print(features.numpy())

Passing training=False matters when the model contains layers such as dropout or batch normalization. Without it, the activations may differ between training and inference mode, which can make debugging inconsistent.

Intermediate Outputs For Pretrained Models

The same pattern works for application models such as MobileNet or ResNet. In those cases, layer names are usually safer than numeric indexes because indexes become fragile if the architecture changes or you swap one base model for another. That small naming habit saves time when you revisit the code later.

Common Pitfalls

  • Trying to read model.input before a sequential model has been built.
  • Using the wrong layer name or relying on fragile numeric indexes.
  • Forgetting that dropout and batch normalization behave differently in training and inference modes.
  • Using predict everywhere when a direct call such as model(x, training=False) is simpler.
  • Assuming intermediate outputs are detached copies instead of tensors backed by the same model weights.

Summary

  • Build a second keras.Model with the original input and the intermediate output you want.
  • 'model.get_layer(...).output is safer than hard-coding layer indexes when names exist.'
  • You can extract one layer or many layers in a single pass.
  • Sequential models may need an explicit input shape or an initial call before extraction works.
  • Use training=False when you want stable inference-time activations.

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.