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)
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)
7 assert type(grads[“dw”]) == np.ndarray

in propagate(w, b, X, Y)
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))))

<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)
7 assert type(grads[“dw”]) == np.ndarray
8 assert grads[“dw”].shape == (2, 1)
----> 9 assert type(grads[“db”]) == np.float64


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)
F_1 = type(grads[“db”])

<class ‘numpy.ndarray’>

I still do not get the type as float

Can you please help here?

Your calculation of db is correct. After a summation, db should be “float”.
But, it does not mean db in a dictionary grad is “float”, since I do not see any update statement between two.

I’m curious, since grad is created in propagate() just before returning to a caller. So, when you calculate db, there is no grads as a local variable in propagation().

Anyway, an equation to calculate db seems to be correct. Please remove all you additions for debugging which are not part of your real assignments, then, test with given test programs.

I was having the same problem, but I got the correct code with what you said. How come in this usage of np.sum(A-Y) you do not have to mention anything about axis or keepdims? Thanks!

Hi @Darsh_Khandelwal ,

The default for axis is set to none. Here is the reference doc that you can refer to for details of all the parameters.