Course 4 Programming assignment 1

getting AssertionError: Wrong shape: (8, 7, 7, 6) != (4, 9, 9, 2)
I am using

X_pad = np.pad(X, pad_width = 2, mode=‘constant’, constant_values=(0,0))

Any corrections?

Welcome to the community.

The problem is, if “int” is set to pad_width, then, it means “before” and “after” padding will be done for all axes. I’m not sure where “2” comes from, but 'pad_width" is wrong.

The padding is applied to the height and width of an image, as illustrated in Figure 1.

As described, we need to add padding only for the height and width. Please see an example, and create an appropriate sequence for “pad_width”.

I have tried many combinations but could not resolve. Can anyone give the solution code please?

Can anyone give the solution code please?

No one can provide. That’s a violation of the community rule, and also Coursera Honor Code.

Programming is not a trial and error. It’s logical. Here is the way to think about.

  1. Input shape of X for zero_pad() in this test is (4, 3, 3, 2). And, if you read a comment for the code, you see that it is (m, n_H, n_W, n_C). As we are requested to “pad” for height and width, we do care only n_H and n_W.
  2. Expected output for this test is (4, 9, 9, 2) that you got from the assertion error. (Of course, input and output should vary. Just talking about one example, i.e, this test.)

From the above, you can see that you need 4 combinations of (before, after) padding. And, it is obvious that the first and last combination should be (0,0) since no addition is required.

The number of padding is given as “pad” for zero_pad(). This padding number is used for the 2nd and 3rd combination of (before, after). And, we add same number for both “before” and “after”.

The above is almost the code. Hope you can go with the above.

Following your guidance, I have written following

code removed

but still the error is AssertionError: Wrong shape: (4, 7, 7, 2) != (5, 8, 8, 3)

You need to look at each variable very carefully. What you have in zero_pad() is a local variable, “X”, capital X, not “x” which is a global variable.

By the way, please remove the above code. We should always start with a Traceback in any cases.

Finally, it ran. Thanks a lot. :slightly_smiling_face: