[week 1] conv_forward: Wrong output for variable in position 0

I got the following result after running the code and don’t know what does the sentence “Error: Wrong output for variable in position 0.” mean. Could you please help me? Thank you!

Z’s mean =
xxx
Z[0,2,1] =
xxx
cache_conv[0][1][2][3] =
xxx
Error: Wrong output for variable in position 0.
2 Tests passed
1 Tests failed

2 Likes

Hey,

Looks like the value of Z for you is incorrect. Can you check if you’ve used the correct a_slice_prev and weights and biases, when you loop over the channels?

1 Like

Thanks for the hint! I checked the code: a_slice_prev was calculated from a_prev_pad together with vert_start, vert_end…
Since we are in the loop n_C, it was [start: end, start:end, :]. Is it correct?
The weights and bias should be the same.
I couldn’t find my mistake in the code.

1 Like

Yes, the slice seems correct, what are you using for the weights and biases again?

1 Like

weights is W[:,:,:,c], similar to bias.
I got the following message:

“Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for conv_forward. Check your equations and avoid using global variables inside the function.

1 Like

Okay that also feels like that’s correct. I am out of ideas what it could be then now, can you please post your code for the function? As soon as you’re able to figure out the issue, you should delete it.

1 Like

Thank you for providing the code. You should check if you’ve taken the slice window (vertical and horizontal start and end) correctly with respect to the strides, that should solve your issue.

2 Likes

And please delete the code once, you’ve been able to solve it :slight_smile:

1 Like

Thank you very much for the hint! I will check it. I 've already deleted the code.

1 Like

I changed the code to consider strike in for loop h and loop w in range (n_H/W, stride). c in range n_C should be looped w/o stride. But there is still mistakes in the code which I can’t find.

I got the follow message:
Error: Wrong output for variable in position 0.
2 Tests passed
1 Tests failed


AssertionError Traceback (most recent call last)
in
11 print(“cache_conv[0][1][2][3] =\n”, cache_conv[0][1][2][3])
12
—> 13 conv_forward_test(conv_forward)

~/work/release/W1A1/public_tests.py in conv_forward_test(target)
382 ]
383
→ 384 multiple_test(test_cases, target)
385
386

~/work/release/W1A1/test_utils.py in multiple_test(test_cases, target)
151 print(’\033[91m’, len(test_cases) - success, " Tests failed")
152 raise AssertionError(
→ 153 “Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

AssertionError: Not all tests were passed for conv_forward. Check your equations and avoid using global variables inside the function.

1 Like

You should not need to change anything for the ranges in n_H and n_W because you calculated those a few lines earlier to keep stride in your mind. But know that the window moves stride times to the right or towards bottom for every step.

So if you started with horiz_start = 0, horiz_end = 2 let’s say for when you’re looping through n_W, and the stride is 4, the next horiz_start should be 4 and horiz_end should be 6.

You should review this lecture item to help with this. Good luck fixing the issue!

4 Likes

thank you very much for your quick reply. I thought firstly about using stride in calaculating horiz_start/end. But the first value should be start with 0. If I use stride as step in the loop, then it covers the start value zero.
for h in range(n_H, stride): v_start = h, h is from 0 to n_H-1 with step “stride”. first loop h= 0, second loop will be h=0+stride…This should be the same as you described above. Is it correct?
By the way: if stride = 4 as your example above and f = 2, why should the horiz_end = 8 and not 6?

2 Likes

Oh yeah, nice catch! I did not notice that I accidentally did that. Yes it would be 6 rather than 8. I’m sorry about that.

The provided formulas for n_H and n_W already keep the stride into account, so if you’re planning to use the range the way you are it’d be more of range(0, n_H*stride, stride) (you’ll have to specify the 0 as start because otherwise range defaults its arguments to (start, stop)). This should suffice for you.

But don’t you think there might just be an easier way to do it while calculating both vert_start, vert_end and horiz_start, horiz_end while keeping the range simply n_H/n_W?

1 Like

Thank you very much! I tried your proposal and changed vert_start/end and horiz_start/end and h in range (n_H), but still got the message above.

1 Like

what are you using for start and end now?

1 Like

vert_start = hstride, vert_end = hstride+f, similar to horiz_start/end

3 Likes

Now I rewrite the whole code in this function and passed the tests. I don’t know what was wrong before. Thank you very much for your help!

1 Like

Glad that you were able to pass the tests now!

1 Like

Just a note to anyone reading this thread in the future…

I think I figured out what was wrong since I had the same issue. It was as simple as my for loops not being nested so I had:

for i in range…

for h in range…

      for w in range...

                for c in range...

When it should have been:

for i in range…

      for h in range...

                for w in range...

                          for c in range...

Literally a typo but one that OP didn’t do again in their rewrite

6 Likes

It would be handy if the instructions mentioned that you need to use “stride” in two places.

  • in computing n_H and n_W.
  • in computing the vertical and horizontal start position of the slice. The starts are a multiple of the stride.
6 Likes