W2_A1_Ex-2_basic_sigmoid_function

Please i don’t understand what i am supposed to do on this exercise 2. Can someone please put me through. Thank you

To refer to a function belonging to a specific package you could call it using package_name.function(). Run the code below to see an example with math.exp().

import math
from public_tests import *

GRADED FUNCTION: basic_sigmoid


def basic_sigmoid(x):
“”"
Compute sigmoid of x.

Arguments:
x – A scalar

Return:
s – sigmoid(x)
“”"
# (≈ 1 line of code)
# s =
# YOUR CODE STARTS HERE

# YOUR CODE ENDS HERE

return s

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

basic_sigmoid_test(basic_sigmoid)

When i ran the code, it showed me the following:

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

NameError: name ‘basic_sigmoid’ is not defined

Please what may I do?

Hi @Chukwu_Nnaemeka_Paul ,

You are supposed to write the code for this function using the functions from Math library. As you can see from the first line of this cell, there is this line:

import Math

The definition of this function begins with:

def basic_sigmoid(x):

and the code to write is within the starts and ends lines:

Your code starts here
Your code ends here

and the math function to use is:
math.exp() for the exponent part of the equation
sigmoid(x) = \frac{1}{1+e^{-x}}

Please read the implementation instruction carefully. It explains clearly what your function should do.

That means you have not run the cell that defines the basic_sigmoid function or perhaps you ran it, but it threw an error because the code is incomplete or incorrect. Part of what we are learning in this “optional” tutorial assignment is how to work the Jupyter Notebooks. Please make sure not to skip the video about the notebooks in Week 2, which walks you through how they work. Also there’s the “Programming FAQ” reading item. There is also a topic on the DLS FAQ Thread about the ‘name not defined’ error in general.

I tried correcting the error i made, yet its not giving me the correct output. I need help please. Here’s what i got below;

{moderator edit - solution code removed}

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

basic_sigmoid_test(basic_sigmoid)

when I run it, I got the following;

basic_sigmoid(1) = 3.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 def sigmoid_test(target):

~/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.

Please i need more explanations

You have hard-coded the parameter to be 1, so you only get the correct answer for that particular case. The point is that you need to use the parameter x, so that your code works for any input value.

Also note that your implementation of the formula for sigmoid is not quite complete. Please compare the code you wrote to the math formula shown in the instructions.

Also note that you have “order of operations” problems with your implementation. Try running the following and watch what happens:

a = 5.
b = 1. / 1. + a
c = 1. / (1. + a)
print(f"b = {b}")
print(f"c = {c}")

Here’s what you see:

b = 6.0
c = 0.16666666666666666

Here’s another example just involving multiply and divide:

a = 5.
b = 1. / 2. * a
c = 1. / (2. * a)
print(f"b = {b}")
print(f"c = {c}")

Here’s the result of running that:

b = 2.5
c = 0.1
1 Like