Course 4 week1 Assignment 2

help me in rectifying the error

GRADED FUNCTION: convolutional_model

def convolutional_model(input_shape):
“”"
Implements the forward propagation for the model:
CONV2D → RELU → MAXPOOL → CONV2D → RELU → MAXPOOL → FLATTEN → DENSE

Note that for simplicity and grading purposes, you'll hard-code some values
such as the stride and kernel (filter) sizes. 
Normally, functions should take these values as function parameters.

Arguments:
input_img -- input dataset, of shape (input_shape)

Returns:
model -- TF Keras model (object containing the information for the entire training process) 
"""

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

error:
InvalidArgumentError Traceback (most recent call last)
/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1811 try:
→ 1812 c_op = pywrap_tf_session.TF_FinishOperation(op_desc)
1813 except errors.InvalidArgumentError as e:

InvalidArgumentError: Shape must be rank 4 but is rank 5 for ‘{{node max_pooling2d_8/MaxPool}} = MaxPoolT=DT_FLOAT, data_format=“NHWC”, ksize=[1, 4, 4, 1], padding=“SAME”, strides=[1, 4, 4, 1]’ with input shapes: [1,?,8,8,16].

During handling of the above exception, another exception occurred:

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)
44 A2=ReLU(Z2)
45 MP2=tf.keras.layers.MaxPool2D(pool_size=(4, 4), strides=4, padding=“SAME”)
—> 46 P2=MP2(Z2)
47 P2=tf.keras.layers.Flatten()(P2)
48 outputs = tfl.Dense (units= 6,activation=“softmax”)(P2)

/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)
1115 try:
1116 with ops.enable_auto_cast_variables(self._compute_dtype_object):
→ 1117 outputs = call_fn(cast_inputs, *args, **kwargs)
1118
1119 except errors.OperatorNotAllowedInGraphError as e:

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/pooling.py in call(self, inputs)
294 strides=strides,
295 padding=self.padding.upper(),
→ 296 data_format=conv_utils.convert_data_format(self.data_format, 4))
297 return outputs
298

/opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
199 “”“Call target, and fall back on dispatchers if there is a TypeError.”""
200 try:
→ 201 return target(*args, **kwargs)
202 except (TypeError, ValueError):
203 # Note: convert_to_eager_tensor currently raises a ValueError, not a

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/nn_ops.py in max_pool(value, ksize, strides, padding, data_format, name, input)
4519 padding=padding,
4520 data_format=data_format,
→ 4521 name=name)
4522
4523

/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_nn_ops.py in max_pool(input, ksize, strides, padding, data_format, name)
5267 _, _, _op, _outputs = _op_def_library._apply_op_helper(
5268 “MaxPool”, input=input, ksize=ksize, strides=strides, padding=padding,
→ 5269 data_format=data_format, name=name)
5270 _result = _outputs[:]
5271 if _execute.must_record_gradient():

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(op_type_name, name, **keywords)
742 op = g._create_op_internal(op_type_name, inputs, dtypes=None,
743 name=scope, input_types=input_types,
→ 744 attrs=attr_protos, op_def=op_def)
745
746 # outputs is returned as a separate return value so that the output

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/func_graph.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
591 return super(FuncGraph, self)._create_op_internal( # pylint: disable=protected-access
592 op_type, inputs, dtypes, input_types, name, attrs, op_def,
→ 593 compute_device)
594
595 def capture(self, tensor, name=None, shape=None):

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_op_internal(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_device)
3483 input_types=input_types,
3484 original_op=self._default_original_op,
→ 3485 op_def=op_def)
3486 self._create_op_helper(ret, compute_device=compute_device)
3487 return ret

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in init(self, node_def, g, inputs, output_types, control_inputs, input_types, original_op, op_def)
1973 op_def = self._graph._get_op_def(node_def.op)
1974 self._c_op = _create_c_op(self._graph, node_def, inputs,
→ 1975 control_input_ops, op_def)
1976 name = compat.as_str(node_def.name)
1977 # pylint: enable=protected-access

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
1813 except errors.InvalidArgumentError as e:
1814 # Convert to ValueError for backwards compatibility.
→ 1815 raise ValueError(str(e))
1816
1817 return c_op

ValueError: Shape must be rank 4 but is rank 5 for ‘{{node max_pooling2d_8/MaxPool}} = MaxPoolT=DT_FLOAT, data_format=“NHWC”, ksize=[1, 4, 4, 1], padding=“SAME”, strides=[1, 4, 4, 1]’ with input shapes: [1,?,8,8,16].

I am not sure about the error, but I would like to remind you that you have to built the structure of layers.

Since you have defined your Z1, the proper way to apply ReLU is by passing Z1 in a similar way you did with (input_img). So A1 = (…)(Z1), after P1 = (…)(A1) etc.