UNQ_C5 ValueError: math domain error

I think I wrote the code properly but it spits an error like following.

ValueError: math domain error

which indicates the line

best_probs[i,0] = math.log(A[s_idx,i])+math.log(B[i,vocab[corpus[0]]])

And I don’t think this code is wrong. Is there something I am missing now?

Hi, @CaoCao

I think you are getting this error because

or

is a log of a number that is less than or equal to zero.

HI @CaoCao,

In addition to what @arvyzukai said, it would be interesting to see exactly where the error occurs.
So you can use a try except block to catch the value error or use if else statements.
The error is due to the arguments being out of domain. The domain of log function is all positive numbers. Thus you must have used it on a negative number or zero.

Hope it helps,
Arka

Just found that B[i,vocab[corpus[0]]] returns zero and that causes the value error. But I still don’t know how to exclude the case where B[i,vocab[corpus[0]]]is zero, because it seems like it is always zero when I ran the test code. I’ve been stuck here for 3 days, could you give me a bit more hint?

In the assignment Exercise 5 (# UNQ_C5 GRADED FUNCTION: initialize) there is a condition:

        # Handle the special case when the transition from start token to POS tag i is zero
        if None: # Replace None in this line with the proper condition. # POS by word

There are instructions for this exercise which state exactly what your condition should be:

Also, to avoid taking the log of 0 (which is defined as negative infinity), the code itself will just set when …

Please read it again and give it a try

Thank you, I solved the problem, and it was because I set wrong range for the number of column in UNQ_4. Now it works!

Have the instructions for this changed? I cant find these instructions now and I am stuck with this same erroe

Hi @alekhya14

Yes, the instructions slightly changed but it should not impact your implementation. (There are no longer instructions for checking the condition that was never going to happen - in particular, transition matrix A having 0 value).

If you are getting the same error, then

  • first you should check which part of the equation produces the error (most probably ( math.log(B[i,vocab[corpus[0]]]))
  • then check why is it happening (have you applied smoothing?)

In other words, first check if Exercise 3 and Exercise 4 produce the exact outputs as in “Expected Output”

Hi @arvyzukai

Thanks! I made an error in the UNQ_4 . Fixed that and it works fine now

how you fixed it? please share the code.