# MLS week 2 Lab Gradient descent

Hi everyone, I’ve done this code in my lab work but i don’t know where i went wrong i’m getting assertion error. Can someone please explain where am I being wrong.

1 Like

Hello @zbukhari,

The error says that your function cannot compute the result correctly, and that’s because the function isn’t implemented correctly.

Since debugging your code is part of your assignment, I can suggest you 2 methods for debugging, and I hope they will be helpful for the rest of assignments in this specialization. Both methods require your understanding of the exercise.

Also, sharing assignment code isn’t allowed, please remove it and only share the screenshot with the error message.

### Method 1: Read the code

1. These are what we want to implement:

2. If we look at the first equation, it has 3 parts:

1. \frac{\partial J(w,b)}{\partial b}^{(i)}
2. \sum\limits_{i = 0}^{m-1}
3. \frac{1}{m}
3. The idea is we calculate (2.1) for each sample, then sum over all samples (2.2), and then divide the resulting sum by m once (2.3)

4. Following this idea, you read your code again to see if your code is doing exactly that.

### Method 2: Examine the progress of your code

1. Add printing lines in between your codes to print all intermediate variables you have assigned values to, for example,

Source: General code debugging tips

2. Run your function with some simple inputs. For detailed instructions, please check this post out.

3. You check if the printed outputs are up to your expectation, if one number is unexpected, then there is a problem.

Cheers,
Raymond

2 Likes

hi raymond
i followed all these steps but still i’m unable to find my mistake here is the output:

Hello @zbukhari !

1. My steps require that you create some simple inputs to test your function, what inputs did you use?

2. Did you verify that all the printed numbers are as your expectation? This is the crucial step to make sure you code the exercise correctly.

3. After you used the simple inputs to test your function, and verified the printed numbers, can you please share the outputs from the beginning? I will also take a look at your printed outputs and may ask you some questions about them.

Thank you,
Raymond

1 Like

I executed the code as per instructions and this is the output
I can share my code if you want me to.
1 m 3
2 i 0
3 f_wb 3
4 dj_dw_i 1
5 dj_db_i 1
6 dj_db 1
6 dj_dw 1
7 dj_dw 0.3333333333333333
7 dj_db 0.3333333333333333
8 dj_dw, dj_db 0.3333333333333333 0.3333333333333333
2 i 1
3 f_wb 5
4 dj_dw_i 8
5 dj_db_i 4
6 dj_db 4.333333333333333
6 dj_dw 8.333333333333334
7 dj_dw 2.777777777777778
7 dj_db 1.4444444444444444
8 dj_dw, dj_db 2.777777777777778 1.4444444444444444
2 i 2
3 f_wb 9
4 dj_dw_i 24
5 dj_db_i 6
6 dj_db 7.444444444444445
6 dj_dw 26.77777777777778
7 dj_dw 8.925925925925926
7 dj_db 2.4814814814814814
8 dj_dw, dj_db 8.925925925925926 2.4814814814814814
Gradient at initial w, b (zeros): 8.925925925925926 2.4814814814814814
Using X with shape (4, 1)
1 m 3
2 i 0
3 f_wb 3
4 dj_dw_i 1
5 dj_db_i 1
6 dj_db 1
6 dj_dw 1
7 dj_dw 0.3333333333333333
7 dj_db 0.3333333333333333
8 dj_dw, dj_db 0.3333333333333333 0.3333333333333333
2 i 1
3 f_wb 5
4 dj_dw_i 8
5 dj_db_i 4
6 dj_db 4.333333333333333
6 dj_dw 8.333333333333334
7 dj_dw 2.777777777777778
7 dj_db 1.4444444444444444
8 dj_dw, dj_db 2.777777777777778 1.4444444444444444
2 i 2
3 f_wb 9
4 dj_dw_i 24
5 dj_db_i 6
6 dj_db 7.444444444444445
6 dj_dw 26.77777777777778
7 dj_dw 8.925925925925926
7 dj_db 2.4814814814814814
8 dj_dw, dj_db 8.925925925925926 2.4814814814814814

AssertionError Traceback (most recent call last)
in
6 print(‘Gradient at initial w, b (zeros):’, tmp_dj_dw, tmp_dj_db)
7

50 dj_dw, dj_db = target(x, y, initial_w, initial_b)
51 #assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
—> 52 assert dj_db == 0.0, f"Case 1: dj_db is wrong: {dj_db} != 0.0"
53 assert np.allclose(dj_dw, 0), f"Case 1: dj_dw is wrong: {dj_dw} != [[0.0]]"
54

AssertionError: Case 1: dj_db is wrong: 2.4814814814814814 != 0.0

1 Like

i’m not getting expected output but i can’t figure out what is going wrong.

1. My steps require that you create some simple inputs to test your function, what inputs did you use?
2. Did you verify that all the printed numbers are as your expectation? This is the crucial step to make sure you code the exercise correctly.

Raymond

1 Like

x = np.array([1, 2, 4])
y = np.array([2, 1, 3])
w = 2
b = 1

these are the inputs i added as per your sample solution code
I verified the output but I’m not getting expected outputs there is something wrong with values of dj_dw and dj_db

It is also necessary to verify all the intermediate outputs, because bugs are in the steps and those intermediate outputs are for you to inspect the steps. Please do so.

Raymond

@zbukhari, for example, why would you get the following?

7 dj_dw 0.3333333333333333
7 dj_db 0.3333333333333333

I think if you check them with the formula, you wouldn’t expect them.

Let’s read this again:

Cheers,
Raymond

1 Like

Here I dry run my code and as per my calculations i need result of third iteration as integer numbers but as per code i’m not getting desired result.

also the error is same:
AssertionError: Case 1: dj_db is wrong: 2.4814814814814814 != 0.

what should I do now?

Hello @zbukhari,

Your handwritten calculation didn’t take care of how to add them together. I assumed you did that in mind by adding up the results from all 3 iterations.

Now, let’s look at your outputs again. I marked an output line  # <== OK if the printed result looks consistent with your handwritten calculation.

However, if you look at the line with many question marks, you probably can’t explain how that number comes up, can you? I mean, according to your calculations, how could dj_db be 4.333 and 1.444?

Let’s stick with the handwritten calculation you posted from now on. Please don’t change that.

@zbukhari, the key of this debugging step is to check each and every printed numbers and see if you can explain it, and if not, then there is a problem in your code. We CANNOT skip them but only focus on the result, because without correct steps, we can’t expect a correct result. As soon as one number is wrong, I won’t read any further, so I will not worry about the lines you highlighted in yellow now.

Raymond

this is what I’m not getting why am i getting these results as my first iteration is giving me accurate result but not the other one what is wrong with my code.

Remember it is you who instruct Python how to work, you add a line to do one thing on purpose. Please read your code and ask yourself, why do you put the line of code there that causes a question mark line.

In your handwritten calculation, you WILL NOT do that, but why would you do it in Python?

Why would you ask Python to get to some calulcation result that you will not get to on paper?

If you really run out of idea, I suggest you to remove code lines that will generate numbers that you do not expect, and after that, please share your outputs with me again, and then I will read it, and see if only numbers on your paper are showing up. Let’s forget about the final result for the time being and focus on fixing the bugs in the steps.

@zbukhari, I am sorry I need to be away for some time, so here is my suggestion:

1. You have wonderfully written down the calculation you are expecting Python

2. You have code lines that follow exactly your handwritten calculation

3. You have code lines that do not show up in your calculation. For those, I suggest you remove them. It is easy to know which line is producing an unexpected number because you added one printing line to each line of code.

4. After you remove them, and then run the code again, you will see it printing results on and only on your paper

5. then the last thing would be to think about how to add them up You didn’t write it down on your paper, so how would you do it in Python? Here is the usual trick for you to accumulate numbers up through a for-loop:

a = 0
for m in range(3):
a = a + m
print(a) # expecting 0 + 1 + 2 = 3


@zbukhari, please don’t give up and take this exercise as a chance to practice how to code and debug in Python for Machine Learning. You will keep using it throughout the course and in the future as an individual professional.

Good luck!
Raymond

thank you Raymond,
I really appreciate your patience and help i will figure it out now I’m very grateful of you to help me out and take time to reply me.

Regards,
zaynab

You are welcome Zaynab!

I agree with you that it is important to be patient Sometimes when I find a paper that I can’t understand quickly, I have to ask myself to be patient, put it aside, and read it the next day morning.

Btw, I kept saying “you” “you” and “you” in some previous replies but I didn’t mean to be pushing. In my personal experience, sometimes it is important to realize that it is we who shall make the change. I hope I didn’t bother you by too much.

Again, good luck!
Raymond

1 Like