Week 1 Assignment 1 conv_backward

I’m stuck with this error, why is it happening?


ValueError Traceback (most recent call last)
in
10
11 # Test conv_backward
—> 12 dA, dW, db = conv_backward(Z, cache_conv)
13
14 print(“dA_mean =”, np.mean(dA))

in conv_backward(dZ, cache)
88 # Pad A_prev and dA_prev
89 A_prev_pad = zero_pad(A_prev, pad)
—> 90 dA_prev_pad = zero_pad(dA_prev, pad)
91
92 for i in range(m): # loop over the training examples

in zero_pad(X, pad)
17 # X_pad = None
18 # YOUR CODE STARTS HERE
—> 19 X_pad = np.pad(X, ((0,0), (pad, pad), (pad, pad), (0,0)))
20
21 # YOUR CODE ENDS HERE

<array_function internals> in pad(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/lib/arraypad.py in pad(array, pad_width, mode, **kwargs)
746
747 # Broadcast to shape (array.ndim, 2)
→ 748 pad_width = _as_pairs(pad_width, array.ndim, as_index=True)
749
750 if callable(mode):

/opt/conda/lib/python3.7/site-packages/numpy/lib/arraypad.py in _as_pairs(x, ndim, as_index)
521 # Converting the array with tolist seems to improve performance
522 # when iterating and indexing the result (see usage in pad)
→ 523 return np.broadcast_to(x, (ndim, 2)).tolist()
524
525

<array_function internals> in broadcast_to(*args, **kwargs)

/opt/conda/lib/python3.7/site-packages/numpy/lib/stride_tricks.py in broadcast_to(array, shape, subok)
180 [1, 2, 3]])
181 “”"
→ 182 return _broadcast_to(array, shape, subok=subok, readonly=True)
183
184

/opt/conda/lib/python3.7/site-packages/numpy/lib/stride_tricks.py in _broadcast_to(array, shape, subok, readonly)
125 it = np.nditer(
126 (array,), flags=[‘multi_index’, ‘refs_ok’, ‘zerosize_ok’] + extras,
→ 127 op_flags=[‘readonly’], itershape=shape, order=‘C’)
128 with it:
129 # never really has writebackifcopy semantics

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (4,2) and requested shape (1,2)

I had made a mistake in initializing dA_prev, dW and dB. It works after fixing that.