# 1 test failed in Graded Function Propagate

Hello,

I am struggling with this one function in Week 2’s Logistic Regression Assignment. In the function def propagate(), I am expected to write the vectorized formulas for the Activation function, the cost and the gradients dw and db. Everything above this point has run fine! When I execute this particular cell and the cell below it, I get the message: 2 tests passed, 1 failed and the error is Error: Datatype mismatch in variable 1. Got type: <class ‘numpy.float64’> but expected type <class ‘numpy.ndarray’>

Although my output values match exactly with the expected output values, I don’t know where I am going wrong and why is this error persistently coming up. I am attaching a snippet of my code and the results that I am getting below(This is to give better context to the DLAI mentors). Hope to receive some help regarding this at the earliest. Thank You.
-------------------------------------------CELL---------------------------------------------------------------

def propagate(w, b, X, Y):
“”"
Implement the cost function and its gradient for the propagation explained above

Arguments:
w -- weights, a numpy array of size (num_px * num_px * 3, 1)
b -- bias, a scalar
X -- data of size (num_px * num_px * 3, number of examples)
Y -- true "label" vector (containing 0 if non-cat, 1 if cat) of size (1, number of examples)

Return:
cost -- negative log-likelihood cost for logistic regression
dw -- gradient of the loss with respect to w, thus same shape as w
db -- gradient of the loss with respect to b, thus same shape as b

Tips:
- Write your code step by step for the propagation. np.log(), np.dot()
"""

m = X.shape[1]

# FORWARD PROPAGATION (FROM X TO COST)
#(≈ 2 lines of code)
# compute activation
# A = ...
# compute cost using np.dot. Don't use loops for the sum.
# cost = ...

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

# BACKWARD PROPAGATION (TO FIND GRAD)
#(≈ 2 lines of code)
# dw = ...
# db = ...

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

cost = np.squeeze(cost)

"db": db}

---------------------------------------CELL-----------------------------------------------------
w = np.array([[1.], [2.]])
b = 2.,
X =np.array([[1., 2., -1.], [3., 4., -3.2]])
Y = np.array([[1, 0, 1]])
grads, cost = propagate(w, b, X, Y)

print ("dw = " + str(grads[“dw”]))
print ("db = " + str(grads[“db”]))
print ("cost = " + str(cost))

## propagate_test(propagate) -------------------------------------CELL---------------------------------------------------------

dw = [[0.99845601]
** [2.39507239]]**
db = 0.001455578136784208
cost = 5.801545319394553

## Error: Datatype mismatch in variable 1. Got type: <class ‘numpy.float64’> but expected type <class ‘numpy.ndarray’> 2 Tests passed 1 Tests failed

AssertionError Traceback (most recent call last)
in
14 print ("cost = " + str(cost))
15
—> 16 propagate_test(propagate)

~/work/release/W2A2/public_tests.py in propagate_test(target)
91 ]
92
—> 93 multiple_test(test_cases, target)
94
95 def optimize_test(target):

~/work/release/W2A2/test_utils.py in multiple_test(test_cases, target)
140 print(’\033[92m’, success," Tests passed")
141 print(’\033[91m’, len(test_cases) - success, " Tests failed")
→ 142 raise AssertionError(“Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))
143

AssertionError: Not all tests were passed for propagate. Check your equations and avoid using global variables inside the function.

Expected output

dw = [[0.99845601]
** [2.39507239]]**
db = 0.001455578136784208
cost = 5.801545319394553

Hello @NeelD1999,

Welcome and good job working on the logistic regression problem. You are almost there!

If you check the comments for this particular task, you get the advice to use np.dot for the cost computation (# compute cost using np.dot. Don't use loops for the sum.). However, you can also use np.sum, if you make sure the shape returned corresponds to the shape returned by using np.dot for this task.

I will give you an example that hopefully helps you understand the difference between np.sum and np.dot for this task:

import numpy as np

v = np.array([[1.],[2.],[3.]])
print(v)

cost = np.sum(v)
print("cost:", cost)
print("cost.dtype:", cost.dtype)
print("cost.shape:", cost.shape)

cost2 = np.sum(v, keepdims=True)
print("cost2:", cost2)
print("cost2.dtype:", cost2.dtype)
print("cost2.shape:", cost2.shape)

ones = np.array([[1],[1],[1]])
cost3 = np.dot(ones.T, v)
print("cost3:", cost3)
print("cost3.dtype:", cost3.dtype)
print("cost3.shape:", cost3.shape)

Output:

[[1.]
[2.]
[3.]]
cost: 6.0
cost.dtype: float64
cost.shape: ()
cost2: [[6.]]
cost2.dtype: float64
cost2.shape: (1, 1)
cost3: [[6.]]
cost3.dtype: float64
cost3.shape: (1, 1)

You can read more about np.sum on numpy.sum — NumPy v1.20 Manual

Try changing the cost calculation to only using np.dot operations. It is a good exercise in linear algebra.

3 Likes

Hey @jonaslalin,

Thank you so much for that explanation along with that example! I understood what you meant to explain to me. Basically, I ended up losing the dimensions when I did the np.sum() operation directly. So, I fixed it as you suggested and it works perfectly now. All tests passed! Appreciate your help.

Also, I have passed all the tests but I don’t see a submit assignment button. I have noticed many students have reported this issue. I hope to get a fix for that so that I can submit this assignment and go on exploring the course.

1 Like

Do you have multiple assignments opened at the same time ?
Also, can you mention which OS and browser you are using ? Thanks!

You are welcome @NeelD1999.

Hi @Mubsi

I don’t have multiple assignments open at the same time.

I am using Windows 10 and Google Chrome.

Thanks

I’m so confused! I can get it to run fine using the long written-out formula like this ^. But I can’t figure out how to use np.sum() or np.dot() to get the same results.

1 Like

Hello @parrotox,

If you multiply a row vector by a column vector, it is effectively summing up the multiplication of each corresponding element. I recommend you to read more about Dot product - Wikipedia.

Let me give you an example that you can play with:

import numpy as np
a = np.array([[1,2,3]])
b = np.array([[4,5,6]])
print(a)
print(b)
print(b.T)
sum1 = np.sum(a * b, keepdims=True)
print(sum1)
sum2 = np.dot(a, b.T)
print(sum2)

outputs

[[1 2 3]]
[[4 5 6]]
[[4]
[5]
[6]]
[[32]]
[[32]]

Can you now see how you can change the np.sum into np.dot?

2 Likes

Thank you! Yes, this is very helpful. I think I was completely missing the boat on what dot product means. I’ve been using your example to try all kinds of things to see what happens and it’s starting to click.

1 Like