Week 2 Programming Assignment Sigmoid_Derivative


Anyone able to help with where I have gone wrong. I am brand new to python.

{Moderator Edit: Solution Code Removed}

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

NameError: name ‘np’ is not defined

Please run all the above cells. You have to do that (running all the cells from top to bottom) every time you open your assignment.

PS: Sharing your code is not allowed. Next time, only share your full error.

Hi, apologies, but are you able to clarify? Was my code correct? Do i need to hit “run” for each “return”?

I did not check your code but you have to hit run (or Shift + Enter) for every cell.

Can we lock this chat to check the code? I really do not understand why I am getting the errors. I have run each cell independently for each line of code.

We cannot lock this chat but we can use the private message option to check the code. However, it is not necessary yet.

Are you running all the cells sequentially? Try Kernel → Restart and run all. Have you deleted/modified any pre-written code or cell? If so, please read this guide to get a fresh copy of your assignment.

Ok, I will get a fresh copy of the assignment. Can I send you my code in a private message? Honestly, I do not understand what I am doing wrong. I have gone back and watched videos again, however, I keep failing one test or all tests.

Right, check the previous cells to find where “np” is defined. You should find this statement:

import numpy as np

in one of the previous cells, right? If not, then it means you’ve modified the notebook in ways that you were not supposed to. In that case, you need to do the “get a clean copy” procedure that Saif linked for you.

If you do find that statement, then you need to run the cell containing that statement in order for “np” to be defined. The simplest thing is to highlight your failing test cell and then click “Cell → Run All Above”. That will run all the previous cells, including the one that imports numpy.

You need to do this every time you open the notebook, even if you had run it the last time. You lost all the “runtime” state anytime you close and reopen or do “Kernel → Restart”.

If you’re new to using Jupyter notebooks, there is a fair amount to get used to here. If you thought you were saving time by skipping the lecture titled “A Quick Intro to the Jupyter Notebooks”, perhaps that is worth another look. There is also a reading topic in Week 2 titled “Programming Assignment FAQ” that is worth a look.

I recommend you attend a Python tutorial before you continue in this course.
The course assumes you have Python skills already.

I only need to put something like return s * (1 - s) in the cell right? I don’t need to define with DS = and then the formula in the cell, right?


Hello Claire,

If you have followed @saifkhanengr instructions and still running into error for this section.
Things you need to check:

  1. If all your previous cell test have passed. if not then check the most traced back grader cell for mistakes.
  2. Also for the concern error what I can see there should 2 line codes, so I hope you use the hint mentioned before the grader cell.
    You often code this function in two steps:
  3. Set s to be the sigmoid of x. You might find your sigmoid(x) function useful.
  4. Compute 𝜎′(𝑥)=𝑠(1−𝑠)


you are getting there :slight_smile:
check the instructions before the grader cell.

Yes, there are several ways to write the python. But I think you’re missing the point of what Saif and I have been talking about: you have to correctly understand what the error message is telling you. It’s not complaining about your code yet: the error happens in running the test cell where it is defining the input that will be used to test your function. And the error is that you have not yet imported numpy as “np”, so the name “np” is undefined.

Please read my previous response again with what I just said in mind.

At the risk of overloading this thread with advice:


  • Nominally, a function should have only one return statement.
  • A function should use the local variables that are passed to it as arguments. They’re the variable names that appear in the “function” definition.
  • Indentation is vitally important in Python. It’s how blocks of code are defined.