Question about Keras model.layers

Hello all,

What exactly does this assignment mean?
base_model = model2.layers[4]

I found out that model.layers will return a shallow copy version of the layers list.
What I don’t understand is what the use the above ‘4’ causes on the copy.

The output for ‘Number of layers’ in the base model is 155

Thanks in advance.

1 Like

According to me, this assignment will make the base_model represent the 4th layer of your neural network out of the 155 layers.

1 Like

Wenn you run this code:

base_model = model2.layers[4]
for i in model2.layers:
    print(i)
print('------------------------------')
print(len(base_model.layers))
for i in base_model.layers:
    print(i)
print('------------------------------')

you will see this:

<tensorflow.python.keras.engine.input_layer.InputLayer object at 0x7f54685e0a10>
<tensorflow.python.keras.engine.sequential.Sequential object at 0x7f5518143150>
<tensorflow.python.keras.engine.base_layer.TensorFlowOpLayer object at 0x7f54685a45d0>
<tensorflow.python.keras.engine.base_layer.TensorFlowOpLayer object at 0x7f54685a3f50>
<tensorflow.python.keras.engine.functional.Functional object at 0x7f54685d9890>
<tensorflow.python.keras.layers.pooling.GlobalAveragePooling2D object at 0x7f54684c4490>
<tensorflow.python.keras.layers.core.Dropout object at 0x7f5468503ed0>
<tensorflow.python.keras.layers.core.Dense object at 0x7f54684189d0>
------------------------------
155
<tensorflow.python.keras.engine.input_layer.InputLayer object at 0x7f54a066f3d0>
<tensorflow.python.keras.layers.convolutional.ZeroPadding2D object at 0x7f548452a290>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f55441ccd10>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f548452af90>
<tensorflow.python.keras.layers.advanced_activations.ReLU object at 0x7f54a062e6d0>
<tensorflow.python.keras.layers.convolutional.DepthwiseConv2D object at 0x7f54844c7d10>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f54844c1210>
<tensorflow.python.keras.layers.advanced_activations.ReLU object at 0x7f54844ece50>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f54844ecf10>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f5484489750>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f548448cad0>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f54844a5250>
<tensorflow.python.keras.layers.advanced_activations.ReLU object at 0x7f5484492750>
<tensorflow.python.keras.layers.convolutional.ZeroPadding2D object at 0x7f54844b06d0>
<tensorflow.python.keras.layers.convolutional.DepthwiseConv2D object at 0x7f5484439290>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f5484449b50>
<tensorflow.python.keras.layers.advanced_activations.ReLU object at 0x7f5484452e50>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f548446a0d0>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f548446ffd0>
<tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7f5484474c90>
<tensorflow.python.keras.layers.normalization_v2.BatchNormalization object at 0x7f548440b350>
........
1 Like

The square brackets in Python are the list index operator. According to the Python doc, the operation s[i] results in i^{th} item of s , origin 0. Accordingly, model.layers[4] returns the 4 ^{th} object in the layers list, as suggested above by @Elemento

Notice in the printout generated by @nalixianren the 4 ^{th} layer is of type tensorflow.python.keras.engine.functional.Functional - that layer is itself an entire model, which is why when you iterate over base_model in the second loop you see another list of layers.