(Python Basics with Numpy (optional assignment)) Exercise2

Kindly help me understand the reason behind error.

code:
{moderator edit - solution code removed}

print("basic_sigmoid(1) = " + str(basic_sigmoid(1)))

basic_sigmoid_test(basic_sigmoid)

output:
basic_sigmoid(1) = 2.718281828459045
Error: Wrong output.
1 Tests passed
1 Tests failed

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

Apologies if I’m breaking any rules.

That can’t possibly be the code you are actually running. Note that the output of sigmoid should be between 0 and 1, right?

I’d say your code looks correct, but notice that your answer for basic_sigmoid(1) is e, right? So what I bet happened is that you typed the new code, but did not click “Shift - Enter” on the basic_sigmoid cell. Unless you do that, just typing the new code does nothing. If you simply call the function again, you are running the old code, which probably was this:

s = math.exp(x)

2 Likes

Thanks! Works alright now. I wasn’t aware of the fact that I’m supposed to click “shift+enter” on the cell after every change.

1 Like

Don’t feel bad about that: the purpose of this first assignment is to get used to how the notebooks work as well as to get introduced to numpy. So keep that in mind as you go through the rest of the course. It’s easy to see the effect I described once you know about it: you can do a little experiment to prove to yourself how it works. Change the code by adding a factor of 2 and then run the test again without clicking “Shift-Enter” and nothing changes, right? Now click “Shift-Enter” and call it again.

The other way to make things consistent is “Kernel → Restart and Clear Output” followed by “Cell → Run All”.

Also note that every time you close and reopen the notebook, you have to run all the cells again. The output may be sitting there but the runtime version of the code is not loaded.

2 Likes

Grateful for the tips and help. Thanks once again!

Glad to hear you found the solution. The point is that it’s not just that you need to execute the cell: the code also needs to be correct. You also need to be sure to run all the previous cells by doing “Cell → Run All Above” so that all the referenced support and test functions are imported for use.

I am facing similar issue,

basic_sigmoid(1) = 2.718281828459045
Error: Wrong output.
1 Tests passed
1 Tests failed

AssertionError Traceback (most recent call last)
in
1 print("basic_sigmoid(1) = " + str(basic_sigmoid(1)))
2
----> 3 basic_sigmoid_test(basic_sigmoid)

~/work/release/W2A1/public_tests.py in basic_sigmoid_test(target)
21 ]
22
—> 23 test(test_cases, target)
24
25 ### ex 3

~/work/release/W2A1/test_utils.py in test(test_cases, target)
24 print(‘\033[92m’, success," Tests passed")
25 print(‘\033[91m’, len(test_cases) - success, " Tests failed")
—> 26 raise AssertionError(“Not all tests were passed for {}. Check your equations and avoid using global variables inside the function.”.format(target.name))

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

As suggested, I have run cell containing below code first (I have used Run command, upon getting error mentioned above, I cleared the output by running
“Restart kernal and clear all output” than run individual cells, but I am getting same error.

{moderator edit - solution code removed}


later I have run the below cell

print("basic_sigmoid(1) = " + str(basic_sigmoid(1)))

basic_sigmoid_test(basic_sigmoid)

Your code is incorrect. What you have implemented as sigmoid(x) is just e^x. But the formula is a bit more complicated than that, right? When they said to use math.exp, they didn’t mean that was all you needed. That’s just the way you implement the exponential function. Please have another look at the formula they show that is the definition of the sigmoid function.

1 Like

Thank you for the help, I am able correct my code able to submit the same

I’m meeting the same issue, I have below code:

{moderator edit - solution code removed}

But still got the error of 1 test passed 1 test failed…

I missed a negative sign… all good!

1 Like

That’s good news.

A reminder: Please don’t post your code on the forum. Not allowed by the Code of Conduct.