Exercise 2 Functional API.
Error message.
40 Z1=tf.keras.layers.Conv2D(filters=8 , kernel_size= (4,4), strides=(1, 1), padding=‘same’)(input_img)
—> 41 A1=tf.keras.layers.ReLU(Z1)
42 P1=tf.keras.layers.MaxPool2D(pool_size=(4, 4), strides=(4, 4), padding=‘same’)(A1)
43 Z2=tf.keras.layers.Conv2D(filters=16 , kernel_size= (2,2), strides=(1, 1), padding=‘same’)(P1)
The issue should be line 41.
Any ideas on how to make it right?
Thanks
@runsong Think what does ReLU function accept as parameters and what does it return? As a hint, look at the syntax of MaxPool2D layer code on next line.
1 Like
Thanks for the directions. I changed the code as below.
Z1=tf.keras.layers.Conv2D(filters=8 , kernel_size= (4,4), strides=(1, 1), padding='same',activation='relu')(input_img)
P1=tf.keras.layers.MaxPool2D(pool_size=(4, 4), strides=(4, 4), padding='same')(Z1)
Z2=tf.keras.layers.Conv2D(filters=16 , kernel_size= (2,2), strides=(1, 1), padding='same',activation='relu')(P1)
Test failed error raised:
Test failed
Expected value
['Conv2D', (None, 64, 64, 8), 392, 'same', 'linear', 'GlorotUniform']
does not match the input value:
['Conv2D', (None, 64, 64, 8), 392, 'same', 'relu', 'GlorotUniform']
I think I was a bit too indirect in my answer.
The syntax of your edited code (combining conv2d and relu into one layer by adding activation parameter in conv2d) is valid. However, the test failed because the test is looking at the layers in the model and that test expects conv2d and rely to be 2 separate layers.
So going back to your original code, the error was raised for line 41 which was:
A1=tf.keras.layers.ReLU(Z1)
This line of code is passing Z1 to ReLU function. What you need to do instead is to pass Z1 to the object returned by this ReLU function, just like you do in the code lines for other layers.
This modification should correct the syntax as well as satisfy the test criteria.
1 Like
Ah I figured it out.
A1=tf.keras.layers.ReLU()(Z1)
Model: "functional_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 64, 64, 3)] 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 64, 64, 8) 392
_________________________________________________________________
re_lu_3 (ReLU) (None, 64, 64, 8) 0
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 8, 8, 8) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 8, 8, 16) 528
_________________________________________________________________
re_lu_4 (ReLU) (None, 8, 8, 16) 0
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 2, 2, 16) 0
_________________________________________________________________
flatten_2 (Flatten) (None, 64) 0
_________________________________________________________________
dense_2 (Dense) (None, 6) 390
=================================================================
Total params: 1,310
Trainable params: 1,310
Non-trainable params: 0
_________________________________________________________________
All tests passed!
1 Like