C1W3A1 backward_propagation: ValueError: operands could not be broadcast together with shapes (7,9) (9,7)

A test for backward_propagation is erroring with:

<ipython-input-20-651db9ab4d4f> in backward_propagation(parameters, cache, X, Y)
     53     db2 = 1/m * np.sum(dZ2, axis = 1, keepdims = True)
---> 54     dZ1 = np.multiply(1 - np.power(Z1, 2), np.dot(W2.T, dZ2))
     55     dW1 = 1/m * np.dot(dZ1, X.T)

ValueError: operands could not be broadcast together with shapes (7,9) (9,7) 

which is caused by Z1 having different dimensions than the other operand of the element-wise product in dZ1. From the lecture Gradient Descent for Neural Networks, the dimensions are:

dZ^{[1]} = \underbrace{W^{[2]T}dZ^{[2]}}_\text{(n[1], m)} * \underbrace{g'^{[1]}(Z^{[1]})}_\text{(n[1], m)}

I was thinking that my implementation is wrong, and still doubt it isn’t, but after looking at the test code in public_tests.py:

def backward_propagation_test(target):
    cache = {'A1': np.random.randn(9, 7),
         'A2': np.random.randn(1, 7),
         'Z1': np.random.randn(7, 9),   # <=== this right here
         'Z2': np.random.randn(1, 7),}

shouldn’t A1 and Z1 have same dimensions?

yes, but in this implementation, we never use the z values from the cache, so it doesn’t impact the backprop function.

your code for dz1 is incorrect.

Thanks! Got it. And I was wondering why the code comments weren’t mentioning that we need to fetch Z1. That’s because we’re don’t.

1 Like