Assertion Error - C1_W2_Linear_Regression

So for the graded lab assignment, I have created my compute_cost function, but realized that the original code provided was not summing the total_cost function anywhere, and was thus only an array. To resolve this, I used return np.sum(total_cost) rather than just total_cost.

While this ensures that the output of my compute_cost function is a value and not an array, I am running into an assertion error

Can someone please assist in understanding this and suggesting a resolution.

hi @muhammadannas

See the below thread comment which will help you understand where your code must have gone incorrect

Your code for cost is incorrect for the ith example, refer the below screenshot where it mentions the right code.

Regards
DP

The test expects the cost function to return 0, indicating a perfect prediction (when the model’s predictions are correct). However, to our surprise, the cost returned is 28.0 , a far cry from perfect.

Reasons.

Incorrect Model Parameters (initial_w and initial_b) or Incorrect Implementation of the Cost Function, the compute cost might not calculate the cost as expected. The cost should be close to 0 if the predictions perfectly match the labels (y), but in your case, it’s returning 28.0, suggesting either incorrect predictions or cost calculation

The cost function just computes the cost. It is not responsible if the weight and bias have not yet been optimized.

A test case is not required to only check if the cost is zero. Other situations must also be evaluated.

hi @muhammadannas

The second link provided in my previous comments gives you right code for the cost calculation for ith iteration where you mentioned the code without the i

you used (f_wb - y) * 2 where as
it should be (f_wb -y[i]) ** 2

Regards
DP

Thank you @Deepti_Prasad. I get it now that I wasn’t slicing the target variable with [i], thus getting a tuple in the result rather than a value. Once I change, I get an Assertion Error on case 2
Error

Never mind - I got it. My cost_sum = cost_sum + cost was outside the loop

2 Likes