# Dict observation space flattened

**URL:** <https://discuss.ray.io/t/dict-observation-space-flattened/541>\
**Category:** RLlib\
**Created:** [January 22, 2021, 1:58pm UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541 "2021-01-22T13:58:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex\_koch](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@alex\_koch](https://discuss.ray.io/u/alex_koch)\
**Post date:** [January 22, 2021, 1:58pm UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541/1 "2021-01-22T13:58:12Z")

</div>

I want to use Dictionary observations in the model. However, RLlib seems to automatically flatten gym dict spaces. The custom\_preprocessor settings has been deprecated. So I can’t find a way to prevent rllib from flattening the observation space.  
Does anyone have an idea how I could prevent rllib from flattening the observation space?

---

<div class="post-metadata">

**Author:** ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)\
**Post date:** [January 22, 2021, 3:56pm UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541/2 "2021-01-22T15:56:22Z")

</div>

You can still get to the original observation inside your custom model by accessing the input\_dict’s “obs” key, no?

---

<div class="post-metadata">

**Author:** ![alex\_koch](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@alex\_koch](https://discuss.ray.io/u/alex_koch)\
**Post date:** [January 22, 2021, 4:31pm UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541/3 "2021-01-22T16:31:05Z")

</div>

Hi,

thank you for your answer.

No, the input\_dict[‘obs’] contains only a flattened Tensor.  
And also the make\_model function receives only the flattened obs\_space.

I am using the dreamer algorithm, if that helps.

---

<div class="post-metadata">

**Author:** ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)\
**Post date:** [January 25, 2021, 8:27am UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541/4 "2021-01-25T08:27:42Z")

</div>

Hmm, ok, could you try the following then inside your model code?

```auto
from ray.rllib.models.modelv2 import restore_original_dimensions

# inside your model's forward:
original_obs = restore_original_dimensions(input_dict["obs"], self.ob_space.original_space, "[tf|torch]")

```

---

<div class="post-metadata">

**Author:** ![alex\_koch](https://avatars.discourse-cdn.com/v4/letter/a/57b2e6/32.png) [@alex\_koch](https://discuss.ray.io/u/alex_koch)\
**Post date:** [January 25, 2021, 10:00am UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541/5 "2021-01-25T10:00:39Z")

</div>

The function seems to expect self.obs\_space and not self.obs\_space.original\_space.  
But with that change the code works. I can recover the original obs.

The problem is that the ‘forward’ function in the DreamerModel is called ‘policy’. That’s why the restore\_original\_dimensions is not automatically called

---

<div class="post-metadata">

**Author:** ![sven1977](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/sven1977/32/53_2.png) [@sven1977](https://discuss.ray.io/u/sven1977)\
**Post date:** [January 25, 2021, 10:57am UTC](https://discuss.ray.io/t/dict-observation-space-flattened/541/6 "2021-01-25T10:57:14Z")

</div>

Got it. Makes sense. Thanks for reporting back @alex_koch !
