# Week 1 Assignment 1 problem 3

Hello, I seem to have some trouble with the for loop for this function. When I’m composing z[i,h,w,c] at the end I’m using the formula a_slice_prev*weights+float(b), but my dimension for a_slice_prev and weights are not equal.

I think I’m partly having trouble understanding which parts are playing which functions in this process, and was hoping for some clarification.

We have a 4D array A_prev, and we pad the height and width features (the 1st and 2nd dimensions) to get A_prev pad. Then we take a single index of that 4D array to get a 3D array a_prev_pad. The shape I’m getting now is (7,9,4). I’m assuming ‘f’ is the length/width of the filter “square” that we move across the slice when doing the convolution, so in the forloops my vert_start = h and vert_end = h+f. However, after slicing the shape don’t line up.

For a_prev_slice I’m getting (3, 3, 4) and for weights (W[c]) I’m getting (3, 4, 8). Am I slicing it wrong, or defining my vert/horiz_start/end wrong?

It would help if you gave the name of the function. “problem 3” isn’t very specific.

I’m going to assume you’re talking about conv_forward().

You should call conv_single_step() to compute the Z values.

I apologize, I am talking about conv_forward(), and I am calling conv_single_step to compute the Z values at the end.

I’m slicing W and b the following way:
[vert_start:vert_end,horiz_start:horiz_end,:,0]
to get weights and biases, but my shapes still don’t align after c>=7

No, that’s kind of how you slice a_prev_pad to get a_slice_prev.
For W and b, you slice using only ‘c’ as the last dimension.

I’ve tried W[c] and b[c] but get the error:
"operands could not be broadcast together with shapes (3,3,4) (3,4,8) "
I’m assuming something is wrong with my a_slice_prev slicing then?

using W[c] and b[c] I’m getting the following shapes:

A_prev: (2, 5, 7, 4)
W: (3, 3, 4, 8)
b: (1, 1, 1, 8)
a_slice_prev: (3, 3, 4)
weights: (3, 4, 8)
biases: (1, 1, 8)

since the shape of W[c] is (3,4,8) I imagine that I need to slice a_prev_pad to get a_prev_slice to be the same shape, but I don’t see how that would be even possible, especially because I thought the filter is supposed to be of shape (n,n,m) so that it’s basically a square with m-depth?

I’m really sorry for continuing to bother you on this, but the shapes for these arrays are honestly confusing me and don’t seem to be lining up.

The way I’m seeing this is we start with A_prev (shape = (2,5,7,4)) who has ‘m = 2’ many ‘5x7’ 2D arrays of depth 4.

We’re going to be padding each 5x7 2D array with a 0s to become 7x9.

W (shape = (3,3,4,8)) has 3 3x4 2D arrays with depth 8.

From how I thought it worked, we take a ‘2D’ array (with depth) from A_prev_pad, and a ‘2D’ array (with depth) from W, and convolute the one from W with a subarray from A_prev_pad (matching the shape from the weight/filter array).

However, there’s no way to do this when the depths aren’t anywhere close to being equal. Especially because the weights (which we’re not slicing so they should stay the same) have a depth of 8. There’s no way to slice the a_prev_slice array (with a depth of 4) to allow this to happen. What am I misunderstanding here?

Thats not slicing. That’s just indexing.
W and b have four dimensions. ‘c’ is the last one. For the first three, you substitute ‘:’ markers.

Thank you for the help!