Course 4, Week 1, Assignment 1 Exercise 3 indexing issue

Hi there,

I’m getting the following error based on the input for full convolution forward:

weights = W[:,:,:,c]
biases = b[:,:,:,c]
Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)

—> 10 print(“Z[0,2,1] =\n”, Z[0, 2, 1])
IndexError: index 2 is out of bounds for axis 1 with size 1

Any ideas? I suspect it has something to do with how I’m calling the weights/biases, but not sure.

Also, in the test code, Z gets called with three inputs instead of 4…also seems a bit strange considering we’re assigning 4 to it.

Hi @DS1,

In week 1 of course 2 there are three assignments, can you please clarify in which assignment are you having this issue?

Or maybe this pertains to course 4?

Hi @kampamocha

Sorry for the confusion

This is for assignment 1, so:

Course 2 (CNN’s), Assignment 1, Week 1, Exercise 3, function name: conv_forward

I think you mean Course 4, not Course 2. I will update the title and the category.

1 Like

Apologies! I think I’ve been taking the courses in reverse… yes course 4.

I thought course 2 was CNN’s…

But to your original question, note that you can supply fewer indices to a tensor. E.g. if you index a 4D tensor with 3 indices, you fix the first three indices and then you enumerate the last dimension.

But in your case, it looks like Z is the wrong shape. The third dimension should have enough room for index 1 to be valid, but in your case it doesn’t.

I added some print statements to my code to show the dimensions of the various objects. A place to start would be to compare these shapes to what you are seeing.

New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
a_prev_pad shape = (7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
a_prev_pad shape = (7, 9, 4)
Z[1,2,3,7] = 0.4427056509973153
Z's mean =
 0.5511276474566768
Z[0,2,1] =
 [-2.17796037  8.07171329 -0.5772704   3.36286738  4.48113645 -2.89198428
 10.99288867  3.03171932]
cache_conv[0][1][2][3] =
 [-1.1191154   1.9560789  -0.3264995  -1.34267579]
New dimensions = 9 by 11
Shape Z = (2, 9, 11, 8)
Shape A_prev_pad = (2, 11, 13, 4)
a_prev_pad shape = (11, 13, 4)
Z[0,0,0,0] = -1.2238796505752447
a_prev_pad shape = (11, 13, 4)
Z[1,8,10,7] = -0.47458986707940803
New dimensions = 2 by 3
Shape Z = (2, 2, 3, 8)
Shape A_prev_pad = (2, 5, 7, 4)
a_prev_pad shape = (5, 7, 4)
Z[0,0,0,0] = 3.14880664541713
a_prev_pad shape = (5, 7, 4)
Z[1,1,2,7] = 1.0956417259542868
New dimensions = 13 by 15
Shape Z = (2, 13, 15, 8)
Shape A_prev_pad = (2, 17, 19, 4)
a_prev_pad shape = (17, 19, 4)
Z[0,0,0,0] = -0.5096687406137471
a_prev_pad shape = (17, 19, 4)
Z[1,12,14,7] = -0.3247422640409677
(2, 13, 15, 8)
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
a_prev_pad shape = (7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
a_prev_pad shape = (7, 9, 4)
Z[1,2,3,7] = 0.4427056509973153
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
a_prev_pad shape = (7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
a_prev_pad shape = (7, 9, 4)
Z[1,2,3,7] = 0.4427056509973153
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
a_prev_pad shape = (7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
a_prev_pad shape = (7, 9, 4)
Z[1,2,3,7] = 0.4427056509973153
 All tests passed.

Thank you Paul!

Turns out I had an arithmetic error (used - instead of + in a basic calculation)…

Not bad for having accidentally skipped two courses in the specialization though!

Cheers,
David

1 Like