From c6bf251d46676286660b7e3fe422674c768f7b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Quentin=20Gallou=C3=A9dec?= <45557362+qgallouedec@users.noreply.github.com> Date: Mon, 9 Oct 2023 11:11:36 +0200 Subject: [PATCH] Add argument `features_extractor` to `ActorCriticPolicy.extract_features` (#1710) * add argument to extract_features * remove empty lines * changelog and version --- docs/misc/changelog.rst | 3 ++- stable_baselines3/common/policies.py | 22 ++++++++++++++++------ stable_baselines3/version.txt | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/docs/misc/changelog.rst b/docs/misc/changelog.rst index 3fade42..80f1dbd 100644 --- a/docs/misc/changelog.rst +++ b/docs/misc/changelog.rst @@ -3,7 +3,7 @@ Changelog ========== -Release 2.2.0a6 (WIP) +Release 2.2.0a7 (WIP) -------------------------- Breaking Changes: @@ -52,6 +52,7 @@ Others: - Fixed ``stable_baselines3/common/buffers.py`` type hints - Fixed ``stable_baselines3/her/her_replay_buffer.py`` type hints - Buffers do no call an additional ``.copy()`` when storing new transitions +- Fixed ``ActorCriticPolicy.extract_features()`` signature by adding an optional ``features_extractor`` argument Documentation: ^^^^^^^^^^^^^^ diff --git a/stable_baselines3/common/policies.py b/stable_baselines3/common/policies.py index e975349..9bb7b11 100644 --- a/stable_baselines3/common/policies.py +++ b/stable_baselines3/common/policies.py @@ -123,9 +123,9 @@ class BaseModel(nn.Module): """ Preprocess the observation if needed and extract features. - :param obs: The observation - :param features_extractor: The features extractor to use. - :return: The extracted features + :param obs: Observation + :param features_extractor: The features extractor to use. + :return: The extracted features """ preprocessed_obs = preprocess_obs(obs, self.observation_space, normalize_images=self.normalize_images) return features_extractor(preprocessed_obs) @@ -642,16 +642,26 @@ class ActorCriticPolicy(BasePolicy): actions = actions.reshape((-1, *self.action_space.shape)) 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. :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: - 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: + 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) vf_features = super().extract_features(obs, self.vf_features_extractor) return pi_features, vf_features diff --git a/stable_baselines3/version.txt b/stable_baselines3/version.txt index 47f323d..9b9407a 100644 --- a/stable_baselines3/version.txt +++ b/stable_baselines3/version.txt @@ -1 +1 @@ -2.2.0a6 +2.2.0a7