Course 4- Week-1 Assignment-2

Hi, I struck over with the error , please help .Thank you.

happy_model = happyModel()
# Print a summary for each layer
for layer in summary(happy_model):
print(layer)

output = [['ZeroPadding2D', (None, 70, 70, 3), 0, ((3, 3), (3, 3))],
        ['Conv2D', (None, 64, 64, 32), 4736, 'valid', 'linear', 'GlorotUniform'],
        ['BatchNormalization', (None, 64, 64, 32), 128],
        ['ReLU', (None, 64, 64, 32), 0],
        ['MaxPooling2D', (None, 32, 32, 32), 0, (2, 2), (2, 2), 'valid'],
        ['Flatten', (None, 32768), 0],
        ['Dense', (None, 1), 32769, 'sigmoid']]

ERROR:

comparator(summary(happy_model), output)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-36-f33284fd82fe> in <module>
----> 1 happy_model = happyModel()
      2 # Print a summary for each layer
      3 for layer in summary(happy_model):
      4     print(layer)
      5 

<ipython-input-35-127bf9124664> in happyModel()
     34 
     35             ## Flatten layer
---> 36             tfl.Flatten(tfl.MaxPool2D(pool_size=(0, 0), padding='valid')),
     37 
     38             ## Dense layer with 1 unit for output & 'sigmoid' activation

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/core.py in __init__(self, 
data_format, **kwargs)
    642   def __init__(self, data_format=None, **kwargs):
    643     super(Flatten, self).__init__(**kwargs)
--> 644     self.data_format = conv_utils.normalize_data_format(data_format)
    645     self.input_spec = InputSpec(min_ndim=1)
    646     self._channels_first = self.data_format == 'channels_first'

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/conv_utils.py in 
normalize_data_format(value)
    190   if value is None:
    191     value = backend.image_data_format()
--> 192   data_format = value.lower()
    193   if data_format not in {'channels_first', 'channels_last'}:
    194     raise ValueError('The `data_format` argument must be one of '

 AttributeError: 'MaxPooling2D' object has no attribute 'lower'

The instructions in the comments tell you to use the default parameters for the MaxPooling layer. That means you can just omit the arguments. You can specify them if you want, but you’ll need to read the documentation to find out the default pooling size. Hint: it’s not 0.

The next point is that you’re building a Sequential model here, so you don’t need to fill in the MaxPooling step inside the Flatten statement: the output of the previous layer (the MaxPooling) just becomes the input to the next layer in a “Sequential” model. That is the point of the Sequential class. Note that it will all be different when we get to the “Functional API”, but that’s in the next section.

Also please do us a favor and remove your source code from your post, once you have this figured out. We don’t want to leave source code sitting around in the forums.

Thank you, i have done with sequential . And i have removed the code.

1 Like

That’s great! Thanks for confirming and for removing the source code.

Thanks , in Functional API, i face a problem " tf " is not defined. In grading part, i followed instructions . Any suggestions, Thanks in advance.


NameError Traceback (most recent call last)
in
----> 1 conv_model = convolutional_model((64, 64, 3))
2 conv_model.compile(optimizer=‘adam’,
3 loss=‘categorical_crossentropy’,
4 metrics=[‘accuracy’])
5 conv_model.summary()

in convolutional_model(input_shape)
17 “”"
18
—> 19 input_img = tf.keras.Input(shape=input_shape)
20 ## CONV2D: 8 filters 4x4, stride of 1, padding ‘SAME’
21 # Z1 = None

NameError: name ‘tf’ is not defined

That probably means you’ve closed and reopened the notebook or done a “Kernel → Restart”. You just need to re-execute the previous cells including the “import” cell at the beginning. Try “Cell → Run All Above” and then run the failing cell again. You always need to recreate the “runtime” state anytime you reopen the notebook.

Hi Thanks, i restarted the kernel and i got a error:

ERROR:

     ---------------------------------------------------------------------------
     TypeError                                 Traceback (most recent call last)
    <ipython-input-61-f1284300b767> in <module>
    ----> 1 conv_model = convolutional_model((64, 64, 3))
          2 conv_model.compile(optimizer='adam',
          3                   loss='categorical_crossentropy',
          4                   metrics=['accuracy'])
          5 conv_model.summary()

<ipython-input-60-cbdaeaf9276a> in convolutional_model(input_shape)
     26     ## MAXPOOL: window 8x8, stride 8, padding 'SAME'
     27     # P1 = None
---> 28     P1= tfl.MaxPool2D(kernel_size = (8,8), strides = (8,8), padding = 'SAME')(A1)
     29     ## CONV2D: 16 filters 2x2, stride 1, padding 'SAME'
     30     Z2 = tfl.Conv2D(filters=16, kernel_size=(2,2), strides=(1, 1), padding ='SAME')(P1)

 /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/pooling.py in 
__init__(self, pool_size, strides, padding, data_format, **kwargs)
    459         nn.max_pool,
    460         pool_size=pool_size, strides=strides,
--> 461         padding=padding, data_format=data_format, **kwargs)
    462 
    463 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/pooling.py in 
__init__(self, pool_function, pool_size, strides, padding, data_format, name, **kwargs)
    270                padding='valid', data_format=None,
    271                name=None, **kwargs):
--> 272     super(Pooling2D, self).__init__(name=name, **kwargs)
    273     if data_format is None:
    274       data_format = backend.image_data_format()

/opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in 
 _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
--> 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in 
 __init__(self, trainable, name, dtype, dynamic, **kwargs)
    316     }
    317     # Validate optional keyword arguments.
--> 318     generic_utils.validate_kwargs(kwargs, allowed_kwargs)
    319 
    320     # Mutable properties

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/utils/generic_utils.py in 
validate_kwargs(kwargs, allowed_kwargs, error_message)
    776   for kwarg in kwargs:
    777     if kwarg not in allowed_kwargs:
--> 778       raise TypeError(error_message, kwarg)
    779 
    780 

 TypeError: ('Keyword argument not understood:', 'kernel_size')

Have you looked at the documentation for MaxPool2D? The error message seems pretty clear.

ohh yeah , got u ,just corrected to pool_size

1 Like

and there was also another error on Flatten, which i missed to put () which caused ```
‘Tensor’ object has no attribute ‘lower’