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.

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 …

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”