Week 2 Practice Coding Assignment Exercise 4

Can you guys tell me what is going wrong with this code?

def sigmoid_derivative(x):
“”"
Compute the gradient (also called the slope or derivative) of the sigmoid function with respect to its input x.
You can store the output of the sigmoid function into variables and then use it to calculate the gradient.

``````Arguments:
x -- A scalar or numpy array
``````

Return:
“”"

``````#(≈ 2 lines of code)
# s =
# ds =
``````

[ Moderator edit: solution code removed as per honor code.]

``````# YOUR CODE ENDS HERE

return ds
``````

t_x = np.array([1, 2, 3]) print ("sigmoid_derivative(t_x) = " + str(sigmoid_derivative(t_x))) ​ sigmoid_derivative_test(sigmoid_derivative)

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

in sigmoid_derivative(x)
18
19 s = 1 / (1+ np.exp(-x))
—> 20 ds = s(1-s)
21
22 # YOUR CODE ENDS HERE

TypeError: ‘numpy.ndarray’ object is not callable

Hello Spencer,

hmm I must still be mis-understanding something. Isn’t that equation that I set equal to s the equation for the sigmoid of x? I also can’t find the sigmoid function that it mentions right after, if that has something to do with it.

This is a case where math notation and python are not the same. If you write this in python:

s(1 - s)

you are saying that s is a function and you are invoking (calling) the function with the argument 1 - s. If s turns out not to be a function, that will throw an error like the one you are getting.

If what you meant to do was multiply, the syntax for that in python is:

s * (1 - s)

This is not a beginning python course: you need to be a competent python programmer as a prerequisite to succeed here.

And Rashmi’s other point is that you don’t need to write out the sigmoid function again: you already built that function earlier in the assignment, so you can just call it rather than rewriting the logic.

You have right pointed out that Paul sir. I had missed that part