Torchrl - Error by creating an environment with batch_size

archbold: <p>Hello there!,</p>
<p>I have a question about torchrl. I did the tutorial ‘Pendulum: Writing your environment and transforms with TorchRL → <a href="https://pytorch.org/rl/tutorials/pendulum.html" class="inline-onebox" rel="noopener nofollow ugc">Pendulum: Writing your environment and transforms with TorchRL — torchrl main documentation</a>’ and noticed that it is possible to initialize many environments in parallel in a rollout object but for that it is necessary to define the batch_size (= number of environments) inside a rollout object. Is it possible to define the batch_size for the environment itself? I would appreciate your help!</p>
<p>*In the tutorial it looks like this:</p>
<p>'# Executing a rollut with a batch of data requires us to reset the environment out of the rollout function,</p>
<h1><a name="since-we-need-to-define-the-batch_size-dynamically-and-this-is-not-supported-by-rollout-1" class="anchor" href="#since-we-need-to-define-the-batch_size-dynamically-and-this-is-not-supported-by-rollout-1"></a>since we need to define the batch_size dynamically and this is not supported by ‘rollout()’</h1>
<p>rollout = env.rollout(<br>
47,<br>
auto_reset=False, # we’re executing the reset out of the ‘rollout’ call<br>
tensordict=env.reset(env.gen_params(batch_size=[batch_size])),<br>
)<br>
print(“rollout of len 3 (batch size of 10):”, rollout)’</p>
<ul>
<li>and I want something like this (and obtain the same results as above):</li>
</ul>
<p>‘env.batch_size = [batch_size])<br>
env.rollout(47)’</p>
<ul>
<li>I get the following error:</li>
</ul>
<p>'ValueError                                Traceback (most recent call last)<br>
Cell In[8], line 3<br>
1 env = PendulumEnv(device=“cpu”)<br>
2 check_env_specs(env)<br>
----&gt; 3 env.batch_size = [7]</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torchrl/envs/common.py:397, in EnvBase.<strong>setattr</strong>(self, key, value)<br>
384 if key in (<br>
385     “_input_spec”,<br>
386     “_observation_spec”,<br>
(…)<br>
391     “_done_spec”,<br>
392 ):<br>
393     raise AttributeError(<br>
394         “To set an environment spec, please use <code>env.observation_spec = obs_spec</code> (without the leading”<br>
395         " underscore)."<br>
396     )<br>
 → 397 return super().<strong>setattr</strong>(key, value)</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torch/nn/modules/module.py:1747, in Module.<strong>setattr</strong>(self, name, value)<br>
1745     buffers[name] = value<br>
1746 else:<br>
 → 1747     super().<strong>setattr</strong>(name, value)</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torchrl/envs/common.py:442, in EnvBase.batch_size(self, value)<br>
437 if (<br>
438     hasattr(self, “output_spec”)<br>
439     and self.output_spec.shape[: len(value)] != value<br>
440 ):<br>
441     self.output_spec.unlock_()<br>
 → 442     self.output_spec.shape = value<br>
443     self.output_spec.lock_()<br>
444 if hasattr(self, “input_spec”) and self.input_spec.shape[: len(value)] != value:</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torchrl/data/tensor_specs.py:605, in TensorSpec.<strong>setattr</strong>(self, key, value)<br>
603 if key == “shape”:<br>
604     value = torch.Size(value)<br>
 → 605 super().<strong>setattr</strong>(key, value)</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torchrl/data/tensor_specs.py:3334, in CompositeSpec.shape(self, value)<br>
3332 if isinstance(spec, CompositeSpec):<br>
3333     if spec.shape[: len(value)] != value:<br>
 → 3334         spec.shape = value<br>
3335 elif spec is not None:<br>
3336     if spec.shape[: len(value)] != value:</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torchrl/data/tensor_specs.py:605, in TensorSpec.<strong>setattr</strong>(self, key, value)<br>
603 if key == “shape”:<br>
604     value = torch.Size(value)<br>
 → 605 super().<strong>setattr</strong>(key, value)</p>
<p>File ~/miniconda3/envs/vmas/lib/python3.9/site-packages/torchrl/data/tensor_specs.py:3337, in CompositeSpec.shape(self, value)<br>
3335     elif spec is not None:<br>
3336         if spec.shape[: len(value)] != value:<br>
 → 3337             raise ValueError(<br>
3338                 f"The shape of the spec and the CompositeSpec mismatch during shape resetting: the "<br>
3339                 f"{self.ndim} first dimensions should match but got self[‘{key}’].shape={spec.shape} and "<br>
3340                 f"CompositeSpec.shape={self.shape}."<br>
3341             )<br>
3342 self._shape = torch.Size(value)</p>
<p>ValueError: The shape of the spec and the CompositeSpec mismatch during shape resetting: the 0 first dimensions should match but got self[‘th’].shape=torch.Size(<span class="chcklst-box fa fa-square-o fa-fw"></span>) and CompositeSpec.shape=torch.Size(<span class="chcklst-box fa fa-square-o fa-fw"></span>).’</p>