W3 assignment submission error and assertion error


I received the following response after attempting to submit MLS W3 final assignment:

“Cell #14. Can’t compile the student’s code. Error: NameError(“name ‘z_wb’ is not defined”)”. I previously had an error message that included ‘z_wb’, which I seems to be solved now, but I’m still getting the same Cell #14 error message when I submit my assignment.

Separately, I have assertion errors. Based on the threads, it seems that it relates to indentation, three error message appears in the subsequent boxes after my coding.

Please can you advise?
Thank you


AssertionError Traceback (most recent call last)
—> 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)
—> 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.0014534636631983945


AssertionError Traceback (most recent call last)
—> 11 predict_test(predict)

~/work/public_tests.py in predict_test(target)
69 raise ValueError(“Did you apply the sigmoid before applying the threshold?”)
70 assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"
—> 71 assert np.allclose(result, expected_1), f"Wrong output: Expected : {expected_1} got: {result}"
73 b = -1.7

AssertionError: Wrong output: Expected : [1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 0.0, 1.0] got: [1. 1. 1. 1. 1. 1. 1. 1.]


AssertionError Traceback (most recent call last)
—> 13 compute_gradient_reg_test(compute_gradient_reg)

~/work/public_tests.py in compute_gradient_reg_test(target)
122 dj_db, dj_dw = target(X, y, w, b, lambda_)
→ 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}"

AssertionError: Wrong dj_db. Expected: -0.1506447567869257 got: -0.07142857142857142

Hello @R2004W,

Since writing working code is part of the assignment, code debugging is also part of it. What we can do is to give you some tips so that you can inspect your code, find the bug, and try to fix it.

Let’s analyze the error messages.

  1. You still have problem with z_wb in cell #14 even though you had put some effort in it previously, otherwise, the message would not have appeared and you would not need to share it with us. First, identify (code) cell #14. Then, in the cell, check if z_wb is explicitly used OR used in one of the functions that is called AND defined by you. Think whether z_wb should be used there, and if it should be, make sure it is defined before it’s being used. I know you must think you had defined it, otherwise, you would have fixed it already. The error message doesn’t lie, so you will have to check very very carefully.

  2. C6 depends on C3, so I suggest you to fix C3 and C4 first, then try the test for C6 again before starting to look for any bug.

  3. I added this point because you suspect it to be related to indentations. There may or may not be indenation problems, but it is worth having a look at it. First, if you want, you might spend 10 minutes on this post to get more familiar with how to indent. Then read your code and make sure each and every line is indented according to your understanding of the equations that the functions are implemented for. For example, if there are 2 loops in one of your function, where one is nested inside another, then you should ask yourself, did you place each line of your code in the correct loop? Did you put any outer loop code in the inner loop or any inner code in the outer loop? This can happen if you miss or mis-add an indentation. Try to reason about every line with yourself. This checking takes less time than what I am going to propose next.

  4. The error messages in C3, C4 and C6 all indicate that your functions cannot return correct results. This means you were not implementing the functions exactly according to the exercise’s description. Now, it is our own job to understand the description and implement the required function in the correct way, so we must know what each line of our code should produce. I suggest you to spend around 30 minutes to go through this post. The post is about where to add “printing lines” in between your codes to keep track of the progress of your function. You will NEED TO create a new and temporary code cell, define some simple inputs as demonstrated in the post, call your function to be inspected in that temporary code cell, read the printed lines, reason yourself whether the printed results are as expected, locate the bug when unexpected results occur, and fix the bug based on your understanding. After you fix the bug and pass the tests, you NEED to remove the temporary code cell, before any submission trials, or the autograder might not work properly and result in failure.

@R2004W, these are some general code debugging tips I can offer to you for this and any future assignments when a function can compute some results but those results are incorrect. Since we can’t fix the bug for you anyway, I hope you will take this time as an opportunity to go through these debugging techniques, fix the problems yourself, and most importantly, be able to locate and fix any future problems as an individual machine learning practitioner. This is more than just an assignment.

I write code everyday, and I verify and debug my code everyday.

I wish you good luck and a successful new year ahead!


1 Like