# Week3 Assignment Cost Function Test failed

Hi, Sir,

When I calculate the cost function in this assignment:
def compute_cost(A2, Y):
“”"
Computes the cross-entropy cost given in equation (13)

``````Arguments:
A2 -- The sigmoid output of the second activation, of shape (1, number of examples)
Y -- "true" labels vector of shape (1, number of examples)

Returns:
cost -- cross-entropy cost given equation (13)

"""

m = Y.shape[1] # number of examples

# Compute the cross-entropy cost
# (≈ 2 lines of code)
# logprobs = ...
# cost = ...

cost = float(np.squeeze(cost))  # makes sure cost is the dimension we expect.
# E.g., turns [[17]] into 17

return cost
``````

A2, t_Y = compute_cost_test_case()
cost = compute_cost(A2, t_Y)
print("cost = " + str(compute_cost(A2, t_Y)))

compute_cost_test(compute_cost)

cost = 0.6926858869721941
Error: Wrong output
1 Tests passed
1 Tests failed

Expected output

`cost = 0.6930587610394646`

I tried np.multiple or np.dot, I got cost as 0.69268588 instead of 0.6930587. 1 Tests failed. Could you help me check what wrong it is.

Thanks,
Grace

Further: This function works well for later usage. No any other functions failed or test failed, so I think it is not a big deal, but due to some calculation errors.

Hello @graceyang, and welcome to the DL Specialization. Before we start you need to delete the box that contains your code. Please be mindful never to post any thing that you have entered to implement a function, i.e. anything between the # YOUR CODE STARTS HERE and # YOUR CODE ENDS HERE comments. Thanks!

@kenb Thanks to reply. I didn’t know how to delete part of my question. Does it mean that I can’t get suggestion if I can’t remove this part of codes? I tried, but don’t know how. So I only can post the result instead of my code?

@kenb Oh, I found the solution to remove my code. Now could you let me know why I got this error? I am still curious why my tests failed, but it works later.

Thanks, @graceyang. Can you post a snip of the cell output that identifies the sequence of errors? The is called the “traceback.” But you should still take care to not include any parts that reveal your code.

As I recall, you were having a problem with coding the expression for the cost function. You can start be making sure that you understand the mathematical function as presented in the notebook and then trying to express that understanding in the required Python code.

Hi @graceyang. I see that you did post the traceback. Apologies! It’s curious that your result is very close to the expected output. ← That’s me. Stay tuned!

Hello, have the same issue

cost = 0.6926858869721941
Error: Wrong output
1 Tests passed
1 Tests failed

AssertionError Traceback (most recent call last)
in
3 print("cost = " + str(compute_cost(A2, t_Y)))
4
----> 5 compute_cost_test(compute_cost)

~/work/release/W3A1/public_tests.py in compute_cost_test(target)
140 ]
141
→ 142 single_test(test_cases, target)
143
144 def backward_propagation_test(target):

~/work/release/W3A1/test_utils.py in single_test(test_cases, target)
119 print(’\033[92m’, success," Tests passed")
120 print(’\033[91m’, len(test_cases) - success, " Tests failed")
→ 121 raise AssertionError(“Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))
122
123 def multiple_test(test_cases, target):

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

Expected output

cost = 0.6930587610394646

One more strange thing from this assignment and i didn’t change the learning rate or datasets

Cost after iteration 0: 1.385354
Cost after iteration 1000: 0.000317
Cost after iteration 2000: 0.000158
Cost after iteration 3000: 0.000105
Cost after iteration 4000: 0.000079
Cost after iteration 5000: 0.000063
Cost after iteration 6000: 0.000053
Cost after iteration 7000: 0.000045
Cost after iteration 8000: 0.000040
Cost after iteration 9000: 0.000035
W1 = [[-0.65848169 1.21866811]
[-0.76204273 1.39377573]
[ 0.5792005 -1.10397703]
[ 0.76773391 -1.41477129]]
b1 = [[ 0.287592 ]
[ 0.3511264 ]
[-0.2431246 ]
[-0.35772805]]
W2 = [[-2.45566237 -3.27042274 2.00784958 3.36773273]]
b2 = [[0.20459656]]
All tests passed.

Expected output

``````Cost after iteration 0: 0.692739
Cost after iteration 1000: 0.000218
Cost after iteration 2000: 0.000107
...
Cost after iteration 8000: 0.000026
Cost after iteration 9000: 0.000023
W1 = [[-0.65848169  1.21866811]
[-0.76204273  1.39377573]
[ 0.5792005  -1.10397703]
[ 0.76773391 -1.41477129]]
b1 = [[ 0.287592  ]
[ 0.3511264 ]
[-0.2431246 ]
[-0.35772805]]
W2 = [[-2.45566237 -3.27042274  2.00784958  3.36773273]]
b2 = [[0.20459656]]
``````

I solved this issue. My cost function was wrong. After we fixed the cost function, the issue was solved.

1 Like

Thank you for your reply, i’ve looked into cost function and found out that i forgot about second part of the equation

1 Like

I have the exact same screen results. Following exactly the suggestion in the comment above the cost function code. I’m just using the def function compute_cost line, which does reference the names of global parameters, but, of course, doesn’t default the values (which in code above created issues for the test function.
I have to ask how “graceyang” “solved the issue”. Is it possible the problem lies in the test function?

## cost = 0.6926858869721941 Error: Wrong output 1 Tests passed 1 Tests failed

AssertionError Traceback (most recent call last)
in
3 print("cost = " + str(compute_cost(A2, t_Y)))
4
----> 5 compute_cost_test(compute_cost)

~/work/release/W3A1/public_tests.py in compute_cost_test(target)
141 ]
142
→ 143 single_test(test_cases, target)
144
145 def backward_propagation_test(target):

~/work/release/W3A1/test_utils.py in single_test(test_cases, target)
129 print(’\033[91m’, len(test_cases) - success, " Tests failed")
130 raise AssertionError(
→ 131 “Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))
132
133

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

# Thanks!

update: i neglected to use complete formula for “logprobs”. It’s working now!