keras - difference between layers_by_depth and layers

Viewed 462

I want to compute the number of model layers. I use the two methods:

len(model.layers)

and

len(model.layers_by_depth)

but I get different results. What is the difference between layers_by_depth and layers? Thank you!

2 Answers

model.layers gives you a list of all layers. They are pretty much in order as you've added them.

model.layers_by_depth (seems to have been removed in newer versions) will give you a dict with a list of layers for each depth (key).

model.layers_by_depth will be pretty similar for linear models (i.e. each layer has one in/output) but be different for non-linear models (e.g. inception nets). Here layers will be grouped by depth. If you have a layer in depth n that outputs to two layers, you will find those two new layers in the list at depth n+1, instead of the non grouped model.layers.

You can read the source code to find how layers_by_depth is built:

# Build a dict {depth: list of layers with this depth}
layers_by_depth = {}
for layer, depth in layers_depths.items():
    if depth not in layers_by_depth:
        layers_by_depth[depth] = []
layers_by_depth[depth].append(layer)

and how self.layers is built:

# Set self.layers and self.layers_by_depth.

layers = []
for depth in depth_keys:
    layers_for_depth = layers_by_depth[depth]
    # Container.layers needs to have a deterministic order:
    # here we order them by traversal order.
    layers_for_depth.sort(key=lambda x: layer_indices[x])
    for layer in layers_for_depth:
        layers.append(layer)
self.layers = layers
self.layers_by_depth = layers_by_depth
Related