I have revised all the dimensions of the a_slice_prev and weights are the same and correct (comparing them to paulinpaloalto commented in other thread), but gives me an error I can’t understand.
edit: I changed the values in the for loop for height and width for values that seem correct, but get the following error. I don’t know anymore if the problem is in this indexes, it seems so. Someone please help me with this.
It looks like your loop logic must still be wrong. Maybe you have exchanged the meanings of h and w. Remember that h stands for height, not horizontal, right? And w is for width.
What is the shape of your Z value in that case? That’s what it is complaining about, right?
I added some print statements to my code and here’s what I get for that test cell:
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!
Thank you. I have not interchanged the values of h and w. What I do not have clear is the boundaries in the for loops. Watching the dimensions you sent, height I guess it’s h in range(0, n_H, stride), though considering the filter and the formula for the dimensions of the resulting matrix I thought it would be range(0, n_H - f +1, stride). Either way, the code lets it pass, but if the latter is not the case I do not understand why.
My indexing problem comes from the axis 3 which is the C. I don’t see any other option than for c in range(n_C), but it keeps showing the error "index 4 is out of bounds. I compared range(n_C_prev) but does not match the dimensions you gave me.
So I guess that the problems is within the boundaries of the for loop of C. If I type range(n_C_prev) it shows the first error I posted. If I type range(n_C) it shows the second “out of bounds” error
It is a mistake to include the stride in the range limits of the loop, so you need to get that sorted out first. Here is the way to think about convolution: the mapping is from a “patch” of the input space to each individual point in the output space. The loops are over the output space and must not skip any positions of the output. Then in the body of the loop, we need to calculate where the input patch is and that is where the striding happens. So it is not part of the loop limit: it is part of the calculation of vert_start
and horiz_start
.
Also your Z dimension is wrong. The number of output channels is not nC_{prev}, right? It’s nC_{out}. Those are different.
Ok, understood. I think I may have the error. I had np.zeros bad defined, now I see that it is np.zeros((m, n_H, n_W, n_C)). I finally got values, though they are wrong. I see it is probably in the calculations of vert_start and horiz_start, but I do not quite understand which correlation there is.
Almost: Z has 4 dimensions, right? You left out the “samples” dimension, which is the first dimension.
Oh, sorry, I see you fixed it.
Yes I corrected that! Now I’m figuring out the definition of vert_start