# \[RLlib\] Variable-length Observation Spaces without padding

**URL:** <https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726>\
**Category:** RLlib\
**Created:** [February 4, 2021, 2:07pm UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726 "2021-02-04T14:07:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexanderb14](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@alexanderb14](https://discuss.ray.io/u/alexanderb14)\
**Post date:** [February 4, 2021, 2:07pm UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/1 "2021-02-04T14:07:01Z")

</div>

[Here](https://docs.ray.io/en/master/rllib-models.html#variable-length-complex-observation-spaces) it is statet that

> RLlib internally will insert preprocessors to insert padding for repeated elements.

I have a dynamic observation space that is quite heterogeneous. And I don’t want to let the space of the maximum sized observation dictate the size of all observations, because this would be quite inefficient. Also, I have my own model that handles the variable-sized inputs.

Is it possible to disable this padding? (or padding preprocessor)?

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [February 4, 2021, 3:19pm UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/2 "2021-02-04T15:19:35Z")

</div>

@alexanderb14 is your custom model a TF Keras model?  
I am interested in how you try to handle variable-sized inputs in your model?

BTW: I think it is helpful when you label your question “rllib”

---

<div class="post-metadata">

**Author:** ![alexanderb14](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@alexanderb14](https://discuss.ray.io/u/alexanderb14)\
**Post date:** [February 4, 2021, 4:13pm UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/3 "2021-02-04T16:13:24Z")

</div>

Thanks for the answer. I just added the label.  
My model is a custom PyTorch model with a Graph Neural Network architecture that can handle variable-sized graphs.

---

<div class="post-metadata">

**Author:** ![klausk55](https://avatars.discourse-cdn.com/v4/letter/k/f17d59/32.png) [@klausk55](https://discuss.ray.io/u/klausk55)\
**Post date:** [February 5, 2021, 9:52am UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/4 "2021-02-05T09:52:08Z")

</div>

Ah, interesting… my model is build on TF Keras and as far as I know (from last year) TF has ragged tensors to handle variable-sized data but ragged tensors were not support by a Keras model yet. I don’t know if this still holds but it also doesn’t matter for your PyTorch model 😅  
I will also try to use the repeated spaces with their automatic padding and I hope learning and efficency won’t suffer from that 🙏

Maybe it is helpful for you to take a look in the documentation for rllib.models.RepeatedValues and rllib.models.Preprocessor.

---

<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:** [February 8, 2021, 8:41am UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/5 "2021-02-08T08:41:57Z")

</div>

What @klausk55 said 🙂 !

Also, you can take a look at this example script here, which shows how to use the Repeated space as an environment’s obs space and as inputs into a simple model.

> <https://github.com/ray-project/ray/blob/master/rllib/examples/complex_struct_space.py>

As far as I remember, the “obs” goes into the model in this example as Repeated w/o padding.

---

<div class="post-metadata">

**Author:** ![alexanderb14](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@alexanderb14](https://discuss.ray.io/u/alexanderb14)\
**Post date:** [February 8, 2021, 11:06am UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/6 "2021-02-08T11:06:47Z")

</div>

Hello @sven1977 , thanks a lot for your response!

However, padding is applied in example you mentioned (actually my code is based on this example).  
For easy analysis, I printed the shape of the “items” (which is a Repeated Space) that the models gets with :  
print(input\_dict[“obs”].values[“items”].values.shape)

It is always the same shape, althought the environment passes different lenghts of items. So the padding is applied here in this example.

I tried to pass a custom hierarchy of preprocessors with a custom one for RepeatedValues (that doesn’t pad); however it always breaks things in RLlib.  
Would this be the right approach to remove the padding?

---

<div class="post-metadata">

**Author:** ![alexanderb14](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@alexanderb14](https://discuss.ray.io/u/alexanderb14)\
**Post date:** [February 8, 2021, 11:59am UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/7 "2021-02-08T11:59:15Z")

</div>

RLlib seems be very agnostic of the shape of the observations internally and does some transformations of the observations according to it.  
Is having repeated elements that don’t follow this transformation pipeline something that is not compatible with RLlib’s architecture?

---

<div class="post-metadata">

**Author:** ![alexanderb14](https://avatars.discourse-cdn.com/v4/letter/a/f04885/32.png) [@alexanderb14](https://discuss.ray.io/u/alexanderb14)\
**Post date:** [March 9, 2021, 9:06am UTC](https://discuss.ray.io/t/rllib-variable-length-observation-spaces-without-padding/726/8 "2021-03-09T09:06:30Z")

</div>

Hi, I have added rudimentary support for this for my use case. The solution is very hacky and will break other things in RLlib. However I want to share it in case anyone runs into a similar issue. Tested with the DQN and PPO Algorithms.

The version of RLlib that this patch applies to is 1.1.0.

> **[diff -ur -x '\*.pyc'...](https://pastebin.com/mqERnPuR)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
