# Week2 Programming Assignment -

HI, I’m currently working on Week2 Assignment. Need your help to move forward.

My cost function is:
A = sigmoid(np.dot(w.T,X)+b)
cost = np.dot(-1/m,((np.dot(Y,np.log(A)))+np.dot((1-Y),np.log(1-A))))

dw = 1/m * np.dot(X,(A-Y).T)
db = 1/m * (A-Y)

I get an error as below:

ValueError Traceback (most recent call last)
in
3 X = np.array([[1., -2., -1.], [3., 0.5, -3.2]])
4 Y = np.array([[1, 1, 0]])
----> 5 grads, cost = propagate(w, b, X, Y)
6

in propagate(w, b, X, Y)
32
33 A = sigmoid(np.dot(w.T,X)+b)
—> 34 cost = np.dot(-1/m,((np.dot(Y,np.log(A)))+np.dot((1-Y),np.log(1-A))))
35
36 # YOUR CODE ENDS HERE

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

ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)

Computation of `cost` is incorrect. The expression should have a `np.sum` term instead of which you are using `np.dot`

Two things…

1. (-1/m) is a constant. You do not need to be inside any operations like np.dot/np.sum.
2. You are very close. There are two ways to calculate the cost. One is to implement the sum of element-wise multiplication as Balaji suggested. The other is to use np.dot that you did. Your approach is better than using np.sum from the computational view point, but you need to be careful about the shape. You may need to transpose either the first term or 2nd term to get the summation as a result.

Thank you.
A = sigmoid(np.dot(w.T,X)+b)
cost = np.multiply(-1/m,((np.multiply(Y,np.log(A)))+np.multiply((1-Y),np.log(1-A))))

dw = 1/m * np.dot(X,(A-Y).T)
db = 1/m * (A-Y)

Are these right?

I get a different error now:
AssertionError Traceback (most recent call last)
in
8 assert grads[“dw”].shape == (2, 1)
----> 9 assert type(grads[“db”]) == np.float64
10
11

AssertionError:

And,…, (-1/m) does not depends on i. So, it can be OK with a simple multiplication.

Sorry for making you confused. I removed my first paragraph about summation. Please see below about the difference between two approaches.

Here is an overview of differences in approach.

“Simple multiplication + Summation” is straight forward. (And this may be what is requested for this exercise.). But, if you look at the approach for np.dot, it is just one operation. But, you need to be very careful about the shape. We need to “transpose” A, in this case.

1 Like

The db should be taken as the average of the sum of (A-Y). If you use np.sum(A-Y), the datatype np.float64 will be preserved.

Hi, I have changed code as below:

db = 1/m * np.sum(A-Y)
print(db)
print(F_1)

Output:
-0.1250040450043965
<class ‘numpy.ndarray’>

I still do not get the type as float