# UNQ\_C5 ValueError: math domain error

**URL:** <https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189>\
**Category:** NLP with Probabilistic Models\
**Tags:** week-module-2\
**Created:** [June 6, 2022, 1:36pm UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189 "2022-06-06T13:36:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![CaoCao](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@CaoCao](https://community.deeplearning.ai/u/CaoCao)\
**Post date:** [June 6, 2022, 1:36pm UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/1 "2022-06-06T13:36:09Z")

</div>

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

```auto
ValueError: math domain error

```

which indicates the line

```auto
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?

---

<div class="post-metadata">

**Author:** ![arvyzukai](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/arvyzukai/32/165165_2.png) [@arvyzukai](https://community.deeplearning.ai/u/arvyzukai)\
**Post date:** [June 7, 2022, 5:14am UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/2 "2022-06-07T05:14:54Z")

</div>

Hi, @CaoCao

I think you are getting this error because

> [@CaoCao](#):
>
> `math.log(A[s_idx,i])`

or

> [@CaoCao](#):
>
> `math.log(B[i,vocab[corpus[0]]])`

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

---

<div class="post-metadata">

**Author:** ![thearkamitra](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/thearkamitra/32/29965_2.png) [@thearkamitra](https://community.deeplearning.ai/u/thearkamitra)\
**Post date:** [June 7, 2022, 5:30am UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/3 "2022-06-07T05:30:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CaoCao](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@CaoCao](https://community.deeplearning.ai/u/CaoCao)\
**Post date:** [June 7, 2022, 1:08pm UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/4 "2022-06-07T13:08:47Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![arvyzukai](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/arvyzukai/32/165165_2.png) [@arvyzukai](https://community.deeplearning.ai/u/arvyzukai)\
**Post date:** [June 8, 2022, 5:32am UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/5 "2022-06-08T05:32:39Z")

</div>

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

```auto
        # 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

---

<div class="post-metadata">

**Author:** ![CaoCao](https://avatars.discourse-cdn.com/v4/letter/c/7993a0/32.png) [@CaoCao](https://community.deeplearning.ai/u/CaoCao)\
**Post date:** [June 8, 2022, 3:01pm UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/6 "2022-06-08T15:01:14Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![alekhya14](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/alekhya14/32/162848_2.png) [@alekhya14](https://community.deeplearning.ai/u/alekhya14)\
**Post date:** [May 8, 2023, 4:54pm UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/7 "2023-05-08T16:54:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![arvyzukai](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/arvyzukai/32/165165_2.png) [@arvyzukai](https://community.deeplearning.ai/u/arvyzukai)\
**Post date:** [May 9, 2023, 5:21am UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/8 "2023-05-09T05:21:24Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![alekhya14](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/alekhya14/32/162848_2.png) [@alekhya14](https://community.deeplearning.ai/u/alekhya14)\
**Post date:** [May 9, 2023, 5:37am UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/9 "2023-05-09T05:37:14Z")

</div>

Hi @arvyzukai

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

---

<div class="post-metadata">

**Author:** ![Arin\_Jain](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@Arin\_Jain](https://community.deeplearning.ai/u/Arin_Jain)\
**Post date:** [August 19, 2023, 12:06pm UTC](https://community.deeplearning.ai/t/unq-c5-valueerror-math-domain-error/135189/10 "2023-08-19T12:06:12Z")

</div>

how you fixed it? please share the code.
