Zero_pad function( course 4, Week 1, exersice 1)

I have a problem with defining the zero_pad function to be precise the X_pad value using the np.pad() function.
To define the pad_width I suppose to add 2 * pad_value in both directions horizontally and vertically, but when I saw the expected value in the next test cell it shows that the resulted image has zero paddings only along with its height, so the size for each image in the vector will be changed from (3,2) to (9,2) which is something I don’t understand, I expect to have (9,8) as a new size, assume the pad value to be 3.
Thanks for any help!

The zero pad function only adds padding to the 2nd and 3rd dimensions of the X 4D matrix. So each image becomes 9x9x2. That is what the test case shows.

1 Like

Thanks for the reply, but the problem still not solved, I keep having the dimension of (4, 9, 9, 8) and I don’t know why.
x_pad = np.pad(current_image, (pad,pad), mode=‘constant’, constant_values = (0,0) )
this is what I used inside for loop and then concatenate padded images in one array.

You have only added padding for one dimension. The instructions tell you to add padding for the 2nd and 3rd dimensions. So you have to skip the 1st and 4th dimensions and use (pad,pad) for both the 2nd and 3rd.

Each padding argument requires a tuple with the beginning and ending padding. That’s what “(pad,pad)” does.

To skip the padding for a dimension, you can use (0,0) as the beginning and ending.


Thanks very much, the problem has been solved!

Why is the input a 4D matrix?

The videos all indicated that this would be a 3D matrix as well as a the prompt in the function. Is there an implementation or theoretical reason for this?