Week1: Conv Model application- ZeroPadding2D error

Hi Everyone,
I am getting this error while compiling the code for Week 1 Programming Assignment 2 -

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-133-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-132-93e688c7667d> in happyModel()
     32 
     33             # YOUR CODE STARTS HERE
---> 34             tfl.add([tfl.ZeroPadding2D(padding=3)(input_shape=tf.keras.Input(shape= (64,64,3))),
     35                        tfl.Conv2D(filters=32, Kernel_size=(7,7), strides=1),
     36                        tfl.BatchNormalization(axis=3),

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    914     #   not to any other argument.
    915     # - setting the SavedModel saving spec.
--> 916     inputs, args, kwargs = self._split_out_first_arg(args, kwargs)
    917     input_list = nest.flatten(inputs)
    918 

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _split_out_first_arg(self, args, kwargs)
   2978     else:
   2979       raise ValueError(
-> 2980           'The first argument to `Layer.call` must always be passed.')
   2981     return inputs, args, kwargs
   2982 

ValueError: The first argument to `Layer.call` must always be passed.

Could you please guide me where i am going wrong with this?

Thank you.

When you’re using the .add() method, you don’t need the square brackets.

I tried to remove that, but still gives me the same error. Not sure how to add layers in the sequential model API within square brackets. :frowning_face:

Do i need to use the add() function or is this something that will be automatically taken care of in the sequential model function?

I removed the .add() function and now i get this error -

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-224-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-223-c66c2a94f9a1> in happyModel()
     41                        tfl.MaxPool2D(pool_size=(2, 2)),
     42                        tfl.Flatten(),
---> 43                        tfl.Dense(1, activation='sigmoid')
     44             # YOUR CODE ENDS HERE
     45         ])

/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/sequential.py in __init__(self, layers, name)
    140         layers = [layers]
    141       for layer in layers:
--> 142         self.add(layer)
    143 
    144   @property

/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/sequential.py in add(self, layer)
    180       raise TypeError('The added layer must be '
    181                       'an instance of class Layer. '
--> 182                       'Found: ' + str(layer))
    183 
    184     tf_utils.assert_no_legacy_layers([layer])

**TypeError: The added layer must be an instance of class Layer. Found: tf.Tensor(**
[[[[0 0 0]
   [0 0 0]
   [0 0 0]
   ...
   [0 0 0]
   [0 0 0]
   [0 0 0]]

  [[0 0 0]
   [0 0 0]
   [0 0 0]
   ...
   [0 0 0]
   [0 0 0]
   [0 0 0]]]], shape=(600, 70, 70, 3), dtype=int64)

There are two ways to implement the Sequential model.

  • One is by creating a Sequential layer, and passing it a list (within square brackets) of the layers you want to use, separated by commas.

  • The other is by creating a Sequential layer, and then using the .add method to add the other layers. This method does not use a list, so you don’t need square brackets or commas.

Given the error you are seeing, I suspect there’s a problem (a syntax error) in how you’re defining the list of layers.

I am doing it by the 1st method you suggested as in the assignment, square brackets are already placed in within the sequential model function →

model = tf.keras.Sequential([ .. ])

I am passing in the list of layers comma separated.

would it be okay if I can message you my code for evaluation? Not sure, where the code is wrong

Thank you. (mentor edited the reply)

For someone who might be facing the same problem as me :
Tip - Pass InputLayer as your first parameter to the sequential keras model and everything else will fall into place. :slight_smile:


UnboundLocalError Traceback (most recent call last)
in
----> 1 happy_model = happyModel()
2 # Print a summary for each layer
3 for layer in summary(happy_model):
4 print(layer)
5

in happyModel()
21
22 ## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
—> 23 model.add(tfl.ZeroPadding2D(padding=(3,3))),
24
25 ## Conv2D with 32 7x7 filters and stride of 1

UnboundLocalError: local variable ‘model’ referenced before assignment

Hi, I believe you are using sequential keras model for assignment -

In that case, .add() function is not necessary. You can directly pass the parameters comma separated.
Hope this helps. :slight_smile: