# UNQ_C2 # GRADED FUNCTION: compute_gradient

Hi, guys I am encountering a problem and need help. can someone help?

Hello @Ch_Venkatesh66,

The error means that your exercise wasn’t implemented correctly so it could not compute the correct dj_db value for a test case.

You can click “File” > “Open” > “public_tests.py” to check out the details of the test case, but I can share it with you that it is using these parameters

    x = np.array([2, 4, 6, 8]).T
    y = np.array([4.5, 8.5, 12.5, 16.5]).T
    initial_w = 2.
    initial_b = 0.5

If you mentally do y = wx+b on each samples, you will find that the given w and b are perfect parameter values that will predict every x correctly, so dj_db and dj_dw should both be zeros. From the error message, your dj_db is not zero.

You will come to the same conclusion with the following set of equations that you need to implement for this exercise:


So, please check your code again for where the problem is. You may even take one pair of (x, y) such as (2, 4.5) and mentally run it with your code, and hopfully you will spot the problem when some results don’t look right.


1 Like

Thank you very much @rmwkwok for your quick response. please look and correct me if I did any wrong.

{code removed by mentor }

Hello @Ch_Venkatesh66,

You didn’t implement it as the equations ask you to. The equations sum over the samples first, and only after all the samples are summed over, they divide the sums by the number of samples. Did you wait until all the samples are summed over before dividing by m?


PS: We can’t share assignment code here so I have removed it for you.

So, I should run the code, and then I should run divide, right? And UNQ_C2 is running fine there is no error.

the part of code that sums the loss gradient will run first, and then the part that divide by m will run next. This is what the equations say. The summation is evaluated first, and then the division.

Thank you. You just want me to change the lines, right?

I want you to know what every line of your code does. I want you to know what to include in the loop and why. I want you to know what not to include in the loop and why.

Lastly, I want this to be your and your own success. So figure out how to correct it yourself :slight_smile: There should already be enough hint!


1 Like