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---------------------------------------------------------------
GRADED FUNCTION: propagate
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 = ...
# YOUR CODE STARTS HERE
A = sigmoid(np.dot(w.T,X) + b)
cost = (-1/m) * np.sum((Y * np.log(A)) + (1 - Y) * np.log(1 - A))
# YOUR CODE ENDS HERE
# BACKWARD PROPAGATION (TO FIND GRAD)
#(≈ 2 lines of code)
# dw = ...
# db = ...
# YOUR CODE STARTS HERE
dw = (1/m) * np.dot(X, (A-Y).T)
db = (1/m) * np.sum(A - Y)
# YOUR CODE ENDS HERE
cost = np.squeeze(cost)
grads = {"dw": dw,
"db": db}
return grads, cost
---------------------------------------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)
assert type(grads[“dw”]) == np.ndarray
assert grads[“dw”].shape == (2, 1)
assert type(grads[“db”]) == np.float64
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