Course 4 (CNN): Week 1 - Assignment 1 - Exercise 3

Regarding the week 1 / assignment 1 / exercise 3, I’ve been stuck. Here’s what I have:

a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
weights = W
biases = b
Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

It seems like what passed into weights (= W) and biases (= b) are not correct in order to call conv_single_step(). But, I thought the filter W and biases b are constant throughout, for this problem, right ? The only thing that changes for each iteration is the a_slice_prev, am I right ?

ValueError: operands could not be broadcast together with shapes (2,3,4) (3,3,4,8)

The way you are creating “weights” and “biases”, you’re just creating copies of the variables. That’s not what you need.

It addition to the problem that Tom has pointed out, it looks like your vert_start and vert_end calculations are also incorrect. Why does the h dimension of your a_slice_prev end up with size 2? So it looks like you’ve got at least two layers of problems here: not “slicing” the weights and biases correctly and your height dimension handling is also incorrect.

Hi Paul, I’m not sure how you saw my h dimension in

a_prev_pad [vert_start : vert_end, horiz_start : horiz_end, : ]

The real question I’m having is whether the filter W and biases b are the same for every iteration.

I saw it in the error message. Notice the shape of the first operand is 2 x 3 x 4, right? It should be 3 x 3 x 4.

That code is wrong for vert_start and horiz_start. Where does the stride come into the calculation?

The point is that the innermost loop is over the output channels, right? The filter and bias values are different for each output channel.

Here’s the whole thing for exercise 3:

{moderator edit - solution code removed}

OK, I will look at what’s wrong with vert_start, horiz_start, W and b. I’m going to bed now. I appreciate your review. Thanks!

Yes, there are a number of problems there. Note that the “samples” dimension is the first dimension, right? So you are “slicing” A_prev_pad incorrectly. That’s probably the cause of the wrong h dimension in your previous error message, not the stride error.

Then you omit the stride in calculating the vert_start and horiz_start. The way to think about this is that we are looping over the output space and touching every sample, every h x w position and every channel of the output space. Then for each output position, you have to calculate where that comes from in the input space.

Then you need to understand how the filters and bias values work w.r.t. the channels.

With the above hints, it’s your job to figure out how to get it to work.

Thanks Paul!

I’ve been distracted with some of the things: Asian New Year, tax filing,… I’ve tried to put my mind together for this exercise. I have re-watched some of the video lectures and they helped. Here’s an update version of my work on this exercise, please look through and let me know if I’m close. I’ll appreciate if you point out to me what’s still wrong so I can take a closer look. Thanks!

{moderator edit - solution code removed}

Here’s the output:
shape of weights = (3, 3, 4)
shape of a_slice_prev = (3, 3, 4)

IndexError: too many indices for array

The above error points to this line: Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

This is what “debugging” is about. The first thing you have to do is figure out what the error message is telling you. In this case, it is telling you that Z does not have 4 dimensions. So how many dimensions does it have? Put this print statement right before the line that throws the error:

print(Z.shape)

What do see? Now you have to figure out how it got that way. Where is the shape of Z determined?