I’m working through the optional ungraded bit of assignment “Convolutional Model, Step by Step” and having trouble on the last part, Exercise 8, pool_backward. The assessment cell output tells me “AssertionError: Wrong values for mode max”. (I also get “wrong values” for the average case if I comment out the code for max).
I’ve been through the code carefully to check for trivial errors where I wrote something different from what I intended to write, and I think the code is clear of such things. I’ve checked the code as carefully as I can to see that it’s doing what is intended, according to the instructions, and to my best understanding of the maths. I’ve added various debugging prints and assertions to check my expectations. At least the dimensions of my calculations seem to be right, as I don’t get complaints about invalid dimensions. But there is still that error, which strongly suggests to me that my understanding is wrong.
As this is an optional, ungraded part of the assignment, I hope it should be ok for me to post my code here (let me know if I should remove it). I would appreciate it if someone could point out my mistake!
Below is my code for the backprop (with my debug removed), followed by the grader output with the error.
def pool_backward(dA, cache, mode = "max"):
"""
Implements the backward pass of the pooling layer
Arguments:
dA -- gradient of cost with respect to the output of the pooling layer, same shape as A
cache -- cache output from the forward pass of the pooling layer, contains the layer's input and hparameters
mode -- the pooling mode you would like to use, defined as a string ("max" or "average")
Returns:
dA_prev -- gradient of cost with respect to the input of the pooling layer, same shape as A_prev
"""
# YOUR CODE STARTS HERE
# Retrieve information from cache (≈1 line)
(A_prev, hparameters) = ...
# Retrieve hyperparameters from "hparameters" (≈2 lines)
stride = ...
f = ...
# Retrieve dimensions from A_prev's shape and dA's shape (≈2 lines)
m, n_H_prev, n_W_prev, n_C_prev = ...
m, n_H, n_W, n_C = ...
# Initialize dA_prev with zeros (≈1 line)
dA_prev = ...
for i in range(m): # loop over the training examples
# select training example from A_prev (≈1 line)
a_prev = ...
for h in range(n_H): # loop on the vertical axis
for w in range(n_W): # loop on the horizontal axis
for c in range(n_C): # loop over the channels (depth)
# Find the corners of the current "slice" (≈4 lines)
vert_start = ...
vert_end = ...
horiz_start = ...
horiz_end = ...
# Compute the backward propagation in both modes.
if mode == "max":
# Use the corners and "c" to define the current slice from a_prev (≈1 line)
a_prev_slice = ...
# Create the mask from a_prev_slice (≈1 line)
mask = ...
# Set dA_prev to be dA_prev + (the mask multiplied by the correct entry of dA) (≈1 line)
dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += ...
# elif mode == "average":
# Get the value da from dA (≈1 line)
da = ...
# Define the shape of the filter as fxf (≈1 line)
shape = ...
# Distribute it to get the correct slice of dA_prev. i.e. Add the distributed value of da. (≈1 line)
dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += ...
# YOUR CODE ENDS HERE
# Making sure your output shape is correct
assert(dA_prev.shape == A_prev.shape)
return dA_prev
The output from the grader reads
(5, 4, 2, 2)
(5, 5, 3, 2)
mode = max
mean of dA = 0.14571390272918056
dA_prev1[1,1] = [[ 0.08485462 0.2787552 ]
[ 6.32305492 -1.94032075]
[ 1.17975636 -0.53624893]]
mode = average
mean of dA = 0.14571390272918056
dA_prev2[1,1] = [[0. 0.]
[0. 0.]
[0. 0.]]
---------------------------------------------------------------------------AssertionError Traceback (most recent call last)
<ipython-input-24-14e1d5abab7e> in <module> 21 assert np.allclose(dA_prev1[1, 1], [[0, 0],
22 [ 5.05844394, -1.68282702],---> 23 [ 0, 0]]), "Wrong values for mode max"
24 assert np.allclose(dA_prev2[1, 1], [[0.08485462, 0.2787552],
25 [1.26461098, -0.25749373],
AssertionError: Wrong values for mode max


