Is there a way to get the number of layers (not parameters) in a Keras model?
model.summary()
is very informative, but it is not straightforward to get the number of layers from it.
Is there a way to get the number of layers (not parameters) in a Keras model?
model.summary()
is very informative, but it is not straightforward to get the number of layers from it.
model.layers
will give you the list of all layers. The number is consequently len(model.layers)
To get a graphical view of the layer you can use:
from keras.utils.vis_utils import plot_model
plot_model(model, to_file='layers_plot.png', show_shapes=True, show_layer_names=True)
You will need to pip install pydot
and down load and install graphviz
from https://graphviz.gitlab.io/download/
Attached is a
sample output image
Although it has been a couple of years since this question was asked, today I ran into the same requirement. As @Nickolas mentioned, the given answers do not account for submodels. Here is a recursive function to find the total number of layers in a model, including submodels:
def count_layers(model):
num_layers = len(model.layers)
for layer in model.layers:
if isinstance(layer, tf.keras.Model):
num_layers += count_layers(layer)
return num_layers