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:
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:
This gives you a useful debugging view, especially when a model is assembled dynamically.
Typical output might look like:
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:
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:
Count layers that have weights:
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:
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.layersdirectly 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.

