Yes, the shapes of Z and A_prev_pad are static, so there is no point in printing them out on every iteration of your inner loop. Maybe you are not taking the stride into account correctly somehow. It looks like you are correctly using the output space as the loop limits, but the question is how you compute where that maps to back in the input space. Maybe you should show the nH_{prev} values in your loop to see how they are moving as you loop through.
As an example, I added a print statement in the inner loop and hereβs enough of the output to see what is going on:
New dimensions = 3 by 4
Shape Z = (2, 3, 4, 8)
Shape A_prev_pad = (2, 7, 9, 4)
Z[0,0,0,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,0] = -2.651123629553914
Z[0,0,0,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,0,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[0,0,1,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,1,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[0,0,2,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,2,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[0,0,3,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,0,3,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[0,1,0,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,0,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 0:3
Z[0,1,1,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,1,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 2:5
Z[0,1,2,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,2,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 4:7
Z[0,1,3,0] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,1] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,2] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,3] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,4] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,5] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,6] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,1,3,7] a_slice_prev.shape (3, 3, 4) vert 2:5 horiz 6:9
Z[0,2,0,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,0,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 0:3
Z[0,2,1,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,1,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 2:5
Z[0,2,2,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,2,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 4:7
Z[0,2,3,0] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,1] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,2] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,3] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,4] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,5] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,6] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[0,2,3,7] a_slice_prev.shape (3, 3, 4) vert 4:7 horiz 6:9
Z[1,0,0,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,0,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 0:3
Z[1,0,1,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,1,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 2:5
Z[1,0,2,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,2,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 4:7
Z[1,0,3,0] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,1] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,2] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,3] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,4] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,5] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,6] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9
Z[1,0,3,7] a_slice_prev.shape (3, 3, 4) vert 0:3 horiz 6:9