Week 1 exercise 3 conv_single step

ValueError Traceback (most recent call last)
in
6 “stride”: 2}
7
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
9 z_mean = np.mean(Z)
10 z_0_2_1 = Z[0, 2, 1]

in conv_forward(A_prev, W, b, hparameters)
80 weights = W[h,w,c]
81 biases = b[0,0,0,c]
—> 82 p = np.sum(a_slice_prev * weights)
83 Z[i, h, w, c] = p+biases
84

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

I have understood the values in a_slice_prev are not equal in weights values but the size of the input is not in control of conv_single_step function , if any one can help in person that would be mostly helpful as i cannot paste the solution in discord channel

Your issue is in conf_forward(), not conv_single_step().

Your slicing of ‘W’ and ‘b’ is incorrect:

  • ‘W’ has four dimensions, you’ve only used three.

  • For both ‘W’ and ‘b’, only select ‘c’ as the last dimension. The first three should be ‘:’, so you use all of the values of that index.

You may also have a problem in conv_single_step(). The error trace you posted is rather confusing.

conf_forward() should compute a_slice_prev, weights, and biases, and pass them to conv_single_step().

conv_single_step() should use np.multiply() and np.sum() and np.float().

thanks for solution, but my code in conv_single_step() worked fine!!

i have tried as per mentioned solution but it did not worked, i think i am missing something , can you please take a look at my code

{moderator edit - solution code removed}

i have received same error :

ValueError Traceback (most recent call last)
in
6 “stride”: 2}
7
----> 8 Z, cache_conv = conv_forward(A_prev, W, b, hparameters)
9 z_mean = np.mean(Z)
10 z_0_2_1 = Z[0, 2, 1]

in conv_forward(A_prev, W, b, hparameters)
80 weights = W[:,:,:,c]
81 biases = b[:,:,:,c]
—> 82 p = np.sum(a_slice_prev * weights)
83 Z[i, h, w, c] = p+float(biases)
84

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

There are several problems there:

  1. It’s not necessarily a bug, but why are you manually writing out the logic for padding the A value? You already wrote a function to do that, right?

  2. Your vert_start and vert_end logic is incorrect. You did not take the stride into account, but there’s also a misunderstanding about how ranges and indexing work in python: everything is 0-based, right? So the end index on a range should not be decremented: python does that for you already. The same problem exists for the horizontal ranges as well.

  3. As a general matter, it’s not supposed to be our job to write the code for you. Please don’t post your code and say “please fix it for me”. If we need to see your code in order to help, we will ask privately by DM.

For the point about how ranges work in python, watch this:

v = np.array(range(8))
print(f"v = {v}")
print(v[0:4])
v = [0 1 2 3 4 5 6 7]
[0 1 2 3]

thanks a lot , will folllow your suggestion !!