C4 Week 1 Assignment 1: A_prev dimension

conv_forward and pool_forward both have a three layer nested loop which loops through a_prev_pad and a_prev, respectively.

I don’t fully understand why a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end,:] for con_forward only needs 3 dimensions (it’s a 4 dimension array) while pool_forward 's a_prev_slice = A_prev[i,vert_start:vert_end, horiz_start:horiz_end,c] requires all 4.

I know it has something to do with the fact that a_slide_prev for conv_forward is being multiplied by W vector but I still don’t fully understand why

I believe it’s because the key difference is that pooling is applied independently to each channel (hence the 4D loop in pool_forward), while convolution combines information across all channels in one operation (hence the 3D slice in conv_forward with a separate loop to handle different filters). However, in both scenarios you still have m amount of inputs which you need to apply the pooling filter or the convolution to.

1 Like

What exactly is your question?

I am seeing 4 loops in both conv_forward and pool_forward. Both these functions loop over all 4 dimensions of the input A_prev, pulling out slices with the correct dimensions as necessary.

It’s important to note that a_prev_pad in conv_forward is a 3 dimensional array, not a 4 dimensional array. You can do print(a_prev_pad.shape) to verify. You might be missing the line that extracts the a_prev_pad from A_prev_pad.

It is true that convolution combines information across all channels in one operation, and so we need to supply all the channels at once, whereas pooling operates on a per-channel basis.

1 Like

Yes, the point is that there are four levels of loops and the outer level loop is over the individual samples. That’s what you’re missing. By the time you get to the line you show, you’re down to 3 dimensions because you’re looking at only one sample input.