DLS4 Week1 Assignment 1 Optional ungraded part: Ex 8 pool_backward. Grader says "wrong values", I'd appreciate some help

Hello, I also have the same issue of AssertionError: Wrong values for mode max

Can anybody help me? Thanks

def pool_backward(dA, cache, mode = "max"):
    (A_prev, hparameters) = cache
    f = hparameters["f"]
    stride = hparameters["stride"]
    m, n_H_prev, n_W_prev, n_C_prev = A_prev.shape
    m, n_H, n_W, n_C = dA.shape
    dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev))
    for i in range(m):
        a_prev = A_prev[i,:,:,:]
        for h in range(n_H):
            for w in range(n_W):               # loop on the horizontal axis
                for c in range(n_C):
                    vert_start = h * stride
                    vert_end = vert_start + f
                    horiz_start = w * stride
                    horiz_end = horiz_start + f
                    if mode == "max":
                        a_prev_slice = a_prev[vert_start:vert_end,horiz_start:horiz_end,c]
                        mask = create_mask_from_window(a_prev_slice)
                        dA_prev[i,vert_start: vert_end, horiz_start: horiz_end, c] += mask * dA[i,vert_start: vert_end, horiz_start: horiz_end,c]
                    elif mode == "average":
                        da = dA[i,vert_start:vert_end,horiz_start:horiz_end,c]
                        shape = (f,f)
                        dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += distribute_value(da, shape)
    # Making sure your output shape is correct
    assert(dA_prev.shape == A_prev.shape)
    return dA_prev

I encountered the same error and it turned out that I used the wrong dimensions for the initialization of dA_prev. Thank you for the clear graphical explanation!

You saved me,… thanks

dA[i, h, w, c]

I got the same problem like you do

I have similar problem, and cant find whats wrong.
The error is on:
dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += mask * dA[i, vert_start: vert_end, horiz_start: horiz_end, c]

ValueError: non-broadcastable output operand with shape (2,1) doesn’t match the broadcast shape (2,2)

I have the same code like willtse (above), except the zero inits
Initialize dA_prev with zeros (≈1 line)
dA_prev = np.zeros((m, n_H, n_W, n_C))

Any suggestion? I dont want to send here the code (as I understand, its not desirable

Hello @Alfiik!

This thread is verbose (and old). Please start a new thread and share your full error.


Found a error in another thread (bad dA zero initialization).

I am glad you found it…

Thanks @Lee_Wong , I stuck 2 for this !!! It would be great if we can get some hint in the instruction.

@Shuwen1, I’m going to close this thread, because it would be much more effective if you start a new thread for your specific situation.

Posting on a thread that has been cold for a year is not a strategy for success.