C4W1Exercise 3 - conv_forward

may i ask how to represent i in the loop ?how to present the batch of training examples? is m correct?
and if yes, how to present ith example?

m is the number of samples. So, this loop is to pick up a sample one-by-one from 0 to (m-1) in Python programming.

Then, if you look at descriptions in this “conv-forward()”, “A_prev”'s dimension is (m, n_H_prev, n_W_prev, n_C_prev). You only need to focus on “m” to get "i"th slice. As we do not care “n_H_prev”, “n_W_prev” and “n_C_prev”, "i"th slice can be obtained like [i,:,:,:].

1 Like

Nobu has explained the i index, but note that the range you are specifying on the loop over h is also going to cause you a problem. The loops here are over the output space and we must supply a value for every point in the output space. The skipping caused by the stride happens in the input space.

sorry i am stucked here with definition of W and B, my i ask if i can have any hint here?

{moderator edit - solution code removed}

sorry i am stucked here with definition of W and B, my i ask if i can have any hint here?

{moderator edit - solution code removed}

Please read the description about the arguments carefully. Usually, lots of hints are there. :slight_smile:
Weights and Biases are passed when conv_forward() is called. Those are;

W – Weights, numpy array of shape (f, f, n_C_prev, n_C)
b – Biases, numpy array of shape (1, 1, 1, n_C)

Now, you are working on the 4th dimension (n_C), and want to pick up "c"th weights and bias.

By the way, your a_slice_prev is not correct. I do not know what “f” is, but both horizontal position and vertical position start “0”. This is not correct. You need to cut out an appropriate window by using vert_start and horiz_start.

This algorithm is using 4 loops.

  1. loop over examples (m)
  2. (for one example) - loop over vertical axis (h) with setting vertical start/end point.
  3. (for one example) - loop over horizontal axis (w) with setting horizontal start/end point
  4. (for one example) - loop over the channels to do a linear operation (a_slice_prev*weights + bias)

So, first 3 loops are setting an appropriate window to do actual calculation. Hope this helps to understand the logic flow, and what is requested.

By the way, it is better not to post any part of your implementation in here.

hi Nobu thanks,
i made some changes still have some confuse on Z . seems that’s why didn’t pass. may i ask if you can help to have a check?
{moderator edit - solution code removed}

Please do not paste any code in here. I believe that is our community rule.

All 4 lines are not correct.

  1. You do not need any slice for the last dimension. What we need is to specify a window, and apply weights and bias of "c"th channel.
  2. You need to remember what “w” is. Small “w” is an index for a horizontal loop. Once you select the right variable, then, you need to pickup the right value from the right dimension.
  3. “b” includes all biases. Remember its shape (1,1,1,n_c). You need to select one.
  4. It is not a scalar operation, but a matrix operation. You will most likely get the value in wrong dimension. If you do not find the right matrix operation, then, as an inline hint suggested, you can just call conv_single_step().

hi Nobu,
may i ask
a_slice_prev = a_prev_pad[vert_start:vert_end,horiz_start:horiz_end,all] is correct? as it still error with IndexError: only integers, slices (:), ellipsis (...), numpy.newaxis (None) and integer or boolean arrays are valid indices

No. it is not correct. What “all” means ? You can just set “:”. No need to specify any range. :slight_smile:

hi Nobu,

thanks,
but i met error here,ValueError: operands could not be broadcast together with shapes (2,3,13,4) (3,3,4)

may i ask was it due to a_slice_prev or W i pass to the function conv_single_step?

i use W[…,c] present w

At first, you should check dimensions of all required variables, w, b, a_slice_prev.
Here is a hint.
a_slice_prev.shape =(3, 3, 4)
w.shape =(3, 3, 4)
b.shape=(1, 1, 1)

thanks Nobu,
now i have operands could not be broadcast together with shapes (2,3,4) (3,3,4)

i use h and (vert_start + f) to present vert_start:vert_end why it show 2 other than 3?

Interesting shape…
At first, you need to update a start point with considering “stride”. Please look into both vertical and horizontal start. It is not just h.
Then, the essential error may be in the different code. I assume that you set the right slice to weights/biases, since it does not change the first dimension. So, it should come from a_slice_prev, right ? Which one has the dimension of (2,3,4), an incorrect one ?
You can check each step to see how dimensions are changed. First, you check A_prev_pad.shape, then, a_prev_pad.shape, then, a_slice_prev.shape…