Understanding TF Model constructor

# Choose `mixed_7` as the last layer of your base model
last_layer = pre_trained_model.get_layer('mixed7')
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output

# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024, activation='relu')(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)                  
# Add a final sigmoid layer for classification
x = layers.Dense  (1, activation='sigmoid')(x)           

# Append the dense network to the base model
model = Model(pre_trained_model.input, x) 

The above code snippet is from the ungraded lab in C2-W3. The syntax to append our layer with the pre-trained model using the Model constructor (Model(pre_trained_model.input, x) is very odd to me maybe coming from a C/C++ background. I would have expected to look more like Model(pre_trained_model,x) instead of specifically using pre_trained_model.input.

The first argument of the Model constructor is the input and the 2nd argument is the output of the model. Since we only pass in the input layer in the pre-trained model yet it was able to get every layers in the pre-trained model starting from the pre_trained_model.input and ends at the layer x.output.

Only way I can make sense of this constructor is by thinking of the Model constructor as Model(layerStart, LayerEnd) . I am not sure if this is correct or not.

1 Like

Tensorflow creates a model from the input layer(s) upto and including the output layer(s). You can read about functional api here

I am stuck on the Model constructor in the C2W3 graded assignment.
In the ungraded lab the model = Model(pre-trained_model.input, x).
In the graded lab you are asking for inputs to Model (inputs= and outputs=.
I used inputs=pre_trained_model and outputs= x.
Apparently that is wrong.

A model object has inputs and outputs attributes. It’s a valid approach to use these when creating a new model from an existing model. Keep in mind that the final model to create has the same inputs as the pretrained model.

Please remove code from posts / replies. It’s okay to share stacktrace though.

Still same problem. What do you make of this error while using attributes :"pre_trained_model.input’ and “x”
AttributeError Traceback (most recent call last)

in 1 # Save your model in a variable ----> 2 model = create_final_model(pre_trained_model, last_output) 3 4 # Inspect parameters 5 total_params = model.count_params()

in create_final_model(pre_trained_model, last_output) 17 18 # Add a fully connected layer with 1024 hidden units and ReLU activation —> 19 x = layers.dense(1024, activation= ‘relu’)(x) 20 # Add a dropout rate of 0.2 21 x = layers.dropout (0.2)(x)

AttributeError: module ‘keras.api._v2.keras.layers’ has no attribute ‘dense’

Please pay attention to the case. It’s keras.layers.Dense

Thank you for the help!

do we need to use tf.keras.layers here for defining x???

Your question is related to the import keyword. Please become familiar with this construct before moving forward.

yes that part I understood. I took the suggestion from your previous comments in this thread and implemented and cleared the grader.

now I need help for week 4 assignment, my model is giving training accuracy of only 86% and validation accuracy of 94%. can you suggestion where I could go which would improve. I used only 2 layers for the model as it was mentioned use no more than 2

Please do 2 things:

  1. Create a public topic with your question and the stacktrace if available. Don’t post code.
  2. Click my name and message your notebook as an attachment or link to google colab.

Tagging / mentioning me on the public topic is okay as well.

Ok :frowning: I will do so. please check the post.

Discussing unrelated assignments on the same topic is likely to confuse other learners.

Here’s the community user guide to get started.

Yes sir I know, sorry :(. I have created a new topic and tagged you. please have a look.

Thank You
DP