Exercise 2 - convolutional_model

Hi
I have written all the commands in accordance with each layer, but I got an error. Additionally, I have searched in discourse, but I couldn’t find the response. I would so appreciate you if you could help me.
Here Is my received error:

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)
36 # outputs = None
37 # YOUR CODE STARTS HERE
—> 38 Z1 = tfl.Conv2D(kernel_size=(4,4),filters=8,strides=1,padding=“same”,input_shape=input_img)
39 A1 = tfl.ReLU(Z1)
40 P1 = tfl.MaxPool2D(pool_size = (8,8), strides = 8, padding=“same”)(A1)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/convolutional.py in init(self, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
662 kernel_constraint=constraints.get(kernel_constraint),
663 bias_constraint=constraints.get(bias_constraint),
→ 664 **kwargs)
665
666

/opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/layers/convolutional.py in init(self, rank, filters, kernel_size, strides, padding, data_format, dilation_rate, groups, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, trainable, name, conv_op, **kwargs)
135 name=name,
136 activity_regularizer=regularizers.get(activity_regularizer),
→ 137 **kwargs)
138 self.rank = rank
139

/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/base_layer.py in init(self, trainable, name, dtype, dynamic, **kwargs)
403 else:
404 batch_size = None
→ 405 batch_input_shape = (batch_size,) + tuple(kwargs[‘input_shape’])
406 self._batch_input_shape = batch_input_shape
407

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in iter(self)
510 if shape[0] is None:
511 raise TypeError(
→ 512 “Cannot iterate over a tensor with unknown first dimension.”)
513 return _TensorIterator(self, shape[0])
514

TypeError: Cannot iterate over a tensor with unknown first dimension.

Your code for Z1 is incorrect. You need to pass “input_img” as the function parameter, not as the input_shape= argument.

And I think the calling style you used for the ReLU and flatten layers might be wrong also. Try making those use the same method you used for MaxPool2D and Conv2D

And please edit your message and remove the code. Posting your code is not allowed by the Honor Code.

1 Like

e.g. this should be A2 = tfl.ReLU()(Z2). You get the idea.

2 Likes

Thanks for your helpful soloution. It was right

Thank you. I got it.