Wrong Shape Assertion Error with zero_pad

Hi I am trying to finish the zero_pad question in the convolution network step by step programming assignment. I am running into the following error

AssertionError                            Traceback (most recent call last)
<ipython-input-27-a6f364884ebb> in <module>
     12 axarr[1].set_title('x_pad')
     13 axarr[1].imshow(x_pad[0, :, :, 0])
---> 14 zero_pad_test(zero_pad)

~/work/release/W1A1/public_tests.py in zero_pad_test(target)
     27 
     28     assert type(x_pad) == np.ndarray, "Output must be a np array"
---> 29     assert x_pad.shape == (5, 4 + 2 * pad, 4 + 2 * pad, 3), f"Wrong shape: {x_pad.shape} != {(5, 4 + 2 * pad, 4 + 2 * pad, 3)}"
     30     assert np.allclose(x_pad[0, 0:2,:, 0], [[0, 0, 0, 0, 0, 0, 0, 0], 
     31                                             [0, 0, 0, 0, 0, 0, 0, 0]], 1e-15), "Rows are not padded with zeros"

AssertionError: Wrong shape: (4, 7, 7, 2) != (5, 8, 8, 3)

Looking at the error message, I am not sure why the first 1 and the last dimension are different between the expected value and the output. This isn’t something that zero_pad is doing. Could you please help me resolve this issue?

Hi @Ameya_Shringi, This error basically means that the shape of your output mismatches with the shape that the grader expects. Please check if you have correctly passed the values for the pad_width parameter in the np.pad() function, and also check whether it matches with the dimensions of the image(height, width) to which you are applying the padding to. Moreover, only apply padding to the height and width of the image and not the batch size and the number of channels, in the array X.

1 Like

Thanks for the quick reply, @Aditya_Singh. I had checked the width and height padding that were being passed. To provide more context, here is the output that I am getting on the random matrix that is being generated with a padding of 3

x.shape =
 (4, 3, 3, 2)
x_pad.shape =
 (4, 9, 9, 2)

This matches with the dimension specified in the docstring. Additionally if you see, the assertion message, the expected output has larger batch and channel dimension compared to what I am outputting.

assert x_pad.shape == (5, 4 + 2 * pad, 4 + 2 * pad, 3), f"Wrong shape: {x_pad.shape} != {(5, 4 + 2 * pad, 4 + 2 * pad, 3)}"

So I am not padding along those dimensions. Finally, I had added the following line in the function

print(f"Shape of x: {x.shape}, pad: {pad}")

to see the array that was being passed to the function in that test and I got Shape of x: (4, 3, 3, 2), pad: 2. Thus the output of x_pad seems to be correct. Unless I am missing something here.

I was able to resolve the issue. It was a bug in my code where I was using x rather than X to create the padded array and that was causing the discrepancy between the outputs.


I am unable to figure out what I am doing wrong here…
I can see additional 6 is added to 4 and 2, but how can I tell me code not to add it to them?
I have used np.pad(X, pad) anything wrong with my code?

It looks like you probably padded all 4 dimensions. That’s not what we are intended to do: we only want to pad the h and w dimensions (the height and width), not the first dimension (which is “samples”) or the last dimension (which is “channels”).

The np.pad API lets you specify how much padding you want on each individual dimension. That can be 0 for some dimensions. They gave you an example of the API in the instructions. If you missed the point of that, it would be worth giving it another careful look.

Thank you very much @paulinpaloalto
The mistake I was doing was overthinking and trying to do something by myself.
The example mentioned is very clear and apt.
Thank you once again!

1 Like