-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
box.contains check dtype and promote non-ndarrays #2374
Conversation
Closes: openai#2357 and openai#2298 Instead of only casting list to ndarray, cast any class to ndarray (if possible) and emit a warning when casting. Also, check if the dtype of the input matches the dtype of the space.
@tristandeleu Looks like gym's tests depend on Box.contains old behavior of not checking types (https://github.com/openai/gym/pull/2374/checks?check_run_id=3450441531#step:4:252). Potentially downstream code will do this, too, making the current PR a breaking change. What is gym's approach to such scenarios? Change the function to make the dtype check optional and backward-compatible (i.e. add a @jkterry1 Since the current version of the PR would contain a breaking change (due to the additional check suggested by #2298 ) do you have an opinion/preference on this matter? |
There are two issues coming out of those tests:
diff --git a/gym/envs/toy_text/kellycoinflip.py b/gym/envs/toy_text/kellycoinflip.py
index 1a47c0a..4f305ab 100644
--- a/gym/envs/toy_text/kellycoinflip.py
+++ b/gym/envs/toy_text/kellycoinflip.py
@@ -79,7 +79,7 @@ class KellyCoinflipEnv(gym.Env):
return self._get_obs(), reward, done, {}
def _get_obs(self):
- return np.array([self.wealth]), self.rounds
+ return np.array([self.wealth], dtype=np.float32), self.rounds
def reset(self):
self.rounds = self.max_rounds
@@ -236,11 +236,11 @@ class KellyCoinflipGeneralizedEnv(gym.Env):
def _get_obs(self):
return (
- np.array([float(self.wealth)]),
+ np.array([float(self.wealth)], dtype=np.float32),
self.rounds_elapsed,
self.wins,
self.losses,
- np.array([float(self.max_ever_wealth)]),
+ np.array([float(self.max_ever_wealth)], dtype=np.float32),
)
def reset(self):
diff --git a/gym/wrappers/test_flatten_observation.py b/gym/wrappers/test_flatten_observation.py
index f190081..2db176c 100644
--- a/gym/wrappers/test_flatten_observation.py
+++ b/gym/wrappers/test_flatten_observation.py
@@ -19,12 +19,12 @@ def test_flatten_observation(env_id):
space = spaces.Tuple(
(spaces.Discrete(32), spaces.Discrete(11), spaces.Discrete(2))
)
- wrapped_space = spaces.Box(-np.inf, np.inf, [32 + 11 + 2], dtype=np.float32)
+ wrapped_space = spaces.Box(0, 1, [32 + 11 + 2], dtype=np.int64)
elif env_id == "KellyCoinflip-v0":
space = spaces.Tuple(
(spaces.Box(0, 250.0, [1], dtype=np.float32), spaces.Discrete(300 + 1))
)
- wrapped_space = spaces.Box(-np.inf, np.inf, [1 + (300 + 1)], dtype=np.float32)
+ wrapped_space = spaces.Box(-np.inf, np.inf, [1 + (300 + 1)], dtype=np.float64)
assert space.contains(obs)
assert wrapped_space.contains(wrapped_obs) |
@tristandeleu I updated the tests; are we worried about informing downstream about this change? I.e. should we bump a version, update a changelog, or leave a comment somewhere saying "Breaking change: Box.contains now also matches dtype when checking for membership" or something? |
@FirefoxMetzger Yes this will probably require bumping the version, to avoid any surprises. I don't know what is the status on the changelog though (there was a discussion here #2275, but I don't know what was the outcome), but it would definitely be a good idea to inform users of this change through the changelog. |
This was stated elsewhere, but the changelogs are going in GitHub release notes, e.g. here is the changelog for the last release: https://github.com/openai/gym/releases/tag/0.19.0 |
@FirefoxMetzger this needs tests |
Co-authored-by: Tristan Deleu <tristandeleu@users.noreply.github.com>
Co-authored-by: Tristan Deleu <tristandeleu@users.noreply.github.com>
@jkterry1 I've added a unit test to avoid regression regarding the two issues addressed by this PR. Also note that this is also covered by existing tests (the two that were adjusted as part of this PR). |
Just to confirm, the only version bump that would be required would be for KellyCoinflip right? |
@jkterry1 Good catch with the environment version. I'm not an active user of KellyCoinflip, so I can't estimate if the reduction in precision (float64 -> float32) will affect performance or existing algorithms. If so, then the version of the environment should indeed be bumped. The version bump I was talking about with @tristandeleu was related to the overall gym version. The function |
I have sort of an unrelated comment, but I noticed in |
@rohanb2018 Good catch; this is not how it should be. It changed from |
* box.contains check dtype and promote non-ndarrays Closes: openai#2357 and openai#2298 Instead of only casting list to ndarray, cast any class to ndarray (if possible) and emit a warning when casting. Also, check if the dtype of the input matches the dtype of the space. * use import warnings * blackify * changs from code review * fix wrapped space Co-authored-by: Tristan Deleu <tristandeleu@users.noreply.github.com> * fix box bondaries Co-authored-by: Tristan Deleu <tristandeleu@users.noreply.github.com> * TEST: add regression test. * STY: black Co-authored-by: Tristan Deleu <tristandeleu@users.noreply.github.com>
Closes: #2357 and #2298
Instead of only casting list to ndarray, cast any class to ndarray (if possible) and emit a warning when casting. Also, check if the dtype of the input matches the dtype of the space.