Add argument features_extractor to ActorCriticPolicy.extract_features (#1710)

* add argument to extract_features

* remove empty lines

* changelog and version
This commit is contained in:
Quentin Gallouédec 2023-10-09 11:11:36 +02:00 committed by GitHub
parent c6c660e51b
commit c6bf251d46
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 8 deletions

View file

@ -3,7 +3,7 @@
Changelog Changelog
========== ==========
Release 2.2.0a6 (WIP) Release 2.2.0a7 (WIP)
-------------------------- --------------------------
Breaking Changes: Breaking Changes:
@ -52,6 +52,7 @@ Others:
- Fixed ``stable_baselines3/common/buffers.py`` type hints - Fixed ``stable_baselines3/common/buffers.py`` type hints
- Fixed ``stable_baselines3/her/her_replay_buffer.py`` type hints - Fixed ``stable_baselines3/her/her_replay_buffer.py`` type hints
- Buffers do no call an additional ``.copy()`` when storing new transitions - Buffers do no call an additional ``.copy()`` when storing new transitions
- Fixed ``ActorCriticPolicy.extract_features()`` signature by adding an optional ``features_extractor`` argument
Documentation: Documentation:
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

View file

@ -123,9 +123,9 @@ class BaseModel(nn.Module):
""" """
Preprocess the observation if needed and extract features. Preprocess the observation if needed and extract features.
:param obs: The observation :param obs: Observation
:param features_extractor: The features extractor to use. :param features_extractor: The features extractor to use.
:return: The extracted features :return: The extracted features
""" """
preprocessed_obs = preprocess_obs(obs, self.observation_space, normalize_images=self.normalize_images) preprocessed_obs = preprocess_obs(obs, self.observation_space, normalize_images=self.normalize_images)
return features_extractor(preprocessed_obs) return features_extractor(preprocessed_obs)
@ -642,16 +642,26 @@ class ActorCriticPolicy(BasePolicy):
actions = actions.reshape((-1, *self.action_space.shape)) actions = actions.reshape((-1, *self.action_space.shape))
return actions, values, log_prob return actions, values, log_prob
def extract_features(self, obs: th.Tensor) -> Union[th.Tensor, Tuple[th.Tensor, th.Tensor]]: def extract_features(
self, obs: th.Tensor, features_extractor: Optional[BaseFeaturesExtractor] = None
) -> Union[th.Tensor, Tuple[th.Tensor, th.Tensor]]:
""" """
Preprocess the observation if needed and extract features. Preprocess the observation if needed and extract features.
:param obs: Observation :param obs: Observation
:return: the output of the features extractor(s) :param features_extractor: The features extractor to use. If None, then ``self.features_extractor`` is used.
:return: The extracted features. If features extractor is not shared, returns a tuple with the
features for the actor and the features for the critic.
""" """
if self.share_features_extractor: if self.share_features_extractor:
return super().extract_features(obs, self.features_extractor) return super().extract_features(obs, self.features_extractor if features_extractor is None else features_extractor)
else: else:
if features_extractor is not None:
warnings.warn(
"Provided features_extractor will be ignored because the features extractor is not shared.",
UserWarning,
)
pi_features = super().extract_features(obs, self.pi_features_extractor) pi_features = super().extract_features(obs, self.pi_features_extractor)
vf_features = super().extract_features(obs, self.vf_features_extractor) vf_features = super().extract_features(obs, self.vf_features_extractor)
return pi_features, vf_features return pi_features, vf_features

View file

@ -1 +1 @@
2.2.0a6 2.2.0a7