C1W3: Weird behavior of Grader: Failed test case: Exception during compute_cost test: name 'math' is not defined

I have clearly imported math in the first cell of my Jupyter notebook (refer screenshot). Later in the notebook I call math.log. This works fine when running the notebook. However, once I save and submit the notebook to the grader, it errors out on the cells which use math.log with the error:

Failed test case: Exception during compute_cost test: name 'math' is not defined.

I fixed it by importing math in that specific cell, and that makes the grader stop complaining, but it’s odd that the initial import of math didn’t work.

Interestingly, the same failure occurs on two cells, but importing math in just one cell (i.e. the one for exercise 2) fixed the problem in the next exercise too.

I made sure to run my entire notebook and save it before submitting.

Anyway since my problem is solved, I’m good, but the DL.ai staff might want to take a look at this error.

Has anyone else seen this?

The grader doesn’t use your whole notebook.
It only extracts the graded cells, and then runs your code in a separate environment.

The grader does not expect you to use the math library. So it does not import it.

Try changing your solution so it doesn’t use math.log.

Hint: The assignment expects you to use numpy.

I fixed it by importing math in that specific cell,

Please do not do this.

For the future I’ll remember to not import libraries other than what’s specified in the notebook, but it might be good to specify that - in giant letters - in the notebook too.

importing math is not working for me, I am not even using math anywhere.

Please let us know which assignment in which course you are talking about here. If it’s the MLS C1 W3 assignment, note in the previous discussion on this thread that you shouldn’t be using math at all. It is not imported by the given template code in the notebook and there is no need for you to add an import statement.

So I’m guessing you are probably talking about a different assignment. Without knowing which one, it’s a bit hard for us to offer concrete advice. As discussed above, the intent is for you to be using numpy functions, since they support vectors and matrices which is most of the data that we are using in ML. numpy functions also handle scalar inputs as well.

Also as a general matter, note that it is very rare in any of the assignments in these courses to need to add import statements yourself. In all the cases I can think of, the template code that they give you will contain the import statements for all the packages that you need to use in a given assignment. If you are getting a “function not defined” error message, it usually just means you forgot to run the earlier cells in a notebook that contain the given import statements. Try Cell -> Run All Above and then run the previously failing cell again and see if that helps.