DSL 4 week 1 assignment 2 Conv2D

Hi I am getting the following error, I don’t understand the dimensions

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 = tf.keras.layers.Conv2D(filters= 8 , kernel_size= (4,4) ,strides = (1,1), padding=‘same’, input_shape=input_img)
39
40 A1 = tf.keras.layers.ReLU(Z1)

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

1 Like

Your call to Conv2D is incorrect.
Don’t use “input_shape=input_img”
Instead end the Conv2D call after the padding argument, then use (input_img) as a separate parameter.
Z1 = tf.keras.layers.Conv2D(filters=8, kernel_size= (4,4), padding=‘same’) (input_img)
That’s now all of the layers work - you define the layer, and then call it with the data argument.

3 Likes

When I use ReLU on Z1 with shape = (None, 64, 64, 8), it gives me A1 with shape (1, None, 64, 64, 8) which causes my input to MaxPool having 5 dimensions instead of 4. Why is this happening?

Edit: I found the answer to this in another thread. It was because I was adding commas. Not sure what TF does with the commas to mess it up though.

I think commas are only used for creating lists. They’re used in the sequential model, but aren’t used in the functional model.

I’m not sure which one this assignment uses.