Week 4 assignment 1 conv_forward error

i could not solve the error in my code, help will be appreciated.

error is IndexError: too many indices for array . But i think there is no problem about the shape of the “weights” and “a_slice_prev” . Help will be appreciated.

Do not post your code on the Forums. That breaks the Honor Code. Please edit your message and remove the code.

Tip: The vert_start and horiz_start need to include a multiplication by “stride”.

1 Like

sorry. deleted the code . but in the loop i used range with stride.
ex: range(0,n_H_prev-f+1,stride)
double checked the vertical and horizontal indices. they seem ok.

the error i got is different. the last line Z[i,h,w,c]=np.sum(w*x)+float(b) raises the error ’ too many indices for array’ .
probably shape mismatch but i did not understand why?

error_output
83 print(i,h,w,c)
—> 84 Z[i, h, w, c]=np.sum(w*x)+float(b)
85 print(Z)
86
IndexError: too many indices for array

Why are you using np.sum there? That is the point at which you should be calling conv_single_step, right? Don’t they say that in the instructions? But to do that you also have an important step to “index” the weights and bias values. That b there is actually 4D array. You don’t want the pass the whole thing, right?

Also note that the loops here on h, w and c are over the output volume, right? Then your job in the loop is to compute where that maps to in the input space.

thanks,
ok i modified the last step with the “conv_single_step” like you mentioned . also updated the input output indexes i think.
for the conv single step i send w=3d array, X=3d array, b=3d array . for the test case; w=(3,3,4) biases (1,1,1) .
the output shape should be 2x3x4x8 for the test case. (m=2, nC=8)

if i do not use Z[i, h, w, c] at the end and use Z=conv_single_step(a_slice_prev, weights, biases) ,
size of “Z” is 2x3x4x8 and mean is 0.4427056509973153 .

but i got error :
—> 10 print(“Z[0,2,1] =\n”, Z[0, 2, 1])
11 print(“cache_conv[0][1][2][3] =\n”, cache_conv[0][1][2][3])
12

IndexError: invalid index to scalar variable.

if i use Z[i,h,w,c] , i got error :

—> 88 Z[i,h,w,c]=conv_single_step(a_slice_prev, weights, biases)
89
90

IndexError: too many indices for array

what am i missing now?

example indices of z and corresponding values from my code (first and last):

0 0 0 0
-2.651123629553914

1 2 3 7
0.4427056509973153

The whole point of that big 4 levels of nested loops is that in each iteration you set exactly one element of the 4D array that is Z. The output of conv_single_step is a scalar value, so of course it doesn’t work to assign that simply to Z as opposed to the appropriately indexed position in Z.

But what this says is that when you use the correct assignment to one element of Z, there must be some other problem that causes that value to actually be incorrect. The hardest part in all of this is the computing the index values for the positions in the input space. Are you still using the same “range” code that you showed above in your original post? If so that is going to cause problems. Remember what I said earlier: the loops here are over the output volume. You have to step through every possible value of i, h, w, and c in the output space. That means the “step size” is always 1, right? Otherwise it means you are skipping positions in the output. At each one of those steps you have to be deriving the answer by calling conv_single_step on the correct point in the input space.

I added similar instrumentation to my code and here are my outputs from the first test in the standard test cell:

New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
Z[0,0,0,0] = -2.651123629553914
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]

You can see that my values for Z[0,0,0,0] and Z[1,2,3,7] are the same as yours, so that is a good sign.

yes, i changed my loops:
for h in range(0,XX)
XX is then used stride multiplication at the iterations (h*s), plus filter size (start+f) to match the beginning and end of the convolution window.
by the way our means are different because i could not fill Z [i,w,h,c]. because i got the same error “too many indices” . i already googled this error and it says dimension mismatch. i can not assign the output of the conv_single_step to Z[i,w,h,c]. its a scalar value but it needs some conversion perhaps, i am not sure. i think its a python issue. when i print the shape of the returned value from conv_single_step, its () , and len is 1. at the pooling part similar assignment works ok and i can pass that part. but stuck in the convolution step.

|0 0 0 0|-2.651.123.629.553.910|
|0 0 0 1|-0.37849176551833935|
|0 0 0 2|-1.970.549.285.291.890|
|0 0 0 3|-19.623.529.930.298.200|
|0 0 0 4|-1.722.598.716.353.240|
|0 0 0 5|0.4676692981762709|
|0 0 0 6|-643.434.016.261.308|
|0 0 0 7|11.076.499.389.829.900|
|0 0 1 0|467.692.928.425.036|
|0 0 1 1|429.865.415.194.371|
|0 0 1 2|-13.608.031.011.814.100|

|1 2 2 1|35.073.964.881.857.600|
|1 2 2 2|0.8251220227943832|
|1 2 2 3|4.806.554.890.201.450|
|1 2 2 4|-4.104.494.499.685.090|
|1 2 2 5|4.143.585.406.590.580|
|1 2 2 6|0.13194884907078794|
|1 2 2 7|4.353.972.846.494.880|
|1 2 3 0|4.912.983.635.468.990|
|1 2 3 1|-14.449.977.173.929.500|
|1 2 3 2|5.939.207.802.246.890|
|1 2 3 3|-3.926.904.077.024.090|
|1 2 3 4|21.284.030.896.719.500|
|1 2 3 5|12.723.740.197.035.300|
|1 2 3 6|15.699.258.064.931.400|
|1 2 3 7|0.4427056509973153|

i calculated the mean and its same as yours for the values above.

It sounds like perhaps your Z array is just incorrectly initialized. It should be a 4D numpy array with dimensions m x nH x nW x nC. Also check to make sure that your conv_single_step function passes the test cases, which include checking to make sure that it is a scalar float.

my loops were not ok at the beginning (which i updated after your suggestion) but the error i got was the initialization problem all this time. i did not notice i initialized Z as 3d array. Thanks for your help.

1 Like