# (Python Basics with Numpy (optional assignment)) Exercise2

**URL:** <https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [January 15, 2022, 11:19pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137 "2022-01-15T23:19:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yash\_Varshney](https://avatars.discourse-cdn.com/v4/letter/y/a87d85/32.png) [@Yash\_Varshney](https://community.deeplearning.ai/u/Yash_Varshney)\
**Post date:** [January 15, 2022, 11:19pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/1 "2022-01-15T23:19:01Z")

</div>

_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._

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [January 16, 2022, 12:52am UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/2 "2022-01-16T00:52:25Z")

</div>

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)`

---

<div class="post-metadata">

**Author:** ![Yash\_Varshney](https://avatars.discourse-cdn.com/v4/letter/y/a87d85/32.png) [@Yash\_Varshney](https://community.deeplearning.ai/u/Yash_Varshney)\
**Post date:** [January 16, 2022, 8:12am UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/3 "2022-01-16T08:12:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [January 16, 2022, 4:00pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/4 "2022-01-16T16:00:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Yash\_Varshney](https://avatars.discourse-cdn.com/v4/letter/y/a87d85/32.png) [@Yash\_Varshney](https://community.deeplearning.ai/u/Yash_Varshney)\
**Post date:** [January 16, 2022, 9:52pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/5 "2022-01-16T21:52:01Z")

</div>

Grateful for the tips and help. Thanks once again!

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [June 16, 2022, 8:42pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/7 "2022-06-16T20:42:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ssainu](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@ssainu](https://community.deeplearning.ai/u/ssainu)\
**Post date:** [June 14, 2024, 8:34am UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/8 "2024-06-14T08:34:27Z")

</div>

## 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)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [June 14, 2024, 2:11pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/9 "2024-06-14T14:11:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ssainu](https://avatars.discourse-cdn.com/v4/letter/s/258eb7/32.png) [@ssainu](https://community.deeplearning.ai/u/ssainu)\
**Post date:** [June 16, 2024, 11:28am UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/10 "2024-06-16T11:28:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Rachel\_Zenger](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rachel\_Zenger](https://community.deeplearning.ai/u/Rachel_Zenger)\
**Post date:** [June 27, 2024, 6:24pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/11 "2024-06-27T18:24:29Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Rachel\_Zenger](https://avatars.discourse-cdn.com/v4/letter/r/57b2e6/32.png) [@Rachel\_Zenger](https://community.deeplearning.ai/u/Rachel_Zenger)\
**Post date:** [June 27, 2024, 6:25pm UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/12 "2024-06-27T18:25:49Z")

</div>

I missed a negative sign… all good!

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [June 28, 2024, 12:29am UTC](https://community.deeplearning.ai/t/python-basics-with-numpy-optional-assignment-exercise2/87137/13 "2024-06-28T00:29:58Z")

</div>

That’s good news.

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