Programming Assignment: Convolutional Neural Networks: Application

The following error which I can’t able to get rid of while developing the convolution model. Can someone help me out with this?

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 = tfl.Conv2D(filters=8,kernel_size=(4,4),strides=(1,1), padding='same')(input_img),
## RELU
A1 = tfl.ReLU()(Z1),
## MAXPOOL: window 8x8, stride 8, padding 'SAME'
P1 = tfl.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'
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')(F)
                
# YOUR CODE STARTS HERE

# YOUR CODE ENDS HERE
model = tf.keras.Model(inputs=input_img, outputs=outputs)
return model

Error:
TypeError 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)
18 input_img = tf.keras.Input(shape=input_shape),
19 ## CONV2D: 8 filters 4x4, stride of 1, padding ‘SAME’
—> 20 Z1 = tfl.Conv2D(filters=8,kernel_size=(4,4),strides=(1,1), padding=‘same’)(input_img),
21 ## RELU
22 A1 = tfl.ReLU()(Z1),

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

TypeError: Dimension value must be integer or None or have an index method, got value ‘TensorShape([None, 64, 64, 3])’ with type ‘<class ‘tensorflow.python.framework.tensor_shape.TensorShape’>’

Hi PRADEEP417,

Maybe this explanation helps.

2 Likes

Resolved problem. Thanks

1 Like

How did you solve the problem? U have the same problem
“Dimension value must be integer or None or have an index method, got value ‘TensorShape([None, 64])’ with type ‘<class ‘tensorflow.python.framework.tensor_shape.TensorShape’>’”

1 Like

@Roya: In the convolutional_model, we are using the Functional API instead of the Sequential API. The difference is that we are actually instantiating and calling the functions here, with both parameters and explicit inputs and outputs. In the Sequential API, we are only instantiating them as a python list.

That’s a fancy way to say that the mistake in the original post on this thread is putting commas between the lines. Since each line is actually an assignment statement, that has the effect of changing the type of the RHS of the assignment statement to be a python “tuple”. That is a mistake and is what is causing the confusing sounding error message. If you read it again with what I said in mind, it’ll probably make more sense.

1 Like

Oh, I saw it.
Thank you so much! :slight_smile:

1 Like