C1_W1_Lab_3_densenet

Get the convolutional layers and print the first 5

conv2D_layers = [layer for layer in base_model.layers
if str(type(layer)).find(‘Conv2D’) > -1]
print(“The first five conv2D layers”)
conv2D_layers[0:5]

Hi, I do not understand the logic behind how conv2D_layers are found, specifically this part-> str(type(layer)).find('Conv2D) > -1. Will anyone care to explain the code. Thank you!

I am not quite familiar with this either but what I understand for the line is this:

whenever you find a layer in the model type(layer) convert it to string, and then find the occurrence of the keyword ‘Conv2D’. The >-1 means before the end of all the layers.

Hi @chewyh ,

The base model contains the layers. Each layer has a specific type.

type(layer) gives the type of the layer. This layer type is converted to string using str str(type(layer). Now we do find Conv2D in each string.
This str(type(layer)).find(‘Conv2D’) > -1 statement is a conditional statement and if Conv2D is there in the string the its index is returned using str(type(layer)).find(‘Conv2D’) . Now as you know index is always 0 or greater than 0 (returned index are not starting from end so 0 or greater than 0). This only includes layers which are Conv2D.

See here the second column represents the layer type.

If you have any further doubt, please mention. And if your doubt is clear please confirm.

Thank you,
Harsh Der.