Python Basics with Numpy: Exercise 4: Sigmoid Derivative

I wrote these 2 lines of code for exercise 4 and I’m getting an error message. I’m not sure why. I would realy appreciate the help. Thank you.


TypeError Traceback (most recent call last)
1 t_x = np.array([1, 2, 3])
----> 2 print ("sigmoid_derivative(t_x) = " + str(sigmoid_derivative(t_x)))
4 sigmoid_derivative_test(sigmoid_derivative)

in sigmoid_derivative(x)
19 s=1/(1+np.exp(-x))
—> 20 ds=s(1-s)

TypeError: ‘numpy.ndarray’ object is not callable

1 Like

Python syntax is not the same as math notation. If you say this in python:

s(1 - s)

What that means is that s is a function and you are calling it with the argument 1 - s. Of course if s is not actually a function, then it throws an error. If what you intended is multiplication, the way to express that in python is this:

s * (1 - s)


Dear KahmilX abd Paulinpaloalto,
regarding these two lines for ex4 of assignment 1:
What is the purpose of the second line in ex4? The function only returns the sigmoid of x, no ds?

@anantha: the code you show has the same bug that I explained in my previous post. So your ds line will throw an error. But the point of this function is to return the derivative of sigmoid, not sigmoid itself. So the return value should be only ds. The purpose of the ds line of code is to take the sigmoid value and compute the derivative from it.

Also note that there is no need to rewrite the implementation of sigmoid within this function: you already wrote a function that computes that, so why not just call it?

1 Like

I am unable to change anything in jupyter lab can you help me out please?

Hello, Chegond.

Welcome to the community.

What course and assignment are you talking about? Also, please share a screenshot of the error log that the cells are receiving as an error.

Note that in all the notebooks only some of the code cells are modifiable: the cells containing the test cases that check your code are not modifiable. But you should be able to modify the functions that you are supposed to be implementing. You’ll see the sections marked “YOUR CODE HERE”.

If the test fails, the solution is not to change the test, but to figure out what is wrong with your code that causes the test to fail.