Week 2 Exercise 8 - Value Error

Hi, I got this error when working on the final model exercise, I checked through the previous exercises and everything went well. Hope someone can guide me on my problem so I can solve it. Thank you very much in advance!

Hi @Tan_Chin_Yong, as per honor code, you canā€™t share the codes at this platform. Kindly remove it.
The issue is that the boolean value for print cost is not defined when you are calling the functions for params, grads and costs .

Secondly, check the equations you are using for the **cost ** as per this formula:

-\frac{1}{m}\sum_{i=1}^{m}(y^{(i)}\log(a^{(i)})+(1-y^{(i)})\log(1-a^{(i)}))$

You are missing the element-wise multiplication (* sign) as well as not using the np.log in a proper way.

Hi, @Rashmi. We routinely encourage leaners to post their tracebacks. With that in mind, there is no violation. These are difficult straits to navigate, and we hope that the community will abide by the mores to further the learning goals of all. Best, @kenb

There is nothing wrong with the way that print_cost is handled that I can see. Also the cost formula is fine, other than the fact that you donā€™t need the np.sum there if you are using dot products: the dot product does the multiply and the sum in one shot, which is why it is the easier way to go. Although you need the transpose, but you have handled that correctly already.

The real question is why the w that is passed down to propagate is 3 x 1, when it should be 4 x 1. The bug is not in propagate: it is most likely in model. I donā€™t see any errors that would cause that in the code we can see in the exception trace. Are you sure you clicked ā€œShift-Enterā€ to execute the model logic and make sure that your latest version of the code is actually what you are running? Maybe the cleanest way to make sure things are consistent is to do:

Kernel -> Restart and Clear Output
Cell -> Run All

Then see if that same error gets thrown or not.

Dear Rashmi, I checked through my equation for cost and corrected it to as below:
cost=(-1/m)*(np.dot(Y,np.log(A).T)+np.dot((1-Y),np.log(1-A).T))

Is this correct?
I tried to run my exercise 5, the result is all tests passed and the value matched the expected output.

Dear Sir, Iā€™ve tried to Restart and Run all codes again, but the similar error still happens.
Thank you for pointing out the problem, I am figuring out why is the w that is passed down is 3x1.

These are my code for Exercise 8:

{moderator edit - solution code removed}

I am sorry if i violate any rules for posting the code, I will remove immediately once the problem is solved. Thank you for your kind consideration!

Everything you show looks correct as far as I can see. Maybe the bug is in optimize. Are you still getting the 1 x 3 versus 4 x 7 error on the dot product in propagate?

Yes Sir, I still get the same error after restarting and running all codes over again. I cross-checked the previous exercises but still couldnā€™t spot my problem.
{moderator edit - solution code removed}

Hi @Tan_Chin_Yong, Itā€™s okay. I mistook the traceback as the original codes. You are nowhere violating the honor code. As far as I know, the codes are fine. Yes, Paul sir is right, the bug is in the optimize. Go through it once again and follow the restarting process (Go to kernelā€”restart and clear outputā€”save your workā€“then try running the cells from the beginning through shift+enter key). Let me see if this works for you.

Dear @Rashmi, Iā€™ve tried go through the code over again, but I still get the similar error code as posted in this question. Appreciate if you could help to have a look on my code. Thank you!
{moderator edit - notebook removed}

The problem is simple: the code you showed us above is not actually what you are running. You showed this above:

w, b = initialize_with_zeros(X_train.shape[0])

But what you actually have references the global variable dim for the dimension. That has nothing to do with the actual data, which is why you get the wrong answer.

1 Like

Yes, Paul sir is right @Tan_Chin_Yong. Also, I can find (np.sum) is still running while you are calling the functions for ā€˜costā€™.

1 Like

No sorry, the bug is actually in your initialize_with_zeros code. Hereā€™s what it says:

w=np.zeros((3,1))

When I fixed the code in model, it still failed and this is why. Now the question is why that passes the test case for initialize_with_zeros. It must use 3 as the value of dim. So this is the classic example of hard-coding something so that it passes one particular test, but then fails later.

1 Like

Dear Sir, I corrected my mistake in the earlier initialize_with zeros code and managed to complete the assignment. I am more than grateful for your help and I learned a lot through this mistake. Thank you! :smiley: