Week 1 Exercise 2 Functional API Signs

Hi there,

Struggled for hours to try and get this right to no avail. Been through all of the documentation. Tried numerous different changes. Sorry to put the code out there but really stuck. Seems to object to P1 line, but have no confidence in any of it. So many subtle differences in path, API calls and syntax within the same module. Also changed the last line model=…

Any help would be much appreciated.

CONV2D: 8 filters 4x4, stride of 1, padding ‘SAME’

# Z1 = tf.Conv2D(8, (4, 4), strides = (1, 1), padding = 'SAME'),
Z1 = tfl.Conv2D(8, (4,4), strides = (1,1), padding='SAME')(input_img),
## RELU
A1 = tfl.ReLU()(Z1),
## MAXPOOL: window 8x8, stride 8, padding 'SAME'
#tf.keras.layers.MaxPool2D(pool_size=(f, f), strides=(s, s), padding='same'):
#tf.keras.layers.MaxPool2D(pool_size=(f, f), strides=(s, s), padding='same'):
P1 = tf.keras.layers.MaxPool2D(pool_size=(8, 8), strides = (8, 8), padding = 'SAME')(A1),
## CONV2D: 16 filters 2x2, stride 1, padding 'SAME'
Z2 = tfl.Conv2D(16, (2, 2), strides = (1, 1), padding = 'SAME')(P1),
## RELU
A2 = tfl.ReLU()(Z2),
## MAXPOOL: window 4x4, stride 4, padding 'SAME'
#tf.keras.layers.MaxPool2D(pool_size=(f, f), strides=(s, s), padding='same'):
P2 = tfl.MaxPool2D(pool_size=(4,4), strides = (4, 4), padding = 'SAME')(A2),
## FLATTEN
F = tfl.Flatten()(P2),
## Dense layer
## 6 neurons in output layer. Hint: one of the arguments should be "activation='softmax'" 
#outputs = tfl.Dense(6, activation='softmax',),
outputs = tfl.Dense(units=6, activation='softmax')(F),
# YOUR CODE ENDS HERE
model = tf.keras.layers.Model(inputs=input_img, outputs=outputs)
return model

In the functional API, each layer is defined like a function call. So those lines of code should not end with a comma.

It would help if you also post the error messages.

1 Like

Taken the commas out, and get the same error message:

—> 66 P1 = tf.keras.layers.MaxPool2D(pool_size=(8, 8), strides = (8, 8), padding = ‘SAME’)(A1),
67 ## CONV2D: 16 filters 2x2, stride 1, padding ‘SAME’
68 Z2 = tfl.Conv2D(16, (2, 2), strides = (1, 1), padding = ‘SAME’)(P1),

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)

Full error message
ValueError 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)
64 #tf.keras.layers.MaxPool2D(pool_size=(f, f), strides=(s, s), padding=‘same’):
65 #tf.keras.layers.MaxPool2D(pool_size=(f, f), strides=(s, s), padding=‘same’):
—> 66 P1 = tf.keras.layers.MaxPool2D(pool_size=(8, 8), strides = (8, 8), padding = ‘SAME’)(A1),
67 ## CONV2D: 16 filters 2x2, stride 1, padding ‘SAME’
68 Z2 = tfl.Conv2D(16, (2, 2), strides = (1, 1), padding = ‘SAME’)(P1),

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, *args, **kwargs)
924 if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
925 return self._functional_construction_call(inputs, args, kwargs,
→ 926 input_list)
927
928 # Maintains info about the Layer.call stack.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
1090 # TODO(reedwm): We should assert input compatibility after the inputs
1091 # are casted, not before.
→ 1092 input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
1093 graph = backend.get_graph()
1094 # Use self._name_scope() to avoid auto-incrementing the name.

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
178 ‘expected ndim=’ + str(spec.ndim) + ‘, found ndim=’ +
179 str(ndim) + '. Full shape received: ’ +
→ 180 str(x.shape.as_list()))
181 if spec.max_ndim is not None:
182 ndim = x.shape.ndims

ValueError: Input 0 of layer max_pooling2d_15 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [1, None, 64, 64, 8]

The commas are still there, at the ends of the P1 and Z2 lines.

It worked Tom, thanks a millions, so near yet…