Second week residual networks exercise 2

I get a shape incompatible error, but I have looked carefully at my strides and other parameters in the components and I don’t see anything wrong. Here is the error with my print statements showing the shapes.

Original shape =  (3, 4, 4, 3)
Shape after Comp1 =  (3, 1, 1, 2)
Shape after Comp2 =  (3, 1, 1, 4)
Shape after Comp3 =  (3, 1, 1, 6)
Shape after Compshort =  (3, 1, 1, 6)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Input In [18], in <cell line: 3>()
      1 ### you cannot edit this cell
----> 3 public_tests.convolutional_block_test(convolutional_block)

File /tf/W2A1/public_tests.py:103, in convolutional_block_test(target)
     99 X = np.concatenate((X1, X2, X3), axis = 0).astype(np.float32)
    101 tf.keras.backend.set_learning_phase(False)
--> 103 A = target(X, f = 2, s = 4, filters = [2, 4, 6])
    104 assert tuple(tf.shape(A).numpy()) == (3, 1, 1, 6), "Wrong shape. Make sure you are using the stride values as expected."
    106 B = target(X, f = 2, filters = [2, 4, 6])

Input In [17], in convolutional_block(X, f, filters, s, initializer)
     52 print("Shape after Compshort = ", X.shape)
     54 ### END CODE HERE
     55 
     56 # Final step: Add shortcut value to main path (Use this order [X, X_shortcut]), and pass it through a RELU activation
---> 57 X = Add()([X, X_shortcut])
     58 X = Activation('relu')(X)
     59 print("Shape after shortcutadd = ", X.shape)

File /usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py:67, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     65 except Exception as e:  # pylint: disable=broad-except
     66   filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67   raise e.with_traceback(filtered_tb) from None
     68 finally:
     69   del filtered_tb

File /usr/local/lib/python3.8/dist-packages/keras/layers/merging/base_merge.py:73, in _Merge._compute_elemwise_op_output_shape(self, shape1, shape2)
     71   else:
     72     if i != j:
---> 73       raise ValueError(
     74           'Inputs have incompatible shapes. '
     75           f'Received shapes {shape1} and {shape2}')
     76     output_shape.append(i)
     77 return tuple(output_shape)

ValueError: Inputs have incompatible shapes. Received shapes (1, 1, 6) and (4, 4, 3)

NVM! Figured it out. It wasn’t the strides it was not using x_shortcut properly

3 Likes