# Entropy term in Proximal Policy Optimization (PPO) becomes undefined after few training epochs

I have implemented the total loss of my PPO objective as follows:-

total_loss = critic_discount * critic_loss + actor_loss -  entropy_beta * K.mean(-(newpolicy_probs * K.log(newpolicy_probs)))


After training for a few epochs, the entropy term becomes "nan" for some reason. I used tf.Print() to see the new policy probabilities when the entropy becomes undefined, it is as follows-

new policy probs: [[6.1029973e-06 1.93471514e-08 0.000299338106...]...]

I am not clear as to why taking log of these small probabilities is coming out as nan. Any idea how to prevent this?

• Log of zero is mathematically undefined. On a computer, the log is likely approximated (it depends on the implementation). Furthermore, the multiplication of small numbers creates even smaller numbers (e.g. the product of $0.1*0.1 = 0.01$), which leads quite quickly to the "vanishing problem". Are you using floats (single-precision) or doubles? If you're using floats, maybe try using doubles? Or maybe try to manually handle the case where too small probabilities that are passed to the log. If this answers your question, let me know, and I will convert this comment to an answer. – nbro Jul 24 at 20:18
• However, note that this question is off-topic here. You should ask it on Stack Overflow, given that this is a problem related to the implementation. Here we focus more on conceptual and philosophical questions. – nbro Jul 24 at 20:21
• I addressed my implemetation issue with the solution in my answer below. However, I still have conceptual doubts so I opened a separate question here: ai.stackexchange.com/questions/13577/… , thanks for the advice! – Chintan Trivedi Jul 25 at 14:14
• I did handle small values manually by adding 1e-10 so if you want to post it as an answer I'll accept it. – Chintan Trivedi Jul 25 at 14:17