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

[BugFix] Allow for composite action distributions in PPO/A2C losses #2391

Merged
merged 19 commits into from
Sep 4, 2024
Prev Previous commit
Next Next commit
fix tests ppo
  • Loading branch information
albertbou92 committed Aug 12, 2024
commit bc00e2d87746d06459696b7e1cf654bc1b02f566
12 changes: 4 additions & 8 deletions torchrl/objectives/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ def get_entropy_bonus(self, dist: d.Distribution) -> torch.Tensor:
except NotImplementedError:
x = dist.rsample((self.samples_mc_entropy,))
if isinstance(dist, CompositeDistribution):
log_prob = dist.log_prob(x).get(self.tensor_keys.sample_log_prob)
log_prob = dist.log_prob(x).get("sample_log_prob")
else:
log_prob = dist.log_prob(x)
entropy = -log_prob.mean(0)
Expand All @@ -478,7 +478,7 @@ def _log_weight(

if isinstance(dist, CompositeDistribution):
vmoens marked this conversation as resolved.
Show resolved Hide resolved
tensordict = dist.log_prob(tensordict)
log_prob = tensordict.get(self.tensor_keys.sample_log_prob)
log_prob = tensordict.get("sample_log_prob")
else:
log_prob = dist.log_prob(action)

Expand Down Expand Up @@ -1118,12 +1118,8 @@ def forward(self, tensordict: TensorDictBase) -> TensorDict:
except NotImplementedError:
x = previous_dist.sample((self.samples_mc_kl,))
if isinstance(current_dist, CompositeDistribution):
vmoens marked this conversation as resolved.
Show resolved Hide resolved
previous_log_prob = previous_dist.log_prob(x).get(
self.tensor_keys.sample_log_prob
)
current_log_prob = current_dist.log_prob(x).get(
self.tensor_keys.sample_log_prob
)
previous_log_prob = previous_dist.log_prob(x).get("sample_log_prob")
current_log_prob = current_dist.log_prob(x).get("sample_log_prob")
else:
previous_log_prob = previous_dist.log_prob(x)
current_log_prob = current_dist.log_prob(x)
Expand Down