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?
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.
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.
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.
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.
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.