Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix mean logit calculation in neurd loss for rnad #1157

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions open_spiel/python/algorithms/rnad/rnad.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,9 @@ def get_loss_nerd(logit_list: Sequence[chex.Array],
"""Define the nerd loss."""
assert isinstance(importance_sampling_correction, list)
loss_pi_list = []

num_valid_actions = jnp.sum(legal_actions, axis=-1, keepdims=True)

for k, (logit_pi, pi, q_vr, is_c) in enumerate(
zip(logit_list, policy_list, q_vr_list, importance_sampling_correction)):
assert logit_pi.shape[0] == q_vr.shape[0]
Expand All @@ -570,9 +573,12 @@ def get_loss_nerd(logit_list: Sequence[chex.Array],
adv_pi = is_c * adv_pi # importance sampling correction
adv_pi = jnp.clip(adv_pi, a_min=-clip, a_max=clip)
adv_pi = lax.stop_gradient(adv_pi)

logits = logit_pi - jnp.mean(
logit_pi * legal_actions, axis=-1, keepdims=True)

valid_logit_sum = jnp.sum(logit_pi * legal_actions, axis=-1, keepdims=True)
mean_logit = valid_logit_sum / num_valid_actions

# Subtract only the mean of the valid logits
logits = logit_pi - mean_logit

threshold_center = jnp.zeros_like(logits)

Expand Down