# Wrong Shape Assertion Error with zero\_pad

**URL:** <https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756>\
**Category:** Convolutional Neural Networks\
**Tags:** coursera-platform\
**Created:** [November 7, 2021, 12:35am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756 "2021-11-07T00:35:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ameya\_Shringi](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ameya\_Shringi](https://community.deeplearning.ai/u/Ameya_Shringi)\
**Post date:** [November 7, 2021, 12:35am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/1 "2021-11-07T00:35:42Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![Aditya\_Singh](https://avatars.discourse-cdn.com/v4/letter/a/b487fb/32.png) [@Aditya\_Singh](https://community.deeplearning.ai/u/Aditya_Singh)\
**Post date:** [November 7, 2021, 4:46am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/2 "2021-11-07T04:46:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ameya\_Shringi](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ameya\_Shringi](https://community.deeplearning.ai/u/Ameya_Shringi)\
**Post date:** [November 7, 2021, 7:15am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/3 "2021-11-07T07:15:35Z")

</div>

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`

```auto
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.

```auto
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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Ameya\_Shringi](https://avatars.discourse-cdn.com/v4/letter/a/ea5d25/32.png) [@Ameya\_Shringi](https://community.deeplearning.ai/u/Ameya_Shringi)\
**Post date:** [November 7, 2021, 11:50pm UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/4 "2021-11-07T23:50:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anilget2009](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/anilget2009/32/126065_2.png) [@anilget2009](https://community.deeplearning.ai/u/anilget2009)\
**Post date:** [September 11, 2024, 2:11am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/5 "2024-09-11T02:11:28Z")

</div>

![error](https://global.discourse-cdn.com/dlai/original/3X/9/4/946e6066311c54b6701e79ad501df46dfb4bdde8.jpeg)  
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?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [September 11, 2024, 2:24am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/6 "2024-09-11T02:24:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anilget2009](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/anilget2009/32/126065_2.png) [@anilget2009](https://community.deeplearning.ai/u/anilget2009)\
**Post date:** [September 11, 2024, 2:42am UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/7 "2024-09-11T02:42:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![niravpatel](https://avatars.discourse-cdn.com/v4/letter/n/57b2e6/32.png) [@niravpatel](https://community.deeplearning.ai/u/niravpatel)\
**Post date:** [January 14, 2026, 3:28pm UTC](https://community.deeplearning.ai/t/wrong-shape-assertion-error-with-zero-pad/60756/8 "2026-01-14T15:28:45Z")

</div>

Thanks!
