Stuck on assignment week 1 ex 3

First off, sorry if this is obvious, but without knowing what the checker is expecting, it is hard to find bugs. I made a leap by jumping into this course without completing the previous ones in the Deep Learning track, nor did I program in Python before. Thank you in advance for any insights.

My code:
(m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape
(f, f, n_C_prev, n_C) = W.shape
stride = hparameters[“stride”]
pad = hparameters[“pad”]
n_H = ((n_H_prev - f + 2 * pad) // stride) + 1
n_W = ((n_W_prev - f + 2 * pad) // stride) + 1
Z = np.zeros((m, n_H, n_W, n_C))

A_prev_pad = zero_pad(A_prev, pad)
for i in range(m):
  a_prev_pad = A_prev_pad[i]
  for h in range(0, n_H, stride):
    vert_start = h
    vert_end = h + f
    for w in range(0, n_W, stride):
      horiz_start = w
      horiz_end = w + f
      a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
      for c in range(n_C):            
        weights = W[:,:,:,c] 
        biases = b[:,:,:,c]      
        Z[i, h, w, c] = conv_single_step(a_slice_prev, weights, biases)  


The checker output:
Z’s mean =
Z[0,2,1] =
[0. 0. 0. 0. 0. 0. 0. 0.]
cache_conv[0][1][2][3] =
[-1.1191154 1.9560789 -0.3264995 -1.34267579]
(2, 13, 15, 8)
Error: Wrong output for variable in position 0.
2 Tests passed
1 Tests failed

Without any Python or TensorFlow experience, you have a difficult task ahead. The courses are intended to be taken as a complete sequence.

Starting with:

  • All of your “shape” code should use “np.shape(variable_name)”.

  • I recommend you use “/” instead of “//”

  • Your code for a_prev_pad = … is incorrect. You need to slice the array, not just specify one parameter. The method is similar to what was used for the “weights” and “biases” code later in the function.

  • vert_start needs to include the use of “stride”.

  • vert_end should reference vert_start.

  • Similar changes are needed in the horiz computations.


Thanks, I got it to work. The bug was, as you pointed out, that h and w in the loops are referencing coordinates in the next layer, while *_start and *_end should be referencing the previous layer.

Curiously, the way I was obtaining shape, getting a_prev_pad, and getting the floor value for width and height, had no effect on the solution, although I’ll start coding in the style you recommended.