# How to disable flattened Dict or Tuple observation in ComplexInputNetwork

**URL:** <https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329>\
**Category:** RLlib\
**Created:** [May 28, 2021, 8:41pm UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329 "2021-05-28T20:41:34Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![robot-xyh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/robot-xyh/32/1048_2.png) [@robot-xyh](https://discuss.ray.io/u/robot-xyh)\
**Post date:** [May 28, 2021, 8:41pm UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329/1 "2021-05-28T20:41:34Z")

</div>

I was doing the “A multi-input capable model for Tuple observation spaces (for PPO)” example and encountered a problem.  
The observation in the environment is initialized to

> ```
> from gym.spaces import Tuple as tp
> obs_spaces = {
> "actor1": tp((
> Box(float("-inf"), float("inf"), (84, 84, 1)),
> Discrete(7),
> )),
> }
> 
> ```

The return value of the observation is

> ```
> #image: (84,84,1)
> #a:int
> return {"actor1": (image,a)}
> 
> ```

The function I use is [https://github.com/ray-project/ray/blob/master/rllib/models/torch/complex\_input\_net.py](https://github.com/ray-project/ray/blob/master/rllib/models/torch/complex_input_net.py).  
Training file

> class ComplexInputNetwork(TorchModelV2, nn.Module):  
> def **init** (self, obs\_space, action\_space, num\_outputs, model\_config, name):  
> def forward(self, input\_dict, state, seq\_lens):  
> def value\_function(self):  
> . . . . . . . .  
> ModelCatalog.register\_custom\_model(“testmodel”, ComplexInputNetwork)  
> “model”: {  
> “custom\_model”: “testmodel”,  
> # Extra kwargs to be passed to your model’s c’tor.  
> “custom\_model\_config”: {},  
> },  
> Policy is set to PPO  
> The result of the operation is

> File “C:\ProgramData\Anaconda3\Lib\site-packages\ray\rllib\examples\b\_comlex.py”, line 250, in forward  
> cnn\_out, \_ = self.cnns[i]({“obs”: component})  
> File “C:\ProgramData\Anaconda3\lib\site-packages\ray\rllib\models\modelv2.py”, line 213, in **call**  
> res = self.forward(restored, state or , seq\_lens)  
> File “C:\ProgramData\Anaconda3\lib\site-packages\ray\rllib\models\torch\visionnet.py”, line 192, in forward  
> self.\_features = self.\_features.permute(0, 3, 1, 2)  
> RuntimeError: number of dims don’t match in permute

I printed some of the results and found that obs was paved in` ComplexInputNetwork.forward.` `Hasattr(obs_space, "original_space")` is `True` in ComplexInputNetwork.init. But in `forward(self, input_dict, state, seq_lens)`, it called [https://github.com/ray-project/ray/blob/master/rllib/models/modelv2.py](https://github.com/ray-project/ray/blob/master/rllib/models/modelv2.py) was paved, resulting in progress

> ```
> def forward(self, input_dict, state, seq_lens):
> # Push image observations through our CNNs.
> outs = []
> for i, component in enumerate(input_dict["obs"]):
> if i in self.cnns:
> cnn_out, _ = self.cnns[i]({"obs": component})
> outs.append(cnn_out)
> elif i in self.one_hot:
> if component.dtype in [torch.int32, torch.int64, torch.uint8]:
> outs.append(
> one_hot(component, self.original_space.spaces[i]))
> else:
> outs.append(component)
> else:
> outs.append(torch.reshape(component, [-1, self.flatten[i]]))
> # Concat all outputs and the non-image inputs.
> out = torch.cat(outs, dim=1)
> # Push through (optional) FC-stack (this may be an empty stack).
> out, _ = self.post_fc_stack({"obs": out}, [], None)
> 
> ```

, The input dimension is  
torch.Size([7057])  
#84_84_1+1=7057  
How can I set up so that the input is not paved and kept as original ([84,84,1],1)  
Thank you very much

---

<div class="post-metadata">

**Author:** ![kai](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/kai/32/3380_2.png) [@kai](https://discuss.ray.io/u/kai)\
**Post date:** [May 30, 2021, 5:34pm UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329/2 "2021-05-30T17:34:44Z")

</div>

CC @sven1977 can you help with this?

---

<div class="post-metadata">

**Author:** ![robot-xyh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/robot-xyh/32/1048_2.png) [@robot-xyh](https://discuss.ray.io/u/robot-xyh)\
**Post date:** [May 31, 2021, 2:25am UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329/3 "2021-05-31T02:25:20Z")

</div>

Thank you very much for your help, I tried to update the version, but the error is still not resolved. The error becomes

> Expected flattened obs shape of […, 7063], got torch.Size([32, 7057])

which means I return

> #84×84×1+1=7057  
> {“actor1”: (image,a)}

the dimension of a is 1 , But the dimension of

> #84×84×1+7=7063  
> Tuple（Box(float(“-inf”), float(“inf”), (84, 84, 1)),Discrete(7)）

is7063 , so the error is now Expected flattened obs shape of […, 7063], got torch.Size([32, 7057]). After I changed Discrete to BOX, there was no error for the time being, but I still haven’t found a function for how to convert Discrete to one-hot. I am going to study the program source code carefully

---

<div class="post-metadata">

**Author:** ![smorad](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/smorad/32/272_2.png) [@smorad](https://discuss.ray.io/u/smorad)\
**Post date:** [July 5, 2021, 10:16am UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329/4 "2021-07-05T10:16:15Z")

</div>

> [@robot-xyh](#):
>
> the dimension of a is 1 , But the dimension of

Discrete spaces are automatically converted to one-hot in rllib.

---

<div class="post-metadata">

**Author:** ![robot-xyh](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/robot-xyh/32/1048_2.png) [@robot-xyh](https://discuss.ray.io/u/robot-xyh)\
**Post date:** [July 6, 2021, 1:43am UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329/5 "2021-07-06T01:43:49Z")

</div>

Thank you for your reply, I will learn the program source code again 😃

---

<div class="post-metadata">

**Author:** ![sizki](https://avatars.discourse-cdn.com/v4/letter/s/ebca7d/32.png) [@sizki](https://discuss.ray.io/u/sizki)\
**Post date:** [July 9, 2021, 12:12pm UTC](https://discuss.ray.io/t/how-to-disable-flattened-dict-or-tuple-observation-in-complexinputnetwork/2329/6 "2021-07-09T12:12:14Z")

</div>

I got the same error, its because of the discrete obs\_space. I changed it to box aswell and that helped. The reason is that discrete(7) will be preprocessed with the “OneHotPreprocessor” to 7 values, and a box value is just flattend (if its more than 1-dimensional) and stays 1.

For @sven1977 or someone from ray:  
The problem is in modelv2.py in “def \_unpack\_obs(line 385)”, bc the given “obs” variable doesn’t contain the right preprocessed obs\_space. As i saw it, during my debugging, discrete values were only counted by 1(in catalogy.py, “get\_action\_shape”, used in “get\_action\_placeholder” line 303f) not by their one-hot encoded number.
