Week1: regularization using L2

I do not understand what is the problem with the test, I have made the equation outside the cost function and it works well, but when I test it THE NEXT TEST fails.
I did not use any global variables.

Try putting the equation inside the function, it says dont use global variables so…

I put it inside the function.
But, I check how it works first by printing it outside the function, then comment and keep it inside the function only.

There is an obvious “order of operations” landmine in the L2 calculation that I’m guessing you stepped on. Try the following and watch what happens:

m = 5.
x = 1. / 2. * m
y = 1. / (2. * m)

If you’re expecting x and y to have the same value, you’re in for an unpleasant surprise. :scream_cat:

1 Like

Thanks a lot, I thought () and * have the same order.

Not sure what you mean by that, but the point is that () over-rides everything, right? That’s literally what they are for.

The relevant point is that / and * have the same order, so they get executed from left to right, which is you need the parens as in the y expression I gave above in order to implement the L2 formula correctly.