Week 1 assignment 2 happymodel test error

i don’t know how to fix it, i did search but I’m stuck there

AttributeError 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()
18 model = tf.keras.Sequential([
19 ## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
—> 20 tfl.ZeroPadding2D(padding=3, data_format=(64, 64, 3)),
21 ## Conv2D with 32 7x7 filters and stride of 1
22 tfl.Conv2D(filters =32, kernel_size=(7,7), strides=1),

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/convolutional.py in init(self, padding, data_format, **kwargs)
2800 def init(self, padding=(1, 1), data_format=None, **kwargs):
2801 super(ZeroPadding2D, self).init(**kwargs)
→ 2802 self.data_format = conv_utils.normalize_data_format(data_format)
2803 if isinstance(padding, int):
2804 self.padding = ((padding, padding), (padding, padding))

/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: ‘tuple’ object has no attribute ‘lower’

It looks like you filled in the arguments to tfl.ZeroPadding2D incorrectly. We should not be using data_format, but input_shape for the purpose there. I also specified the padding as (3,3), but I think the way you did it is also correct.

Here is the documentation. It mentions that data_format is a string, for which the valid values are “channel_first” or “channel_last”. They don’t actually mention the input_shape parameter on that page, but it is inherited from one of the parent classes in the Keras Sequential and Functional API. Here’s a page that mentions how to use it.

4 Likes

there is no argument as input shape , and i saw other replies used data_format as i did,
i did put the padding as tuple(3,3) but i though something wrong and replaced it to integer
i fixed the padding error as follow:
as if m=1, none is given
input_shape = (1,64,64,3)
x = np.arange(np.prod(input_shape)).reshape(input_shape)

model = tf.keras.Sequential([
        ## ZeroPadding2D with padding 3, input shape of 64 x 64 x 3
        tfl.ZeroPadding2D(padding=3)(x)

but i got another error with dense layer

TypeError 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()
34 tfl.Flatten(),
35 ## Dense layer with 1 unit for output & ‘sigmoid’ activation
—> 36 tfl.Dense(units=1, activation=“sigmoid”)
37 # YOUR CODE STARTS HERE
38

/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]]

[[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]]

[[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=(1, 70, 70, 3), dtype=int64)

There is an argument input_shape, but it is inherited from the class Layer. This was explained in the documentation that I linked above.

Your mistake is that you are actually invoking the function defined by the ZeroPadding2D layer by giving that argument (x) at the end.

In the Sequential API, we only define the functions with their shapes and parameters, but don’t actually invoke them. We are also just making a list here, so you need a comma after every function instantiation.

1 Like

thanks, I got it, I missed that all was inside sequential function. I only concentrate on layers functions. thanks again

thanks a lot! I add “tfl.InputLayer…”, it works