ValueError: The shape of the spec and the CompositeSpec mismatch during shape resetting: the 1 first dimensions should match but got self['accuracy'].shape=torch.Size([1, 1]) and CompositeSpec.shape=torch.Size([1])

Xemorr: <pre data-code-wrap="from"><code class="lang-from">from poke_env import RandomPlayer
from torchrl.envs import GymWrapper
opponent = RandomPlayer(battle_format="gen9randombattle")
train_env = SimpleRLPlayer(
    battle_format="gen9randombattle", opponent=opponent, start_challenging=True
)

base_env = GymWrapper(train_env, device=device)
check_env_specs(train_env)
</code></pre>
<pre><code class="lang-auto">ValueError                                Traceback (most recent call last)
Cell In[9], line 8
      4 opponent = RandomPlayer(battle_format="gen9randombattle")
      5 train_env = SimpleRLPlayer(
      6     battle_format="gen9randombattle", opponent=opponent, start_challenging=True
      7 )
----&gt; 8 base_env = GymWrapper(train_env, device=device)
      9 check_env_specs(train_env)

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\libs\gym.py:571, in _AsyncMeta.__call__(cls, *args, **kwargs)
    570 def __call__(cls, *args, **kwargs):
--&gt; 571     instance: GymWrapper = super().__call__(*args, **kwargs)
    573     # before gym 0.22, there was no final_observation
    574     if instance._is_batched:

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\common.py:176, in _EnvPostInit.__call__(cls, *args, **kwargs)
    174 auto_reset = kwargs.pop("auto_reset", False)
    175 auto_reset_replace = kwargs.pop("auto_reset_replace", True)
--&gt; 176 instance: EnvBase = super().__call__(*args, **kwargs)
    177 # we create the done spec by adding a done/terminated entry if one is missing
    178 instance._create_done_specs()

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\libs\gym.py:782, in GymWrapper.__init__(self, env, categorical_action_encoding, **kwargs)
    780     with set_gym_backend(libname):
    781         kwargs["env"] = env
--&gt; 782         super().__init__(**kwargs)
    783 else:
    784     super().__init__(**kwargs)

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\common.py:3048, in _EnvWrapper.__init__(self, device, batch_size, allow_done_after_reset, *args, **kwargs)
   3046 self._check_kwargs(kwargs)
   3047 self._env = self._build_env(**kwargs)  # writes the self._env attribute
-&gt; 3048 self._make_specs(self._env)  # writes the self._env attribute
   3049 self.is_closed = False
   3050 self._init_env()

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\libs\gym.py:1010, in GymWrapper._make_specs(self, env, batch_size)
   1004 cur_batch_size = self.batch_size if batch_size is None else torch.Size([])
   1005 action_spec = _gym_to_torchrl_spec_transform(
   1006     env.action_space,
   1007     device=self.device,
   1008     categorical_action_encoding=self._categorical_action_encoding,
   1009 )
-&gt; 1010 observation_spec = _gym_to_torchrl_spec_transform(
   1011     env.observation_space,
   1012     device=self.device,
   1013     categorical_action_encoding=self._categorical_action_encoding,
   1014 )
   1015 if not isinstance(observation_spec, CompositeSpec):
   1016     if self.from_pixels:

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\libs\gym.py:373, in _gym_to_torchrl_spec_transform(spec, dtype, device, categorical_action_encoding, remap_state_to_observation, batch_size)
    371     return CompositeSpec(spec_out, device=device)
    372 elif isinstance(spec, gym_spaces.dict.Dict):
--&gt; 373     return _gym_to_torchrl_spec_transform(
    374         spec.spaces,
    375         device=device,
    376         categorical_action_encoding=categorical_action_encoding,
    377         remap_state_to_observation=remap_state_to_observation,
    378     )
    379 else:
    380     raise NotImplementedError(
    381         f"spec of type {type(spec).__name__} is currently unaccounted for"
    382     )

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\libs\gym.py:364, in _gym_to_torchrl_spec_transform(spec, dtype, device, categorical_action_encoding, remap_state_to_observation, batch_size)
    354     if (
    355         remap_state_to_observation
    356         and k == "state"
   (...)
    361         # naming it 'state' will result in envs that have a different name for the state vector
    362         # when queried with and without pixels
    363         key = "observation"
--&gt; 364     spec_out[key] = _gym_to_torchrl_spec_transform(
    365         spec[k],
    366         device=device,
    367         categorical_action_encoding=categorical_action_encoding,
    368         remap_state_to_observation=remap_state_to_observation,
    369     )
    370 # the batch-size must be set later
    371 return CompositeSpec(spec_out, device=device)

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\envs\libs\gym.py:292, in _gym_to_torchrl_spec_transform(spec, dtype, device, categorical_action_encoding, remap_state_to_observation, batch_size)
    290     out = _gym_to_torchrl_spec_transform(space, device=device, dtype=dtype)
    291     out = out.unsqueeze(0)
--&gt; 292     out.make_neg_dim(0)
    293     return out
    294 elif isinstance(spec, gym_spaces.multi_discrete.MultiDiscrete):

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\data\tensor_specs.py:691, in TensorSpec.make_neg_dim(self, dim)
    689 if dim &lt; 0 or dim &gt; self.ndim - 1:
    690     raise ValueError(f"dim={dim} is out of bound for ndim={self.ndim}")
--&gt; 691 self.shape = torch.Size(
    692     [s if i != dim else -1 for i, s in enumerate(self.shape)]
    693 )

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\data\tensor_specs.py:608, in TensorSpec.__setattr__(self, key, value)
    606 if key == "shape":
    607     value = torch.Size(value)
--&gt; 608 super().__setattr__(key, value)

File F:\Holder\ShowdownBot\.venv\Lib\site-packages\torchrl\data\tensor_specs.py:3870, in CompositeSpec.shape(self, value)
   3868     elif spec is not None:
   3869         if spec.shape[: len(value)] != value:
-&gt; 3870             raise ValueError(
   3871                 f"The shape of the spec and the CompositeSpec mismatch during shape resetting: the "
   3872                 f"{self.ndim} first dimensions should match but got self['{key}'].shape={spec.shape} and "
   3873                 f"CompositeSpec.shape={self.shape}."
   3874             )
   3875 self._shape = torch.Size(value)

ValueError: The shape of the spec and the CompositeSpec mismatch during shape resetting: the 1 first dimensions should match but got self['accuracy'].shape=torch.Size([1, 1]) and CompositeSpec.shape=torch.Size([1]).
</code></pre>
<p>I receive this error when trying to use poke-env together with torch-rl. I can’t work out how to fix this issue at all, would love some help. SimpleRLPlayer extends OpenAIGymEnv for some context.</p>

vmoens: <p>This error could be made more explicit!<br>
Can you tell me what would be the specs of your env and how you build them? It looks like there’s a mismatch between the specs and the shape of the “accuracy”</p>