General code debugging tips

Hello @Pramil.Gupta,

It is the first assignment of this specialization, so it is a good time for you to practice how to debug your code. Let’s look at the error traceback. It says you have this error message

image

And it pointed to this line

image

This indicates that the error was raised when your exercise work was being checked by the public test. To check out the content of the public test, you might click “File” > “Open” > “public_tests.py”. Then in the file, search for “Cost must be 2” which is part of the error message. Then you should find this test:

    # Case 2
    x = np.array([2, 4, 6, 8]).T
    y = np.array([7, 11, 15, 19]).T
    initial_w = np.array([2.])
    initial_b = 1.0
    cost = target(x, y, initial_w, initial_b)
    assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"

Here you can see that what x, y, w, and b are used for testing your work. If you review the cost function and calculate the cost with these parameter values, you should also be able to come up with this: \frac{1}{4}[(2\times2+1-7)^2 + (2\times4+1-11)^2 + (2\times6+1-15)^2 + (2\times8+1-19)^2]=\frac{1}{4}(4+4+4+4) = 2

However, your function computes a cost of 0.5. Now, there are 2 ways to inspect your work:

  1. read the code. This is easier, but you need a very clear mind and ask youself why each of your lines are written the way they are. For example, one common error is wrong indentation. You need to indent correctly to make looping code lines to be in a loop. Check this post for more about indentation.

  2. print your function’s progress. This topic is a demo of how to add print lines to print out all intermediate variables for you to keep track of how the numbers change. You need to read the printouts and ask yourself whether each printed numbers are up to your expectation. The code is written by you and it should work up to your expectation.

Good luck!
Raymond