-
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
[Bug Report] Total order of keys for spaces.Dict
#3023
Comments
As I commented in jax-ml/jax#11871 (comment), a feasible solution is sorting by |
Thanks for the issue but Im confused what the issue that needs solving is? Looking back, I disable that we sort the dict keys and not just follow the order of insertion but it seems to late to change. |
@pseudo-rnd-thoughts, the issue is:
There are two solutions:
Python built-in In [1]: from collections import OrderedDict
In [2]: d1 = {1: 0, 'a': 1}
In [3]: d2 = {'a': 1, 1: 0}
In [4]: d1
Out[4]: {1: 0, 'a': 1}
In [5]: d2
Out[5]: {'a': 1, 1: 0}
In [6]: d1 == d2
Out[6]: True
In [7]: od1 = OrderedDict([(1, 0), ('a', 1)])
In [8]: od2 = OrderedDict([('a', 1), (1, 0)])
In [9]: d1 == od1
Out[9]: True
In [10]: d2 == od1
Out[10]: True When comparing two In [11]: od1 == od2
Out[11]: False
In [12]: dict(od1) == dict(od2)
Out[12]: True We save the
We do not need to disable the sorting behavior, just updating the |
Ok that makes sense. |
Done. |
I definitely think this should be fixed by updating the |
If you are submitting a bug report, please fill in the following details and use the tag [bug].
Describe the bug
A clear and concise description of what the bug is.
Pair of issue jax-ml/jax#11871. A total order is required for dictionary keys.
Equal
Dict
spacesdict1 == dict2
do not implydict1.sample() == dict2.sample()
andflatten_space(dict1) == flatten_space(dict2)
.Not equally seeded in subspaces:
Different order while flattening:
The order of keys is important when seeding the subspaces and flattening the space.
In
spaces.Dict.__init__
method, we always convert the inputs into anOrderedDict
:gym/gym/spaces/dict.py
Lines 82 to 92 in 8b74413
However, function
sorted
will fail when sorting with uncomparable types (e.g.int
vs.str
):So we add a failback choice at line 88 in PR #2491. This means when the keys are not sortable, the keys are ordered by the insertion order (since Python 3.6). However, the order of keys is important when seeding the subspaces and flattening the space.
seed
: (seed in order ofdict.spaces.values()
)gym/gym/spaces/dict.py
Lines 131 to 135 in 8b74413
flatten_space
: (flatten in order ofdict.spaces.values()
)gym/gym/spaces/utils.py
Lines 326 to 333 in 8b74413
This will cause
dict1 == dict2
does not implyflatten_space(dict1) == flatten_space(dict2)
.Code example
Please try to provide a minimal example to reproduce the bug. Error messages and stack traces are also helpful.
See Description above.
System Info
Describe the characteristic of your environment:
Additional context
Add any other context about the problem here.
See also:
tree_util
jax-ml/jax#11871Checklist
The text was updated successfully, but these errors were encountered: