Keras
deep learning
neural networks
model layers
machine learning

Keras find out the number of layers

Master System Design with Codemia

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

Introduction

If you want the number of layers in a Keras model, the usual answer is len(model.layers). That works for most models, but it helps to know exactly what Keras includes in model.layers, especially when you have nested models or are comparing the count with what model.summary() shows.

The Basic Way to Count Layers

For a standard Sequential model:

python
1import tensorflow as tf
2
3model = tf.keras.Sequential([
4    tf.keras.layers.Input(shape=(8,)),
5    tf.keras.layers.Dense(16, activation="relu"),
6    tf.keras.layers.Dropout(0.2),
7    tf.keras.layers.Dense(1),
8])
9
10print(len(model.layers))
11print(model.layers)

In many cases, this prints 3 because Keras tracks the actual layers in the model and does not always count the Input specification as a standalone layer object in the list.

That is the first subtlety: what you wrote in code is not always identical to what appears in model.layers.

Inspect the Layers Directly

If you need more than just the count, iterate over the list:

python
for index, layer in enumerate(model.layers, start=1):
    print(index, layer.name, layer.__class__.__name__)

This gives you a useful debugging view, especially when a model is assembled dynamically.

Typical output might look like:

python
1 dense Dense
2 dropout Dropout
3 dense_1 Dense

That is usually more informative than a raw integer because it tells you which layers Keras is actually tracking.

Functional Models Work the Same Way

The same property exists on Functional models:

python
1inputs = tf.keras.Input(shape=(4,))
2x = tf.keras.layers.Dense(8, activation="relu")(inputs)
3outputs = tf.keras.layers.Dense(2)(x)
4
5model = tf.keras.Model(inputs=inputs, outputs=outputs)
6
7print(len(model.layers))
8for layer in model.layers:
9    print(layer.name)

In a Functional model, you may see an explicit input layer such as input_1 in model.layers, so the count can differ from a simple Sequential example.

That is why "How many layers are there?" sometimes depends on whether you mean:

  • layers Keras tracks in model.layers
  • only trainable or weighted layers
  • conceptual layers in your architecture diagram

Counting Only Certain Kinds of Layers

Sometimes you do not want every layer. For example, you may want to count only trainable layers or only layers with weights.

Count trainable layers:

python
trainable_count = sum(1 for layer in model.layers if layer.trainable)
print(trainable_count)

Count layers that have weights:

python
weighted_count = sum(1 for layer in model.layers if layer.weights)
print(weighted_count)

This distinction matters because layers such as Dropout and Activation affect the model but do not necessarily own trainable weights.

Nested Models Need Extra Care

If you use a model as a layer inside another model, len(model.layers) counts the nested model as one layer at the top level.

Example:

python
1base = tf.keras.Sequential([
2    tf.keras.layers.Dense(8, activation="relu"),
3    tf.keras.layers.Dense(4, activation="relu"),
4], name="base_model")
5
6inputs = tf.keras.Input(shape=(6,))
7x = base(inputs)
8outputs = tf.keras.layers.Dense(1)(x)
9model = tf.keras.Model(inputs, outputs)
10
11print(len(model.layers))
12for layer in model.layers:
13    print(layer.name, layer.__class__.__name__)

If you want a flattened count across nested submodels, you need to traverse recursively instead of relying on the top-level list alone.

model.summary() Versus len(model.layers)

Developers sometimes compare len(model.layers) with the number of rows in model.summary() and get confused. The summary output may include input layers or nested structures in a way that does not match their mental model.

When precision matters, inspect model.layers directly and decide what definition of "layer count" you want to use in your project.

Common Pitfalls

The most common mistake is assuming the Input specification is always counted the same way in every model style. Sequential and Functional models can present this differently.

Another issue is counting all layers when you really care only about trainable layers. Dropout, Flatten, and other utility layers may inflate the count relative to what you mean architecturally.

Nested models are another source of confusion. A submodel may appear as one top-level layer even though it contains many internal layers.

Finally, do not rely only on a printed summary when debugging programmatically. model.layers is the API you should inspect in code.

Summary

  • Use len(model.layers) for the standard Keras layer count.
  • Inspect model.layers directly when you need to know exactly what Keras included.
  • Sequential and Functional models may expose input layers differently.
  • Count trainable or weighted layers separately if that is the real question.
  • Nested models may require recursive traversal for a fully flattened count.

Course illustration
Course illustration

All Rights Reserved.