C4_W2_A1 Ex 1: identity_block() ValueError: Operands could not be broadcast together with shapes (3, 3, 3) (4, 4, 3)

Hi everyone! I’m having trouble with Exercise 1 of Week 2 Assignment 2 (Course 4). I’m getting a ValueError at the Add() part. The stack trace has been provided for reference below. What am I doing wrong?

ValueError                                Traceback (most recent call last)
<ipython-input-15-eb060d3308af> in <module>
      8 A3 = identity_block(X, f=2, filters=[4, 4, 3],
      9                    initializer=lambda seed=0:constant(value=1),
---> 10                    training=False)
     11 print('\033[1mWith training=False\033[0m\n')
     12 A3np = A3.numpy()

<ipython-input-14-f903b1a7d7ea> in identity_block(X, f, filters, training, initializer)
     40 
     41     ## Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
---> 42     X = Add()([X, X_shortcut])(X)
     43     X = Activation('relu')(X)
     44     ### END CODE HERE

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    980       with ops.name_scope_v2(name_scope):
    981         if not self.built:
--> 982           self._maybe_build(inputs)
    983 
    984         with ops.enable_auto_cast_variables(self._compute_dtype_object):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2641         # operations.
   2642         with tf_utils.maybe_init_scope(self):
-> 2643           self.build(input_shapes)  # pylint:disable=not-callable
   2644       # We must set also ensure that the layer is marked as built, and the build
   2645       # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
    321     if input_shape is not None:
    322       input_shape = convert_shapes(input_shape, to_tuples=True)
--> 323     output_shape = fn(instance, input_shape)
    324     # Return shapes from `fn` as TensorShapes.
    325     if output_shape is not None:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
    110       else:
    111         shape = input_shape[i][1:]
--> 112       output_shape = self._compute_elemwise_op_output_shape(output_shape, shape)
    113     # If the inputs have different ranks, we have to reshape them
    114     # to make them broadcastable.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in _compute_elemwise_op_output_shape(self, shape1, shape2)
     83           raise ValueError(
     84               'Operands could not be broadcast '
---> 85               'together with shapes ' + str(shape1) + ' ' + str(shape2))
     86         output_shape.append(i)
     87     return tuple(output_shape)

ValueError: Operands could not be broadcast together with shapes (3, 3, 3) (4, 4, 3)

Please check your calculations inside identity_block() . Both X_shortcut and X should have shape (3,4,4,3) at the Add() step

1 Like

At line 42, you have an extra (X) that you don’t need.

1 Like

Fixed the extra (X) however I’m still getting the same error.

type or paste code herValueError                                Traceback (most recent call last)
<ipython-input-6-eb060d3308af> in <module>
      8 A3 = identity_block(X, f=2, filters=[4, 4, 3],
      9                    initializer=lambda seed=0:constant(value=1),
---> 10                    training=False)
     11 print('\033[1mWith training=False\033[0m\n')
     12 A3np = A3.numpy()

<ipython-input-5-a6559bcb589c> in identity_block(X, f, filters, training, initializer)
     40 
     41     ## Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
---> 42     X = Add()([X_shortcut, X])
     43     X = Activation('relu')(X)
     44     ### END CODE HERE

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    980       with ops.name_scope_v2(name_scope):
    981         if not self.built:
--> 982           self._maybe_build(inputs)
    983 
    984         with ops.enable_auto_cast_variables(self._compute_dtype_object):

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
   2641         # operations.
   2642         with tf_utils.maybe_init_scope(self):
-> 2643           self.build(input_shapes)  # pylint:disable=not-callable
   2644       # We must set also ensure that the layer is marked as built, and the build
   2645       # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
    321     if input_shape is not None:
    322       input_shape = convert_shapes(input_shape, to_tuples=True)
--> 323     output_shape = fn(instance, input_shape)
    324     # Return shapes from `fn` as TensorShapes.
    325     if output_shape is not None:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
    110       else:
    111         shape = input_shape[i][1:]
--> 112       output_shape = self._compute_elemwise_op_output_shape(output_shape, shape)
    113     # If the inputs have different ranks, we have to reshape them
    114     # to make them broadcastable.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in _compute_elemwise_op_output_shape(self, shape1, shape2)
     83           raise ValueError(
     84               'Operands could not be broadcast '
---> 85               'together with shapes ' + str(shape1) + ' ' + str(shape2))
     86         output_shape.append(i)
     87     return tuple(output_shape)

ValueError: Operands could not be broadcast together with shapes (4, 4, 3) (3, 3, 3)

I used the template code. I don’t get what I’m doing wrong

Do these hints help?

  • When you enter the function identity_block, X.shape = (3,4,4,3)
  • At the end of the 1st component i.e. after Activation, X.shape = (3,4,4,4)
  • At the end of the 2nd component i.e. after Activation, X.shape = (3,4,4,4)
  • At the end of the 3rd component i.e. after BatchNormalization, X.shape = (3,4,4,3)
2 Likes

Yep, these hints were quite helpful. I figured it out, I forgot to add padding to the 2nd CONV Layer. Thanks for the guidance!