Error in Week 2 Practice Lab

Please have a look at the screenshot. Though I have got the expected output, it’s showing an assertion error. Can anyone please tell me what exactly is happening and how can I proceed with this?

The problem is that your code in compute_cost() works correctly for one of the tests that is provided in the notebook, but it does not work correctly for the test that the grader uses.

The grader uses entirely different tests, and a different workspace.

Check your code in compute_cost(), and see if (for example) you have anything that uses global variables, or makes assumptions about the size of the data set.

@Ujjval,

The error you are getting from the unit tests in the assignment is different than the error you’re getting from the grader. As @TMosh says, the grader’s tests are different than the assignment’s unit tests, but it is a good first step to get all the unit tests working first.

The error you are seeing from the unit test is a good hint for you about where to look. It says that the cost for a perfect prediction should be 0, but your compute_cost() did not return 0. Look at your code to see what could be the problem. One thing to check - make sure that your code uses the parameters passed into it: (x, y, w, b) and does not hard-code any values like x_train or y_train.

If you need to dig in deeper and look at the code for this test, or any of the other unit tests, you can click on the File/Open menu item and then open public_tests.py to see exactly what the tests are passing in to your function to help you narrow down the problem.

@TMosh @Wendy ,

Thank you for sharing the information. I am very new to this and Python too, just only knew few basic things. I think the code:

#Public tests

from public_tests import *
compute_cost_test(compute_cost)

from the above screenshot is to check whether I got the right value of J_wb (Cost). If what I am thinking is true I guess there can be a much simpler way to check it rather than writing this whole function down below in the public_tests.py file.

There are 5 different cases in function “compute_cost_test” as shown in the screenshot below and I think case#2 is what it should check against my answer since w,b values are same as in the Practice Lab(graded test). Those values are fixed and we can’t change so that we can get a matching value of cost, fair enough.

I know I am wrong but what are those predefined x = np.array([2, 4, 6, 8]).T and y = np.array([7, 11, 15, 19]).T ? wait….while we calculate cost we need x_train and y_train right? so what’s with these x and y values? Don’t we use x as input when we have to make prediction but here we are just calculating the mean squared error. Should I comment these two values of x and y? and put x_train and y_train instead?

Please let me know what I am doing wrong here.

I recommend you not modify anything in the tests, and leave off from modifying public_tests.py.

Fix your code in compute_cost() so that it works correctly for any of the test cases.

1 Like

I didn’t remember what actually fixed the error but somehow it’s working fine now. All tests passed!

@Ujjval

Congratulations!

One key thing to remember: whenever you are implementing a function, you should be using the parameters passed into the function and should not hard-code values from outside the function. This is good to keep in mind as you work on future labs.

In this case, that means using the x and y passed into compute_cost and not hard-coding your function to use x_train and y_train. The general idea of the compute_cost() function is that it uses the training examples passed into it (x and y) to help it determine how close w and b are to matching those known values (ie the cost for that w and b). This result is used in training to help slowly adjusting the values of w and b to get a lower and lower cost.

The unit tests try different example training data (different x’s and y’s) to help check that your function works in different scenarios.

I hope this helps clarify a bit, so it’s not quite such a mystery.

1 Like