Fix return type of evaluate_actions (#1118)

* Fix return type of ActorCriticPolicy.evaluate_actions to optional entropy tensor

* Update changelog.rst
This commit is contained in:
Juan Rocamonde 2022-10-14 16:45:28 +01:00 committed by GitHub
parent b77a0667b2
commit cdcdd32c51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 6 additions and 2 deletions

View file

@ -22,6 +22,7 @@ SB3-Contrib
Bug Fixes: Bug Fixes:
^^^^^^^^^^ ^^^^^^^^^^
- Fix return type of ``evaluate_actions`` in ``ActorCritcPolicy`` to reflect that entropy is an optional tensor (@Rocamonde)
Deprecations: Deprecations:
^^^^^^^^^^^^^ ^^^^^^^^^^^^^

View file

@ -613,7 +613,7 @@ class ActorCriticPolicy(BasePolicy):
""" """
return self.get_distribution(observation).get_actions(deterministic=deterministic) return self.get_distribution(observation).get_actions(deterministic=deterministic)
def evaluate_actions(self, obs: th.Tensor, actions: th.Tensor) -> Tuple[th.Tensor, th.Tensor, th.Tensor]: def evaluate_actions(self, obs: th.Tensor, actions: th.Tensor) -> Tuple[th.Tensor, th.Tensor, Optional[th.Tensor]]:
""" """
Evaluate actions according to the current policy, Evaluate actions according to the current policy,
given the observations. given the observations.
@ -629,7 +629,8 @@ class ActorCriticPolicy(BasePolicy):
distribution = self._get_action_dist_from_latent(latent_pi) distribution = self._get_action_dist_from_latent(latent_pi)
log_prob = distribution.log_prob(actions) log_prob = distribution.log_prob(actions)
values = self.value_net(latent_vf) values = self.value_net(latent_vf)
return values, log_prob, distribution.entropy() entropy = distribution.entropy()
return values, log_prob, entropy
def get_distribution(self, obs: th.Tensor) -> Distribution: def get_distribution(self, obs: th.Tensor) -> Distribution:
""" """

View file

@ -77,6 +77,8 @@ def test_get_distribution(dummy_model_distribution_obs_and_actions):
distribution = model.policy.get_distribution(observations) distribution = model.policy.get_distribution(observations)
log_prob_2 = distribution.log_prob(actions) log_prob_2 = distribution.log_prob(actions)
entropy_2 = distribution.entropy() entropy_2 = distribution.entropy()
assert entropy_1 is not None
assert entropy_2 is not None
assert th.allclose(log_prob_1, log_prob_2) assert th.allclose(log_prob_1, log_prob_2)
assert th.allclose(entropy_1, entropy_2) assert th.allclose(entropy_1, entropy_2)