Week 3, Error

Why do I have the “DW1” wrong if the expected output is identical to my output?

There are two different test cases there. If you get one correct and the other wrong, then the first theory to investigate is to check for some form of “hard-coding” in your solution that makes it specific to that first test case.

It might also be useful to look at the other test case to understand what is different about it. You can do that by clicking “File → Open” and then opening the file public_tests.py and looking for the function that is being called there.

Can I get a small hint?
I have reviewed my code to make sure that everything isn’t “hard-coded” and that every test was passed (every previous function works). I want to assume that my mistake is in the “dW1” because there is no error in any other function or line.

{mentor edit - solution code removed}

Ok, sorry, this is a problem with the test. Your code is actually technically correct, but you’ve implemented the derivative of tanh by going back to pure math.

g(Z) = tanh(Z)
g'(Z) = 1 - tanh^2(Z)

But in this case we also have A = tanh(Z), so the simpler and less expensive way to write the derivative is:

g'(Z) = 1 - A^2

It’s less expensive, since tanh involves the exponential function. When they generated the first test case that you passed, they created a test case in which A1 = tanh(Z1), but in the other test, they just generated all the values randomly. So A1 \neq tanh(Z1) in the second test case.

So your correct code fails. But you could have written the code in a more obvious and efficient way.

I filed a bug about this test case a while ago, but they have not fixed it yet.

So I apologize for what I said above: I had forgotten this problem with the test cases. So your code is technically correct and the test is wrong.

Please try making the fix I suggested above and let us know if that gets you “pass”.

Also as a general matter, note that we aren’t supposed to publish source code. If we need to see it, we’ll send you a DM (personal message) about that. But no harm done: I will just edit your post to remove the code, now that we have an answer.

Thank you very much!! That was really helpful. You are also correct, using “A1” instead of the tanh() makes the code look clean. :grin:

1 Like