Compute_gradient code in course 1 week 3 doesn't give the expected values

Could you please help me with this code. I really don’t know, why it doesn’t lead to the expected results.

Compute and display cost and gradient with non-zero w

test_w = np.array([ 0.2, -0.5])
test_b = -24
dj_db, dj_dw = compute_gradient(X_train, y_train, test_w, test_b)
this leads to:
AssertionError Traceback (most recent call last)
in
8
9 # UNIT TESTS
—> 10 compute_gradient_test(compute_gradient)

~/work/public_tests.py in compute_gradient_test(target)
51 dj_db, dj_dw = target(X, y, test_w, test_b)
52
—> 53 assert np.isclose(dj_db, 0.28936094), f"Wrong value for dj_db. Expected: {0.28936094} got: {dj_db}"
54 assert dj_dw.shape == test_w.shape, f"Wrong shape for dj_dw. Expected: {test_w.shape} got: {dj_dw.shape}"
55 assert np.allclose(dj_dw, [-0.11999166, 0.41498775, -0.71968405]), f"Wrong values for dj_dw. Got: {dj_dw}"

AssertionError: Wrong value for dj_db. Expected: 0.28936094 got: 0.2741061634878938
print(‘dj_db at test_w:’, dj_db)
print(‘dj_dw at test_w:’, dj_dw.tolist())

UNIT TESTS

compute_gradient_test(compute_gradient)

thanks a lot for help!

Hello Stefanie,

The error message says your code doesn’t compute the answer correctly so there are some bugs. I have to make the same suggestions as in this topic so please take a look, and hopefully with them you will find some necessary variables are never used in your work. Those suggestions are useful in general for the rest of this specialization to help debugging your work.

Good luck with the assignments!

Cheers,
Raymond

PS: I will edit your post since sharing assignment code isn’t allowed here. Posting the error traceback is sufficient.

Hi Raymond,

thanks for you reminder concering the sharing of the code! Sorry! - And thank you for the support. I try to figure out, what’s exactly the problem in my code.
By the way, is there a possibility to have a special session, where I can discuss with a professional and get some help? It is really hard as a complete beginner

best
Stefanie

Hi @Stefanie_Steigele ,
We dont have a special session to attend for the learners. On the other side, be sure that we will be happy to try to answer any question that you post here. Just please continue to learn the lessons and don’t hesitate to post any doubts here and we will try to answer as quickly as possible.
Keep learning!

Hi Stefanie, as far as I know we don’t offer discussion session, and I have never seen one either. Personally I think this is not easy considering the very large number of learners there are.

It takes time to learn and it takes time to figure out how to learn effectively in an online and self-paced setting, but I believe this process is rewarding in the long run because machine learning is developing so fast that we need to rely on our own ability to stay updated.

The suggestions I made is how I debug as a learner and in work, and I believe they will benefit in your own work in the future.

The work you had posted was pretty close to perfect. It’s your effort and it’s the last mile to some achievement in your early machine learning journey. :wink:

Sorry that these may not be what you are asking for but if you have questions about the concept in machine learning, please do ask them in this community. It would be even better if you share your own thoughts about the question so we mentors and our fellow learners can align their answers better to your questions.

Cheers,
Raymond

1 Like

thanks for your tipps. I try again and I struggle with the compute_cost. For initial_w = np.zeros(n) and initial_b = 0 the expected value of 0.693 is delivered, but with test_w = np.array([0.2, 0.2])
test_b = -24 a not expected value is delivered. But the “info” said “All tests passed”.
But I need this cost function for the following tasks…may you give me an additional tipp?
best Stefanie

Hi @Stefanie_Steigele,

Is your result for the test_w = np.array([0.2,0.2]) case close to the expected value of 0.218? If so, no need to worry about it. The difference is likely due to a rounding variation that could occur if your implementation is slightly different than the code used to generate the 0.218.

The “All tests passed.” message you’re seeing is printed by the function compute_cost_test(). That function tests several cases, so the fact that all of those cases pass with your code is a good sign.

You can take a look at the code in compute_cost_test() by choosing “Open” from the File menu and then opening public_tests.py. If you look at the compute_cost_test() there, you’ll notice that it checks if the result is close to the expected value, not exactly equal.