Probleme with zero_pad initialization for the first exercice of cov


I’m stuck with the zero pad initialization. I get the following error

AssertionError: Wrong shape: (4, 15, 15, 2) != (4, 9, 9, 2)

I did the following:

np.pad(X, ((0, 0 ),(pad_h, pad_h), (pad_w, pad_w), (0, 0)) ,mode='constant', constant_values = (0,0))

where pad_h and pad_w are 2*pad according to the description of the exercice.

I try to figure out what is the problem but so far don’t see it.
Can anyone give me an insight please ?

It’s very close.
The guide talks about the final image, which is the outcome of padding to both “before” and “after”. Then, it is equal to add “2*pad”.
If you add “2*pad” to both “before” and “after”, then, in total, you are adding 4*pad. That’s the reason for this error.

1 Like

Oh okey got it. It works now but I’m not sure if I understood well.
When we talk about before and after, it refer to what exactly ?
I thought it was at the beginning we want to go from shape “before” to shape “after”.

thank you !

if you visit numpy.pad document, you will see the description like this.

numpy.pad(array, pad_width, mode=‘constant’, **kwargs )

pad_width : {sequence, array_like, int}

Number of values padded to the edges of each axis. ((before_1, after_1), … (before_N, after_N)) unique pad widths for each axis. ((before, after),) yields same before and after pad for each axis. (pad,) or int is a shortcut for before = after = pad width for all axes.

Hope this clarifies.

Yes I already checked the documentation.
I think I will try to play with it to understand better.

Here is the image.

This padding is commonly used for a convolutional network. So, it is better to fully understand how it works. :slight_smile:

1 Like

Great ! Got it better with this image ! Thank you !