Strange log_softmax in AIRL policy train reward
Firstly, thanks for this nice implementations here! I wonder if you can help me to understand log_softmax part in DiscrimNetAIRL.build_policy_train_reward.
R(s,a) = logD(s,a) - log(1-D(s,a)) = f(s,a) - log π(a|s)
When it comes to the comments above, I got it.
But the implemented code here:
self._log_D = tf.nn.log_softmax(self.reward_net.reward_output_train)
self._log_D_compl = tf.nn.log_softmax(self.log_policy_act_prob_ph)
return self._log_D - self.entropy_weight * self._log_D_compl
Why don't you just return these:
return self.reward_net.reward_output_train - self.entropy_weight * self.log_policy_act_prob_ph
For now, log_softmax looks to add some extra term like -(logsumexp(f(s,a)) - logsumexp(log π(a|s))). Is this some reducing variance trick or else?
Please shed a light on me.
Hi @seheevic, thanks for identifying and submitting this issue. I didn't see it until now, sorry.
I believe that this is a major bug in our AIRL implementation that we introduced during refactoring. As you pointed out, the Tensors self._log_D and self._log_D_compl are different from the quantities in the paper.
Before refactoring:
# Construct discriminator logits by stacking predicted rewards
# and log action probabilities.
self._presoftmax_disc_logits = tf.stack(
[self.reward_net.reward_output_train,
self._log_policy_act_prob_ph],
axis=1, name="presoftmax_discriminator_logits") # (None, 2)
https://github.com/HumanCompatibleAI/imitation/blob/a7b33f12febe58ff2bdba6de210219ea409d9ee2/yairl/airl.py#L291-L295
After refactoring: https://github.com/HumanCompatibleAI/imitation/blob/89e395d949fc5aacea91560a294c93cb177b7cb8/src/imitation/rewards/discrim_net.py#L249-L253
The first two lines in "After refactoring" used tf.nn.log_softmax incorrectly. The "before refactoring" implementation used tf.nn.log_softmax on a Tensor with shape (None, 2). This calculated the "log softmax" of each value with respect to its row. The "After refactoring" implementation used tf.nn.log_softmax on Tensors with shape (None,). ~~Using log_softmax on a rank-1 Tensor is a no-op.~~ edit: Actually this is not a no-op, it finds log(exp(x)) / Z for each entry of the Tensor x.
Thanks @seheevic for the initial report and @shwang for the clear before-after comparison.
I do wonder now why we don't do the more direct approach of self.reward_net.reward_output_train - self.entropy_weight * self.log_policy_act_prob_ph as @seheevic suggested? I believe the log_softmax based approach, implemented correctly, should be mathematically equivalent -- but it's much harder to follow. Do we expect it to be more numerically stable?
I've been waiting for this for a long time :) And I agree with @AdamGleave that the direct approach is better. Actually @shwang 's modification seems to be incorrect because of the effect of entropy coefficient.
logD(s,a) - coeff * log(1-D(s,a)) = f(s,a) - coeff * log π(a|s) + (coeff-1) * log(e^f(s,a)+π)
The last term (coeff-1) * log(e^f(s,a)+π) is not what we want unless coeff is 1.
Thanks for helping me proofread again, @seheevic 😅.
I've updated #147 to use the direct approach