Week 3 Programming assignment: Exercise 6 compute_gradient_reg

Running the test, I’m getting the following assertion error:

dj_db: 0.07138288792343662
First few elements of regularized dj_dw:
[-0.010034426775559609, 0.01047627452123708, 0.055745505756197467, 0.003977852112093805]

AssertionError Traceback (most recent call last)
in
11
12 # UNIT TESTS
—> 13 compute_gradient_reg_test(compute_gradient_reg)

~/work/public_tests.py in compute_gradient_reg_test(target)
123
124 assert np.isclose(dj_db, expected1[0]), f"Wrong dj_db. Expected: {expected1[0]} got: {dj_db}"
→ 125 assert np.allclose(dj_dw, expected1[1]), f"Wrong dj_dw. Expected: {expected1[1]} got: {dj_dw}"
126
127

AssertionError: Wrong dj_dw. Expected: [ 0.19530838 -0.00632206 0.19687367 0.15741161 0.02791437] got: [0.17210345 0.00241732 0.20441898 0.17273973 0.02791437]

My code includes the following lines (as per hint given for this exercise:

Loop over the elements of w

for j in range(n): 
        dj_dw_j_reg = (lambda_ / m) * w[j]

Add the regularization term to the correspoding element of dj_dw

dj_dw[j] = dj_dw[j] + dj_dw_j_reg
    
### END CODE HERE ###         
    
return dj_db, dj_dw

It seems that dj_dw is not updated for each iteration of j. What am I missing. Any suggestions will be much appreciated.

Hello @George_Tzotzos

Can you DM your notebook to me and i can take a look.

Can you please send me the email address where I should post the notebook.

Many thanks for your help

Click on my name → click on message → attach notebook

Notebook sent. I hope the correct way. Apologies as I’m not familiar with the interface.

No. I have not recieved it @George_Tzotzos

Did you do the following: Click on my name → Click on Message → Upload your notebook → Click on Message

I did this but there’s a bug in the system, I think.

I’m uploading the notebook in this mail.

Many thanks

George

Hello @George_Tzotzos

In your compute_gradient_reg function, you have not indented the line correctly, where you are adding the regularization term to the corresponding element of dj_dw. As such, the FOR loop is having no effect on that line, while it actually should.

Thank you very much. Very grateful to you for pointing out my mistake.

George

You are most welcome @George_Tzotzos :blush: Happy Learning!!

I have the same problem. I checked my indentation is correct I checked. Can you look at my notebook too. I’m sending you the notebook please check and tell me where I did a mistake.

There was no attachment to your email. If you haven’t solved the problem, please resend.

George