Code Cell UNQ_C6: Function 'compute_gradient_reg' is incorrect

All other cells in the logistic regression assignment pass, but the grader fails UNQ_C6 as incorrect.

I pass all tests in the notebook and values match to many sig figs.

I used a fully vectorized implementation for gradient and normalization. Could this just be a rounding error?

Output from the notebook:

Calculated Output:
dj_db: 0.07138288792343654
First few elements of regularized dj_dw:
[-0.010386028450548698, 0.011409852883280117, 0.0536273463274574, 0.003140278267313467]
All tests passed!

Expected Output:
dj_db:0.07138288792343656
First few elements of regularized dj_dw:
[[-0.010386028450548701], [0.01140985288328012], [0.0536273463274574], [0.003140278267313462]]

I doubt it’s a rounding issue.
Note that the expected output has an additional set of square brackets.

So… the expected square brackets means I want a column vector, I believe.

For the test case posted above, my dj_dw matrix shape, both before and after regularization, is (27,) which I’d think is correct. Data type is <class ‘numpy.ndarray’>

I’m newish to python… not sure why the (27,) is showing output like a row vector.

Can I beg an additional hint?

Thanks,

T.

One other note, I just realized there’s a structure change between the expected results for non-normalized and normalized gradients:

Expected output for non-normalized gradient test: (looks like a row vector)
[-12.00921658929115, -11.262842205513591]

Expected output for normalized gradient test: (looks like column vector)
[[-0.010386028450548701], [0.01140985288328012], [0.0536273463274574], [0.003140278267313462]]

Why would these be different? It doesn’t seem like normalizing should change the shape.

Thanks,

T.

1 Like

Problem was resolved.

I am still facing the same issue

Hello @Spurthi_Dantu,

Please try to go through again the description of the exercise because it is pretty complete in describing the steps of the code, then you can check if your code works correspondingly.

If you still have a problem, take a look at the hints provided below the exercise code cell. The hints is hidden inside a green button so you can click the button to find out what is inside. It usually comes with descriptions of what to code and sometimes a skeleton of the code. Read the content of the hints carefully and compare it with your code. Sometimes there would be further hints hidden in smaller blue buttons, check them out as well when needed, compare with your code.

Understanding your code and make sure your code complies with the what is required in the exercise description is very important to avoid test failures, avoid errors, and for developing hands-on experience so as to help you finish and debug future assignments.

Lastly, indent your code properly. I usually see that learners’ code are almost perfect but just because of one extra, missing, or wrong indentation, it resulted in error, or it ran through without any error but not behaved as expected. You may check out this on how to ident correctly and efficiently, and make sure your code complies with the rules as well.

It might takes time to find out a problem, but being able to debug your own code is by itself a very good experience for your future work. However, if you have dug your head into it for a long time, I find it helpful to take a coffee break or do something else before returning to it, then do what I suggested in above. It is usually easier to figure out what is wrong in your code after a break.

Good luck.

Raymond