W2_A2_propagate function_np.ndarray

Output of the test of the “propagate”-function:
When I print the type of my calculated dw, I get <class ‘numpy.ndarray’>, but the assert dosn’t work as it wants to get “np.ndarray”

I think you must be misinterpreting the results. I added print statements inside propagate and here’s what I see when I run that test cell:

type(db) = <class 'numpy.float64'>
type(dw) = <class 'numpy.ndarray'>
dw.shape = (2, 1)
dw = [[ 0.25071532]
 [-0.06604096]]
db = -0.1250040450043965
cost = 0.15900537707692405
type(db) = <class 'numpy.float64'>
type(dw) = <class 'numpy.ndarray'>
dw.shape = (3, 1)
All tests passed!

So you can see that the type of my dw is the same as you are seeing, but the assert does not “throw” in my case. Hmmmmmm. Please show us the actual output you are getting and perhaps there is a clue there about what really happened.

1 Like

Yes thanks, I was in the wrong line of the ERROR stack.
So my problem is that in the propagation function, I get the expected value for the cost calculation.
But I get an ValueError: cannot reshape array of size 4 into shape (3,1)
So something is wrong with the shape but I cant figure out what it is.
My code is:
A = sigmoid(np.dot(w.T, X) + b)

  • cost = (-1 / m) * np.sum(np.dot(Y, np.log(A.reshape(3,1))) + (np.dot((1 - Y), np.log(1 - A.reshape(3,1)))))*
    and the output of cost is: cost = 0.15900537707692405 as expected.
    I got stuck on that issue. Maybe you got a hint

Okay I found my bug I have to use the given shape of A and Y:
cost = (-1 / m) * np.sum(np.dot(Y, np.log(A.reshape(A.shape[1],1))) + (np.dot((1 - Y), np.log(1 - A.reshape*(A.shape[1],1)**))))*

Are those reshape() functions necessary?

You need something there to deal with the vector orientation issue, but transpose is a better way to accomplish that goal. Also note that the point of using np.dot is that it does the multiply and the sum in “one shot”, so you don’t also need the np.sum there. It does no harm, but it’s a waste of code. If you were willing to include the np.sum call, then you could just use np.multiply (or “*”) and then you wouldn’t need to rearrange the layout of the second operand in each case.

Thanks for that information