Convolutional model, step by step


need some help in slicing inputs to a_prev_pad array? how should we slice this array along with weights & bias array.?

we are iterating in nested loop for each (Height( Width ( channels) ) ) sections

input size = 5x7x4
A_prev_pad = 7x9x4
output size = 3x4x8

please help.
Is padding output correct?

Hey @s_baghel_in,
Welcome to the community. I don’t exactly follow your question. As far as I can understand, I guess you are facing an issue regarding the indexing of a_prev_pad to obtain a_slice_prev in the conv_forward function.

If so, then the simplest way to understand is what each of the dimensions represent. The first dimension represents the height of the input (n_H_prev), the second dimension represents the weight of the input (n_W_prev) and the third dimension represents the channels in the input (n_C_prev). Once you have a clear understanding of these, you can simply obtain the apt indices for these 3 dimensions, and use those to slice a_prev_pad.

if you were looking to ask something else, then do let us know. We would be happy to help.


Hi Elemento.,
thats the exact problem statement. How should i slice using array[…;…;] syntax. we have input format of 7x9x4 . we have output format 3x4x8. nomenclature is height x weight x channels. Either my padding is wrong?


Hey @s_baghel_in,
I guess you are having an issue in understanding how the convolution works. I would suggest reviewing the lectures once more, since, I don’t see any issue in this. I am mentioning here a small explanation which might help you.

We will consider the same example that you are talking about, so:

Dimensions of A_prev = (2, 5, 7, 4)
Dimensions of A_prev_pad = (2, 7, 9, 4)
Dimensions of a_prev_pad = (7, 9, 4)
Dimensions of W = (3, 3, 4, 8) or (f, f, n_C_prev, n_C)
Dimensions of output = (3, 4, 8)

Using the generic formula for the output dimensions, which is:
Output \;size = floor((n + 2p - f) / s) + 1, where \; n = input \; size, p = padding, f = filter \; size, s = stride

We can verify the output format. Consider the height first. We have n = 5, p = 1, f = 3, s = 2 -> Output = 3. Then, consider the width. We have n = 7, p = 1, f = 3, s = 2 -> Output = 4. Here, remember that a_prev_pad already includes padding, so, if we consider height n + 2p = 7 and if we consider width n + 2p = 9, as I have stated above.

So, the height of the output is 3, and the width of the output is 4. Also, since, we have 8 kernels, the channels in the output will be 8. Therefore, the dimensions of the output will be (3, 4, 8). Here, I have assumed that when you are referring to output, you are referring to the output corresponding to a single example. If we have multiple examples, as in this case 2, the dimensions of the output will be (2, 3, 4, 8).

I hope this helps you.


HI Elemento,
thanks for the dimensions calculations,
i am stuck with slicing of a_prev_pad & weights array to fill output matrix.
Lets take an example.

{Moderator’s Edit: Solution Code Removed}

Please help on slicing part .
I have gone through the lectures again.


Sure @s_baghel_in,
Let me explain you the answer in DM, since it is against the community guidelines, to post the solutions publicly.