C1_W2_Linear_Regression exercise 2 compute_gradient

How to do this…Can anyone help me to fix this error?

Hi @Dhivya_R
Welcome to the community!

The implementation of the gredient isn’t correct please follow these steps
first compute this term

\frac{\partial J(w,b)}{\partial b} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) - y^{(i)}) \tag{2}
\frac{\partial J(w,b)}{\partial w} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{w,b}(x^{(i)}) -y^{(i)})x^{(i)} \tag{3}

and after that update the parameters using these equations
\begin{align*}& \text{repeat until convergence:} \; \lbrace \newline \; & \phantom {0000} b := b - \alpha \frac{\partial J(w,b)}{\partial b} \newline \; & \phantom {0000} w := w - \alpha \frac{\partial J(w,b)}{\partial w} \tag{1} \; & \newline & \rbrace\end{align*}

Finally You can check the hint below the code

Best Regards,
Abdelrahman

I am also stuck here. My value difference is just 0.1 for dj_db. Please someone should assist us

Thanks for your reply sir.
As per hint, I have done the implementation of the gradient. But still having issue. If I first compute eqn (2) and (3), we need to know about initial parameter values (b and w). can I share my implementation of the gradient code here, sir?

compute for sigmoid fx
then loss for db and dw
then divide db/m and dw/m
then return db,dw

@Dhivya_R
The intial values of w and b send to you as aparameters in the function def compute_gradient(x, y, w, b) use these values to compute the f(x) as f(x)= w*x^{(i)}+b

Sorry for couldn’t share the code here as it’s not allowed

Hi @Gamaliel_Okudo

You didn’t compute the loss function you just compute the error by f(x) - y and multiply that with the partial drivative of the f(x) according to these equations

\frac{\partial J(\mathbf{w},b)}{\partial b} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{\mathbf{w},b}(\mathbf{x}^{(i)}) - \mathbf{y}^{(i)}) \tag{2}
\frac{\partial J(\mathbf{w},b)}{\partial w_j} = \frac{1}{m} \sum\limits_{i = 0}^{m-1} (f_{\mathbf{w},b}(\mathbf{x}^{(i)}) - \mathbf{y}^{(i)})x_{j}^{(i)} \tag{3}

after tha you divide by m and return 2 variables db,dw

Hi there,

I keep on getting an error that says "prediction’ is not defined and I can’t seem to figure out what is wrong with my code. I have run all the cells in the correct sequence so I’m struggling to understand what is going on.

Please post a screen capture image that shows the entire error you see.

Note that the “Click for hints” code for compute_gradient() does not use a variable named “prediction”.

In line 24, you have misspelled the word “prediction”. You have an extra letter ‘t’.

Uploading: 17328614293774348218119586478998.jpg…
I have been getting this error message, pls what is the solution

You can paste your image directly into your reply.

Your link isn’t working, and I would not click on it in any case (for security reasons).

How about this

Your compute_gradient() function is not returning the correct value for the dj_db value.

So, what’s the solution

In short, the solution is to fix your code so it calculates dj_db correctly.

Have you read the “Click for hints” sections?
It essentially gives you the entire solution.

Yes I have corrected the code and make use of hint but it’s still giving same error message

Thanks :blush:, error message solved

1 Like