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 stable_baselines3/common/distributions.py type hint #123

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion sb3_contrib/common/maskable/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ def evaluate_actions(
distribution.apply_masking(action_masks)
log_prob = distribution.log_prob(actions)
values = self.value_net(latent_vf)
return values, log_prob, distribution.entropy()
try:
entropy = distribution.entropy()
except NotImplementedError:
# When no analytical form, entropy needs to be estimated using -log_prob.mean()
entropy = -log_prob.mean(dim=1)
return values, log_prob, entropy

def get_distribution(self, obs: th.Tensor, action_masks: Optional[np.ndarray] = None) -> MaskableDistribution:
"""
Expand Down
7 changes: 6 additions & 1 deletion sb3_contrib/common/recurrent/policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,12 @@ def evaluate_actions(
distribution = self._get_action_dist_from_latent(latent_pi)
log_prob = distribution.log_prob(actions)
values = self.value_net(latent_vf)
return values, log_prob, distribution.entropy()
try:
entropy = distribution.entropy()
except NotImplementedError:
# When no analytical form, entropy needs to be estimated using -log_prob.mean()
entropy = -log_prob.mean(dim=1)
return values, log_prob, entropy

def _predict(
self,
Expand Down
6 changes: 1 addition & 5 deletions sb3_contrib/ppo_mask/ppo_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,7 @@ def train(self) -> None:
value_losses.append(value_loss.item())

# Entropy loss favor exploration
if entropy is None:
# Approximate entropy when no analytical form
entropy_loss = -th.mean(-log_prob)
else:
entropy_loss = -th.mean(entropy)
entropy_loss = -th.mean(entropy)

entropy_losses.append(entropy_loss.item())

Expand Down
6 changes: 1 addition & 5 deletions sb3_contrib/ppo_recurrent/ppo_recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,7 @@ def train(self) -> None:
value_losses.append(value_loss.item())

# Entropy loss favor exploration
if entropy is None:
# Approximate entropy when no analytical form
entropy_loss = -th.mean(-log_prob[mask])
else:
entropy_loss = -th.mean(entropy[mask])
entropy_loss = -th.mean(entropy[mask])

entropy_losses.append(entropy_loss.item())

Expand Down