Week 1 assignment 1 : slicing qn

in the assignment for the part on “convo_forward” in the nested n_c loops for the channel slices,
the variable a_slice_prev is equal to a_prev_pad slices from the start to end of horizontal and vertical but what is the last slicing colon in the a_slice_prev is used for? is it for all the channels in the a_prev_pad but i am left to wonder why is the training for batch omitted?

to give some context on why i asked, it is becuz for the W and b params the colons are [:,:,:,c] which shows that there are 4 inputs to consider for the array to be fed into the single_convo function.

The dimensions of W are f x f x nC_{in} x nC_{out}. The shape of the input has nC_{in} channels, so the filter matches all of them at each position of the output. Of course all the loops here are over the output space and we must touch each position. The striding (not what you’re asking about I realize) happens in the input space and all operations against the input space include all the input channels.

i see so when it comes to the final part of the function of trying to get the Z from the single convo, the slice of a_slice_prev = a_prev_pad[vert_start:vert_end,horiz_start:horiz_end,:] is still (m, n_H_prev, n_W_prev, n_C_prev)?

if that is the case can i just ask for the below shapes to check my understanding at each line.

for A_prev_pad = zero_pad(A_prev,pad),A_prev_pad is of shape (m, n_H_prev, n_W_prev, n_C_prev)

for a_prev_pad = A_prev_pad[i], a_prev_pad is of shape (n_H_prev, n_W_prev, n_C_prev)

for a_slice_prev = a_prev_pad[vert_start:vert_end,horiz_start:horiz_end,:], a_slice_prev is of shape (i (i being from the first for loop), n_H_prev, n_W_prev, n_C_prev)

which is why at the last line at the single convo step part for 1 slice of z the a_slice_prev can be multiplied with the W?

Remember how the loops work: it is a 4 level nested loop. The outer loop is over the first dimension, which is the samples dimension. So a_slice_prev has no m dimension. Print the shape of it in your code and you’ll see it is a 3D tensor. At least it should be.

The shape of A_prev_pad will include the padding, right? So what you show is only correct in the case that pad = 0.

vert_start to vert_end should be f, right?

yep supposed to be f, mb. tks for the explanation i understand the slicing a little better theory-wise. so can i check if a say a_slice_prev is of size (f,f,c) when it multiplies with the weights of size (f,f,c, no of filter), i will get an output of (1,1, no of filters),(assuming if the c in a_slice_prev and weights are the same), would that be correct paul?

If I understand what you are saying, that sounds correct. But the point is that for each value of c for the output filter number (which is what the loop is doing), you get a scalar output value from conv_single_step, right? You are slicing the W value along the output filter dimension, so that the resultant shape is f x f x nC_{in} and you have also sliced the A value have that same shape and you slice the b value so that it is 1 x 1 x 1 and pass those to conv_single_step.

1 Like

Roger that, tks for the explanation paul