Help On Course 4 Excersise 1 conv_forward(A_prev, W, b, hparameters)

Hello Mentor ,

I need help here :

I am getting following error
Debug a shape is : (3, 3, 4)
Debug Wt shape is : (3, 3, 4)


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)
104 a_T = np.transpose(a_slice_prev)
105 #weights = np.add(np.dot(W_slice,a_slice_prev))
→ 106 weights = np.add(np.dot(W_slice,a_T))
107 biases = b[:,:,c]
108 Z[i, h, w, c] = weights + biases

<array_function internals> in dot(*args, **kwargs)

ValueError: shapes (3,3,4) and (4,3,3) not aligned: 4 (dim 2) != 3 (dim 1)

Here is my code:

{moderator edit - solution code removed}

Any help would be appreciated :slight_smile:

From the lecture I remember that n_ch(number of channels) in activation and weights should match . In my case it does as seen in the debug statement.

Thanks
Vinod

My bad actual error is
ValueError: shapes (3,3,4) and (3,3,4) not aligned: 4 (dim 2) != 3 (dim 1)

Updated code is:

{moderator edit - solution code removed}

Sorry for above inconvenience.

Thanks
Vinod

Why are you using np.dot there? You should be calling conv_single_step at that point, right? Of course that will do an “elementwise” multiply, not a dot product.

One other thing to note: it looks like you are ignoring the stride value in conv_forward. That will not end well.

Thanks for quick response .

I did fix the issue , however now I get different error , doesnot make sense , so bit confused.

Error snippet:

TypeError 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)
97 #print (“Debug Wt shape is :”,W_slice.shape)
98 biases = b[:,:,c]
—> 99 Z[i, h, w, c] = conv_single_step(a_slice_prev, W_slice, biases)[c]
100
101

in conv_single_step(a_slice_prev, W, b)
25 s = np.multiply(W,a_slice_prev)
26 Z = np.sum(s)
—> 27 Z = np.add(Z,np.float(b))
28 # YOUR CODE ENDS HERE
29

Here is my fix:
Stride fix:

{moderator edit - solution code removed}

      >>>> conv_single_step returns scalar , Z[i,h,w,c] is also scalar.

Hence not sure what does error mean.

Thanks
Vinod

Hey @Vinod_KumarSingh,
I am facing a bit of confusion as to what you are trying in the assignment, since there are considerable differences in your fixes from what should have been there in the first place. Can you please DM me your .ipynb notebook, so that I can understand what is the error that you are facing, and what you are trying to rectify it. Make sure to not post it here, since it is against the community guidelines.

Regards,
Elemento

Sorry to be slow in responding, but I was away from home yesterday.

There are several confusions there: the main one is that you don’t need to “slice” the W filter value in the height and width dimensions. It is already f x f x nC_{prev} x nC. You should only select the appropriate output channel, just as you do with the bias values.

And what is going on with the extra index value on this line of code:

—> 99 Z[i, h, w, c] = conv_single_step(a_slice_prev, W_slice, biases)[c]

You are indexing the output (return value) of the function. Why?

Hello Sir,

Thanks for your response.

Still I am not of woods :frowning:

Here is the error I am getting:

Sorry, I don’t see the error message.

What do you mean ?
Does my code work for you ?

Thanks
Vinod

Just for debug I had the below code in new jupyternotebook:

np.random.seed(1)
A_prev = np.random.randn(2, 5, 7, 4)
W = np.random.randn(3, 3, 4, 8)
b = np.random.randn(1, 1, 1, 8)

{moderator edit - solution code removed}

Here is the error:

Please realize that no-one else can see your notebooks. You said you are posting an error message, but it does not show in your post.

In the code you show in your next post (which is really not permitted by the rules, so I will edit the post to remove it) there are a number of mistakes:

Why are you calling np.pad directly? You already wrote a function to perform that task, right? You should call that instead of writing the code over again.

The way you are slicing the A_prev value does not look correct. Why always start with index 0?

Why are you always selecting channel 0 of the W value?

Is this conv_forward you are talking about? Or conv_single_step?

I think it would be a good idea to read through all the instructions in the notebook from the beginning. If that doesn’t help, then it might also be worth watching the lectures again.

I think there is a confusion here ,
The code i sent in previous email thread was
to mimic the issue i am seeing in the assignment in new jupyternote book .

Thanks
Vinod

Ok, but you still have not really shown us what the issue is. What is that actual error that you are getting? Please show us the exception trace.

Hello Paul,

As per your advise , I did revisit class lecture , and made update to my code ,
This is for def conv_forward(A_prev, W, b, hparameters):
Still no luck .

Here is the error:

Debug1 A_prev shape is  (2, 5, 7, 4)
Debug2 A_prev_pad shape is  (2, 7, 9, 4)
Debug a shape is : (3, 3, 4)
Debug Wt shape is : (3, 3, 4)
Debug b_slice shape is : (1, 1, 1)

Debug1 A_prev shape is (2, 5, 7, 4)
Debug2 A_prev_pad shape is (2, 7, 9, 4)
Debug a shape is : (3, 3, 4)
Debug Wt shape is : (3, 3, 4)
Debug b_slice shape is : (1, 1, 1)


IndexError 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)
108 biases = b[:,:,:,c]
109 print (“Debug b_slice shape is :”,biases.shape)
→ 110 Z[i, h, w, c] = conv_single_step(a_slice_prev, W_slice, biases)
111
112

IndexError: too many indices for array

Here is code snippet:

{moderator edit - solution code removed}

Pls update (remove my code ) once you get it

Your Z value is the wrong shape. You have omitted the “samples” dimension. It needs 4 dimensions, but when you initialize it you only give it 3. The error message is pretty clear: it tells you that Z doesn’t have 4 dimensions, so then the question is “how did that happen?”

Finally resolved ,
Issue was
Z = np.zeros((n_H,n_W,n_C))
instead of

Z = np.zeros((m,n_H,n_W,n_C))

Hint:
Z – conv output, numpy array of shape (m, n_H, n_W, n_C)

Thanks for the help .

Thanks
Vinod