C4W2: Transfer learning with Mobilenet UNQ_C3

Hi, in excersize 3 of the transfer learning assignment, Im really not understanding this part:

# UNQ_C3

base_model = model2.layers[4]

base_model.trainable = True

# Let's take a look to see how many layers are in the base model

print("Number of layers in the base model: ", len(base_model.layers))

how does this base_model = mode2.layers[4] work? what does it do?
and shouldnt len(base_model.layers) not only be equal 1? why does it equal 155?

and this code refreezes everything before fine_tune_at correct? so for this example we only refreeze the ~30 layers after 120? meaning only these layers will backpropagate and update weights? am I getting this right?

# Freeze all the layers before the `fine_tune_at` layer
for layer in base_model.layers[:fine_tune_at]:
    layer.trainable = False

finally how are the two new prediction layers plugged into the model?

Thank you

Hello, @Abbasid,

It takes the 4th layer out. The 4th “layer” is not a single layer. If you check out the summary of model2, or do print(model2.layer[4].name), you will get more idea what it is and why it contains 155, as you said, sub-layers.

Yes, before fine_tune_at=120, so it freezes from 0 up to (but not including) 120.

Layers that are freezed (those 120 layers) are not trainable. Layers that are not freezed (from 120-th layer up) are trainable and they have weights-update with back-prop.

Not sure what you mean by “plugged”.

Here is the story:

we are only selecting and freezing the first 120 sub-layers of the 4th layer of model2. The architecture of model2 is not changed except that those layers are freezed. Then you train the partly freezed model2, and get prediction from that model2.



so the 4th layer of model2 is the entire mobilenetv2? and I assume this was done in “alpaca_model” function, in this part of the code:

am I right? so x is mobilenetv2 model?

how does the rest of the code change or “add” the two final classification layers?

then what are the previous layers , "Sequential, TensorflowOpLayer, TensorflowOpLayer, and where are they defined? are they the augmentation and preprocessing steps?

sorry its alot of questions but this bit is a bit unclear to me
Thank you,

Not the entire model, the top layer (output is not included)!

Just follow the code stream top to bottom, its not 2 final classification layers, its just one layer with one neuron! Also you are not allowed to publish code solutions!

Yes, and the sequential is the type of model you are building here!

Not the entire model, the top layer (output is not included)!

so the original mobilenetv2 is 157 layers, this 4th functional layer is the first 155 layers minus the last two layers (classification head) ?

its not 2 final classification layers

I meant 2 final layers (GlobalAvgPooling - Dense) pooling is considered a layer but dropout isnt correct?
where/how did we tell the model to drop the last 2 layers?

Not 4 layers just the last top layer or output layer, everything else is intact. Output is only the dense layer.

I think you have difficulty in understanding the basic concepts, a rerun of the entire specialization is advisable for you.

this is what I said:

this 4th functional layer is the first 155 layers minus the last two layers (classification head) ?

this 4th layer not 4 layers,
I completed all the assignments of the specialization so far just fine, my question is just specific to adding the new classification head.

I think you have difficulty in understanding my question, and if so just say that, and spare me the condescending tone.

Hello, @Abbasid,


Above is the summary of the model you built for Exercise 2. In other words, these layers were there because you added them.

The 4th (zero-based) layer is Functional which is corresponding to the MobileNetV2 (without the top). You may print(len(model2.layers[4].layers)) to verify the number of sub-layers there, so I won’t check it.

@Abbasid, there is no code in the notebook that dropped any of these layers from model2. model2 had been having all of these layers from the moment it was created until the end of the notebook. Nobody has dropped any layers, so nobody needed to plug anything back.

The code you quoted in your first post in this thread only selectively make some of the sub-layers in the MobileNetV2 trainable. That code does not drop anything from model2. model2 always has the classification layer, so we don’t need to plug it back.

model2’s architecture had NEVER been changed from the moment you created it with your work in exercise 2.


In exercise 3, we trained model2, not only the basemodel. The only thing here is that some of the layers in the basemodel INSIDE our model2 had become trainable.

If there are other reasons that make you believe that some layers were dropped and needed to be plugged back, let us know :wink: , so we can directly address the concern.



The changes we had made to those 35 layers were on the base_model that resided in model2. In other words, we had been changing the model2 itself. At all time, there had been one and only one base_model instance and that one instance resided inside model2 and not anywhere else.

@Abbasid, we are all here trying to help :wink: :wink:


1 Like

Aha, ok that’s all my questions,

1 Like

This comment is not necessary in a public forum. We’re volunteers from the community, trying to be helpful.

1 Like

Indeed I didnt like his comment @TMosh thank you. @Abbasid I was trying to steer you into a right direction because it seemed to me you were lacking basic understanding and my first advice to everyone is to revisit the material again, I do myself the same sometimes. And this the same adviceI give to my university students too, without any prejudice! I am here to guide you in the right direction not do the heavy lifting, but its ok once in a while we get some of these responses!