C1_W2_Linear_Regression - Programming Assignment Week 2 - "compute_cost not defined" error on submission

Hi everyone,

I’m stuck on the Week 2 practice lab for Linear Regression and hoping someone can help!

The Problem: Everything runs perfectly when I execute the lab in Jupyter - all cells work, plots display correctly, and my compute_cost function produces the expected output. However, when I submit the assignment, I consistently get this error:

Cell #4. Can't compile the student's code. 
Error: NameError("name 'compute_cost' is not defined")

What I’ve Tried:

  • Double-checked that my compute_cost function is in the correct cell
  • Made sure I’m not missing any parentheses or indentation
  • Verified the function signature matches exactly: def compute_cost(x, y, w, b):
  • Ran all cells from top to bottom before submitting
  • Cleared outputs and re-ran everything

The Function Structure: My compute_cost function is implemented in the designated cell and follows the template provided. It calculates the cost using the formula from the lectures and returns a scalar value.

Question: Has anyone else encountered this “not defined” error specifically on submission when the code runs fine locally? Is there something about the grader environment that I might be missing?

I’m wondering if it’s:

  • A cell execution order issue in the grader?
  • Something with variable scope that works in Jupyter but not in the submission environment?
  • A syntax issue that Jupyter is forgiving about but the grader isn’t?

Any insights would be greatly appreciated! I’ve spent hours on this and can’t figure out what’s different between my local execution and the submission environment.

Thanks in advance!

Additional Context:

  • Using the provided Coursera lab environment
  • All previous assignments submitted successfully
  • Function works correctly when tested with the provided test cases

Has anyone experienced similar submission vs. local execution discrepancies in the programming assignments?

Typically this type of error in the grader occurs if you’ve modified the notebook file in some way that breaks the grader compatibility.

Any of these can cause problems:

  • Moving a cell
  • Deleting a cell
  • Adding a cell
  • Editing a cell that you should not have touched.
  • Modifying a cell outside of the area marked for your code.
  • Deleting the comment lines or metadata that the grader uses.
  • Renaming the notebook.

Do any of these apply to your situation?

Also note that if you work on the notebook outside of Coursera, then upload it back to Coursera for grading, mayhem is sure to happen within the grader.

The only way I can get the lab to work is to run the def compute_cost before it is called in the code above. However it seems that the autograder is running the code top to bottom and when it is called the function is not defined. I have submitted the assignment in multiple ways and even if it works for me I always fail.




Hi,

Is the cell containing def compute_cost located below the cell where the function is called, for some reason (cf. image)?

When you execute all the code cells from the top to bottom, your def compute_cost cell should have the execution number [7] and the first cell calling it [8]. If it is not the case, something seems wrong with your file structure.

That’s not how the grader works.

It extracts the graded functions from your notebook, and runs them in a separate environment,with different tests.

The only way the grader will not be able to locate a function is of the notebook has been modified in a way that erases the metadata keys it is looking for.

I recommend you get a new copy of the notebook and start over.

Use the File->Open menu, and rename your notebook.
Then use the “Lab Help” tool, and “Get latest version”.
Then exit the assignment and re-enter it.

The “Lab Help” tool is the “question-mark inside a circle”, in the upper right corner of your browser.

Enter your code by hand, do not copy it over from the old notebook.

1 Like

Also note:
Every time you open a notebook, you must run all of the cells starting from the top. That’s how the assets are imported and the workspace is configured.

Do not add any extra “import” statements to the notebook.

Only add your code in the marked places inside the graded functions.

This is why it is vital that cells not be moved, deleted, or added within the notebook.

1 Like

One more thought:

Is it vital that you do not modify the file name for any notebook you want to grade.
The grader always uses the file with the original notebook name. It does not matter what notebook is open when you click the “Submit” button.

You can keep renamed copies of a notebook for your own reference, but you cannot submit them for grading.

Yes. That is the problem. It’s called in the 4th Cell but not defined until the 8th Cell. It only runs for me if I run the 8th cell before the 4th Cell but the autograder seems unable to do this. Please can someone help me.


I didn’t realise you can change the order of the cells. I did this and moved the def compute_cost before it was called and it worked. This should be more obvious.

1 Like

I recommend you do not change the order of the cells.
It is not necessary.
If it worked for you, it is because it compensated for some other issue.

Compute_cost() is not called in the 4th cell.
Here is the 4th cell:

In the original notebook, the compute_cost function is defined in the 7th cell, and tested in the 8th cell.

So re-arranging the cells in your notebook should not be necessary.

1 Like