From 6748009019f51b33413e5191120a0139ebf5fc57 Mon Sep 17 00:00:00 2001 From: Olaf Lipinski Date: Fri, 5 Aug 2022 09:05:18 +0100 Subject: [PATCH 1/4] Fix OneHotPreprocessor, use gym.spaces.utils.flatten Signed-off-by: Olaf Lipinski --- rllib/models/preprocessors.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/rllib/models/preprocessors.py b/rllib/models/preprocessors.py index 2cdd0ff82ade..da02893daa82 100644 --- a/rllib/models/preprocessors.py +++ b/rllib/models/preprocessors.py @@ -185,13 +185,7 @@ def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]: @override(Preprocessor) def transform(self, observation: TensorType) -> np.ndarray: self.check_shape(observation) - arr = np.zeros(self._init_shape(self._obs_space, {}), dtype=np.float32) - if isinstance(self._obs_space, gym.spaces.Discrete): - arr[observation] = 1 - else: - for i, o in enumerate(observation): - arr[np.sum(self._obs_space.nvec[:i]) + o] = 1 - return arr + return gym.spaces.utils.flatten(self._obs_space, observation) @override(Preprocessor) def write(self, observation: TensorType, array: np.ndarray, offset: int) -> None: From 77a6f32755823b9f5f51ec8497fbfaee005b9ffe Mon Sep 17 00:00:00 2001 From: Olaf Lipinski Date: Mon, 8 Aug 2022 09:48:27 +0100 Subject: [PATCH 2/4] Add unit tests for multidimensional multidiscretes Signed-off-by: Olaf Lipinski --- rllib/models/tests/test_preprocessors.py | 56 ++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/rllib/models/tests/test_preprocessors.py b/rllib/models/tests/test_preprocessors.py index ca2f869665e6..1d97f00aaeab 100644 --- a/rllib/models/tests/test_preprocessors.py +++ b/rllib/models/tests/test_preprocessors.py @@ -154,6 +154,62 @@ def test_nested_multidiscrete_one_hot_preprocessor(self): [1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0], ) + def test_multidimensional_multidiscrete_one_hot_preprocessor(self): + space2d = MultiDiscrete([[2, 2], [3, 3]]) + space3d = MultiDiscrete([[[2, 2], [3, 4]], [[5, 6], [7, 8]]]) + pp2d = get_preprocessor(space2d)(space2d) + pp3d = get_preprocessor(space3d)(space3d) + self.assertTrue(isinstance(pp2d, OneHotPreprocessor)) + self.assertTrue(isinstance(pp3d, OneHotPreprocessor)) + self.assertTrue(pp2d.shape == (10,)) + self.assertTrue(pp3d.shape == (37,)) + check( + pp2d.transform(np.array([[1, 0], [2, 1]])), + [0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0], + ) + check( + pp3d.transform(np.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]])), + [ + 1.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 0.0, + 1.0, + ], + ) + if __name__ == "__main__": import pytest From 93cb3d01aa11d8d13b9d57979464900fec714d87 Mon Sep 17 00:00:00 2001 From: Jun Gong Date: Thu, 1 Sep 2022 09:19:15 -0700 Subject: [PATCH 3/4] ci From efb8f397002679023af1f12b3fafed5156c9e945 Mon Sep 17 00:00:00 2001 From: Olaf Lipinski Date: Wed, 7 Sep 2022 10:23:21 +0100 Subject: [PATCH 4/4] Fix issue with auoregressive torch model Signed-off-by: Olaf Lipinski --- rllib/models/preprocessors.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rllib/models/preprocessors.py b/rllib/models/preprocessors.py index da02893daa82..98014ed94120 100644 --- a/rllib/models/preprocessors.py +++ b/rllib/models/preprocessors.py @@ -185,7 +185,7 @@ def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]: @override(Preprocessor) def transform(self, observation: TensorType) -> np.ndarray: self.check_shape(observation) - return gym.spaces.utils.flatten(self._obs_space, observation) + return gym.spaces.utils.flatten(self._obs_space, observation).astype(np.float32) @override(Preprocessor) def write(self, observation: TensorType, array: np.ndarray, offset: int) -> None: