Week 1 exercise 3 (operands could not be broadcast together with shapes (1,2,8,4) (3,3,4) )

Hello everyone, I am stuck in exercise 3 of the first task of week 1, I get the following error:

I have searched other posts to see if I can solve the problem but I have seen that all are of the style (3,3,2) (3,3,4) as to give an example, but in my case is a dimension of 4 vs one of 3, I suspect that the error is due to a_prev_pad = A_prev_pad[: , : ,:, :] but I am not sure, I would appreciate if you could help me since I have been a day and I have not been able to advance in this task :V

I do not know if I put the code so that it is easier to find the error and when this solved I eliminate the code, anything tell me if I do it, thanks.

Yes, your a_slice_prev is the wrong shape. So where is that shape determined? I added some print statements to my code to see the shapes of the relevant objects. Please compare yours to these values and also check carefully your code for setting a_slice_prev.

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 =
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]

paulinpaloalto thank you for helping me, I checked the dimensions that I name and this is what I get
the other values like Z[0,0,0,0,0] = -2. 65112362629553914 do not appear I guess it is because the function does not run because of the problem with the a_slice_prev, verifying that the dimensions of A_prev_pad and Z are correct, I do not know if the problem is in how I’m handling the variables of vert_start/end and horiz_start/end and loops with the stride, I will put the code if there is problem tell me and delete it is that the truth I have reviewed the theory and do not see the problem but I do it to rule out.

{moderator edit - solution code removed}

Note that the outer “for” loop is over the samples. The “sample” dimension is the first dimension, so the way you are computing a_prev_pad from A_prev_pad looks wrong. You are taking all of the first three dimensions. The value i needs to appear there, right? You need to be selecting the i-th sample from A_prev_pad, which will give you a 3D tensor. Print the shape of your a_prev_pad. It should be (7,9,4), but I’ll bet yours is not that shape.

And note that is only the first problem with your code. You are also using the stride incorrectly: you are striding over the output space. That means you will be skipping positions in the output space. That’s backwards, right? You skip positions in the input space, but you must touch every position in the output space. The height and width and channels loops need to touch every output value (for every sample, which is the outer loop).

I will go ahead and remove the current version of your code. Please consider the things I said above and give it another try.

1 Like

thank you very much for the help I did it, I thought that the stride in the range was the correct way :smiley: