C4 W1 Assignment 2

can you please help on the following error:
NameError Traceback (most recent call last)
----> 1 happy_model = happyModel()
2 # Print a summary for each layer
3 for layer in summary(happy_model):
4 print(layer)

in happyModel()
34 #input(shape=(64, 64, 3))
—> 35 ZeroPadding2D(padding = (3,3), input_shape = (64,64,3)),
36 Conv2D(filters = 32, kernel_size = (7, 7), strides = (1, 1)),
37 BatchNormalization(axis= 3),

NameError: name ‘ZeroPadding2D’ is not defined

I considered zeropadding2d is the first hidden layer and the model run did not issue an error but when the function happymodel() is being called the above error is generated, please help!

You need a prefix in front of the layer name. I think this is mentioned in the instructions.

Hi TMosh,
thanks for the feedback. I was reading about the prefix and I found that since we are using the square brackets no need to add any prefix and in case we use the normal brackets we need to use the prefix like model.add().

anyway I tried to use the prefix but got the same error:

in happyModel()
34 #input(shape=(64, 64, 3))
—> 35 model.add(ZeroPadding2D(padding = (3,3), input_shape = (64,64,3)))
36 model.add(Conv2D(filters = 32, kernel_size = (7, 7), strides = (1, 1)))
37 model.add(BatchNormalization(axis= 3))

NameError: name ‘ZeroPadding2D’ is not defined

please help!

Hi again,

I have tried the prefix tfl. then layer name and it worked. thanks a lot.
still I wonder how to match what I have read about the sequential model as I mentioned in the previous post.

Thanks a lot

Wrong prefix. Use tfl.

Please give a reference for where you read that.

Hi TMosh,

this is the place I have read about the model.
Guide to the Sequential model - Keras Documentation (faroit.com)

in my personal opinion, I feel like we need an extra course to the DLS dedicated for the TF to avoid searching for answers. I totally agree that it is all about getting familiar with TF and challenging ourselves but this can be happening through the course itself. :slight_smile:

Thanks for the link.
This course assumes you already learned about TensorFlow during Course 2.

Square brackets and parentheses are both normal parts of Python syntax, and neither one is related to package namespaces.

First, the square brackets are used to tell the model constructor that you are passing in a comma delimited list. Nothing at all to do with the namespace or paths to the definitions of the specific layer classes used within the list. Rather, those are defined by the import statement, in the example at the page you linked it is:

from keras.layers import Dense, Activation

See for example on this official TensorFlow doc page. The 3 Dense layer definitions are passed as a list using the comma delimiter within the square bracket, but because of the way import was used, the layer types are still qualified by the layers package name:

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Define Sequential model with 3 layers
model = keras.Sequential(
        layers.Dense(2, activation="relu", name="layer1"),
        layers.Dense(3, activation="relu", name="layer2"),
        layers.Dense(4, name="layer3"),

Notice the difference in the import statements from the two examples. The first one imports Dense and Activation explicitly. The second one just imports layers, so Dense must be qualified as layers.Dense when it is used.

Second, what you refer to as the prefix above is the name of the instance variable you created. It is fundamentally different from a TensorFlow or Keras package name.

Third, what you refer to as normal brackets, the parentheses, is the Python syntax for function call. Again, fundamentally different from any package name or namespace.

Hope this helps

I am so grateful for the comprehensive explanation. I am sure that whoever will read it will understand the difference way of calling the functions.

Appreciate your support.

1 Like