What exactly does this command do. model2.layers[4]

When going through the notebook at week 2 exercise 2 I didn’t get what this line meant. base_model = model2.layers[4]. What exactly is the base_model and whats does layers[4] mean. Thank you for your time

If you have never read through the overview of TensorFlow and Keras Layers and Models, you should do.

Basically, a Layer is a unit of computation. It takes in one or more Tensor inputs and performs some kind of operation or transformation. This operation is performed inside the Layer’s call() function.


Keras Model groups Layers into an object that has training and inference behaviors. It has an attribute that is a collection of Layers, named, appropriately enough, layers.


You can reference a particular layer in a model by performing an indexed lookup on the model’s layers as in a_layer = my_model.layers[index]. What you get back from that lookup is guaranteed to be of type Layer. In the Keras class hierarchy class Model inherits from class Layer, in object-oriented lingo a Model is-a Layer, so that Layer you get back could also be of type Model. It looks like that is the assumption here. Basically you’re leveraging the class hierarchy to define a model using layers, at least one of which is itself a model.

It might make sense to define a simple, or base_model, then extend its behavior by stacking it with other Layer instances. For example, if you always grouped Conv2D, Pooling, and ReLU, you could define a model with those three layers, the stack them repeatedly as one object. There are some existing threads that have examples of this kind of stacking. Here is one:

I had a model that performed sentiment analysis. It required a specific kind of encoded input that was not at all human readable. I built a new model by putting a text encoding layer in front of it. In that case, my ‘base’ model would have been accessible through extended_model.layers[1]


Ai_curious has given you all the background info to understand that code. Armed with that knowledge, you can look at the code in the notebook to see how model2 is defined. It’s a Keras Model object that was returned by a call to alpaca_model. If you look at the internals of alpaca_model, you’ll see that the fifth layer is the object returned from this call:

base_model = tf.keras.applications.MobileNetV2(input_shape= input_shape, 
                                                   include_top=False, # <== Important!!!!
                                                   weights='imagenet') # From imageNet

Of course indexing in python is 0-based, so myArray[4] is the fifth element of the array myArray.

Also note that the call I showed there does not explain why it is the 5th layer: it just shows what the definition of base_model is. You have to study the rest of the alpaca_model function to see what is done with that object.