C4W1 exercise 3, "too many indices for array" for Z value

I don’t know why it shows too many indices for array, I print out the shape and both a_slice_prev and weights are in shape 3,3,4 and biases is in shape 1,1,1. So I don’t know what’s the problem here, could someone help me out?

1 Like

{deleted reply, since it wasn’t accurate}

1 Like

Take a look at how you are initializing the return variable Z

It should be consistent with the guideline provided in the doc string of the function:

    Returns:
    Z -- conv output, numpy array of shape (m, n_H, n_W, n_C)
1 Like

Thanks. I’m not a python expert.

Thank you so much!!! But should I also discard conv_single_step? since this function will return a scalar value rather than an array?


I don’t quite understand what does "index 4 is out of bounds for axis 2 with size 4
" mean here

Z[i,h,w,c] is a single location in a 4D matrix. Each location holds a single value. You are fine storing the value returned by a function there.

However, the index values i, h, w, c must comply with the shape provided when Z was initialized. The error message is telling you that the index you are using during the assignment of the function return is inconsistent with the shape of Z. This means either the index was computed incorrectly in the for loops (maybe the range limit is too large???) or Z wasn’t initialized to the proper shape in the first place.

Specifically it appears you are trying to assign a value to a location using an index of 4, but that dimension of Z is of size 4 and thus the index must be 0,1,2 or 3. Hope this helps.

2 Likes

Thank you so much!!! I got it right now!!! I got the wrong range for I, h, w, c

1 Like

I’ve deleted that reply.