Keras
deep learning
neural networks
layers
machine learning

Keras confusion about number of layers

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

Counting layers in Keras sounds easy until you build anything beyond a tiny Sequential model. The confusion comes from the fact that Keras tracks layer instances, graph inputs, wrappers, and reused components differently depending on which API you inspect.

What model.layers Actually Counts

The model.layers list contains layer objects that belong to the model. In a simple sequential stack, that often matches what people think of as "the number of layers."

python
1from keras import Sequential
2from keras.layers import Dense
3
4model = Sequential([
5    Dense(16, activation="relu", input_shape=(8,), name="dense_1"),
6    Dense(8, activation="relu", name="dense_2"),
7    Dense(1, name="output"),
8])
9
10print(len(model.layers))           # 3
11print([layer.name for layer in model.layers])

In this example, the count is straightforward because each call in the stack adds one distinct layer instance.

Why the Input Can Be Confusing

In functional models, the graph starts with an input tensor created by keras.Input. People often expect that input placeholder to count as a normal layer, but different displays present it differently.

python
1from keras import Input, Model
2from keras.layers import Dense
3
4inputs = Input(shape=(8,), name="features")
5x = Dense(16, activation="relu", name="hidden")(inputs)
6outputs = Dense(1, name="score")(x)
7model = Model(inputs, outputs)
8
9print(len(model.layers))
10for layer in model.layers:
11    print(layer.name, layer.__class__.__name__)

You will usually see an InputLayer plus the real computational layers. If someone says "this model has two layers," they may be ignoring the input placeholder and counting only trainable transforms. If Keras reports three layer objects, it is including the input layer.

Shared Layers Count Once as Objects

The Functional API supports shared layers, which is where confusion becomes more serious. A single layer instance can be called more than once in the graph.

python
1from keras import Input, Model
2from keras.layers import Dense, Concatenate
3
4shared = Dense(4, activation="relu", name="shared_dense")
5
6left = Input(shape=(6,), name="left")
7right = Input(shape=(6,), name="right")
8
9left_features = shared(left)
10right_features = shared(right)
11merged = Concatenate(name="merge")([left_features, right_features])
12output = Dense(1, name="prediction")(merged)
13
14model = Model([left, right], output)
15
16print(len(model.layers))
17for layer in model.layers:
18    print(layer.name)

Here the shared Dense layer is used twice, but it is still one layer instance. Keras counts it once in model.layers because the weights are shared. If you are mentally counting graph edges or calls, you might expect a higher number than Keras reports.

Wrappers and Containers Add Another Layer of Meaning

Some Keras layers wrap other behavior. For example, TimeDistributed(Dense(...)) appears as one wrapper layer in the model, even though it contains an inner Dense transform.

python
1from keras import Sequential
2from keras.layers import Dense, TimeDistributed, Input
3
4model = Sequential([
5    Input(shape=(5, 3)),
6    TimeDistributed(Dense(4), name="td_dense"),
7    Dense(1, name="head"),
8])
9
10print(len(model.layers))
11for layer in model.layers:
12    print(layer.name, layer.__class__.__name__)

If you are counting conceptual operations, you may think of the inner Dense as a layer too. Keras, however, counts the wrapper layer instance that sits in the model graph.

Use the Right Question

A lot of confusion disappears when you ask a more precise question. Do you want:

  • the number of layer objects in the model?
  • the number of trainable transformations?
  • the number of times layers are called in the graph?
  • the number of parameterized layers?

Those are related, but not identical.

model.summary() is often the best starting point because it shows layer names, output shapes, and parameter counts. If the architecture uses reused layers or multiple inputs, inspect both model.layers and the model diagram rather than relying on a single integer.

What to Count in Practice

For high-level discussion, teams often ignore the input placeholder and count only computational layers. For debugging or introspection code, use len(model.layers) because that is the concrete Keras object list.

If you need only trainable layers, filter by layer.trainable_weights:

python
1trainable_layer_names = [
2    layer.name for layer in model.layers if layer.trainable_weights
3]
4
5print(trainable_layer_names)

That is usually more meaningful than arguing over whether an input layer or merge layer "counts."

Common Pitfalls

The biggest mistake is assuming every use of a shared layer creates a new layer entry. It does not. Reusing the same object shares weights and keeps one layer instance.

Another common issue is mixing conceptual counting with Keras object counting. Someone may say "two dense layers" while model.layers returns three because it includes InputLayer.

People also forget that wrappers, merge layers, and normalization layers are still layers in the model graph. If you compare architectures, agree on the counting method first.

Summary

  • 'model.layers counts Keras layer objects, not every conceptual operation you may imagine.'
  • Functional models often include an InputLayer, which can make counts look one higher than expected.
  • Shared layers are counted once because one layer object is reused across multiple paths.
  • Wrapper layers and merge layers still appear as layers in the graph.
  • When the count matters, define exactly what you mean before comparing models.

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.