Course 4 , Week 1 ,Assignment 1,Excercise 3-setting an array element with a sequence

TypeError Traceback (most recent call last)
TypeError: only size-1 arrays can be converted to Python scalars

The above exception was the direct cause of the following exception:

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)
78 weights= W[:,:,c]
79 bias= b[:,:,c]
—> 80 Z[i, h, w, c]=conv_single_step(a_slice_prev, weights, bias)
81
82 # YOUR CODE ENDS HERE

ValueError: setting an array element with a sequence.
Hello everyone , I just tried to reshape to 4D in zero_pad of a_prev_pad and reshaped while slicing a_prev_pad to broadcast with Weights.Without all these steps I can’t proceed. Is there any mistakes in my steps?

Are you sure your conv_single_step function is correct? What is the type of the return value? It should be a python scalar float. Of course that could also be because you passed it incorrect arguments. Check how you are defining weights and bias there. Note that the arrays W and b have 4 dimensions, but you are only specifying three of them. That will probably result in incorrect shapes.

a_slice_prev=a_prev_pad[0,vert_start:vert_end,horiz_start:horiz_end,:].reshape(vert_end-vert_start,horiz_end-horiz_start,a_prev_pad.shape[3])
weights= W[:,:,:,c]
bias= b[:,:,:,c]
but error is showing as :
—> 83 Z[i, h, w, c]=conv_single_step(a_slice_prev, weights, bias)
84
85 # YOUR CODE ENDS HERE

IndexError: index 4 is out of bounds for axis 3 with size 4

I corrected mistakes which I can understand.
Well I think I am confused with this error but I cannot understand where is the mistake.
Please help me!!!

Well there is no reshape in this ,It was done bymistake.
It would be great if anybody can give a hint to my error.

But there is a reshape there in the most recent code that you show. That should not be necessary. Also note that you are giving 4 dimensions for a_prev_pad, but it should only be a 3D tensor by that point: you already indexed off the “samples” dimension in the outer loop, right?

Also note that the actual error being thrown is complaining about the Z[i, h, w, c] on the LHS of that assignment statement. So either your Z is the wrong shape or your index limits on the loops are wrong.

I added some print statements to my conv_forward logic and here’s what I see on the conv_forward test cases:

stride 2 pad 1
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]
First Test: All tests passed!
stride 1 pad 3
New dimensions = 9 by 11
Shape Z = (2, 9, 11, 8)
Shape A_prev_pad = (2, 11, 13, 4)
Z[0,0,0,0] = 1.4306973717089302
Z[1,8,10,7] = -0.6695027738712113
stride 2 pad 0
New dimensions = 2 by 3
Shape Z = (2, 2, 3, 8)
Shape A_prev_pad = (2, 5, 7, 4)
Z[0,0,0,0] = 8.430161780192094
Z[1,1,2,7] = -0.2674960203423288
stride 1 pad 6
New dimensions = 13 by 15
Shape Z = (2, 13, 15, 8)
Shape A_prev_pad = (2, 17, 19, 4)
Z[0,0,0,0] = 0.5619706599772282
Z[1,12,14,7] = -1.622674822605305
Second Test: All tests passed!

Try printing the shape of your Z and compare to what I showed above.