Tensor and torch problem

AI_my_dream: <pre><code>(torch.rand(shape, generator=self.rng, device=self.device) - 0.5)
</code></pre>
<p>TypeError: only integer tensors of a single element can be converted to an index</p>

ptrblck: <p>Could you post a minimal and executable code snippet reproducing this error as it’s currently unclear which call fails?</p>

AI_my_dream: <pre><code>def _initialize_positions(self, rand_init: bool, default_x: float, default_y: float):
    """
    Returns initial x, y positions based on whether we randomly initialize or use default coordinates.
    """
    if rand_init and not self.is_eval:
        if isinstance(self.batch_size, torch.Size):
            if len(self.batch_size) == 0:
                shape = (3,)
            else:
                shape = (self.batch_size[0], 3)
        elif isinstance(self.batch_size, torch.Tensor):
            shape = (int(self.batch_size.item()), 3) if self.batch_size.numel() &gt; 0 else (3,)
        else:  # assuming it's an int
            shape = (int(self.batch_size), 3)

        positions = (
            (torch.rand(shape, generator=self.rng, device=self.device) - 0.5)
            * 2
            * self.environment_config["agent"]["max_x_y"]
        )

        x_position = positions[..., 0]
        y_position = positions[..., 1]
</code></pre>

ptrblck: <p>This code snippet is unfortunately not executable.</p>

AI_my_dream: <p>what should I do? can you guide me?</p>

AI_my_dream: <p>In my previous case, I had one position of x and y, now I have three agents, and I need to apply three coordinates of x and y. But I have the error</p>

AI_my_dream: <p>Any idea how I could handle it?</p>

ptrblck: <p>It’s unclear what exactly fails and since your code is not executable we have no way to debug it.<br>
In any case, the error could be raised by e.g. this operation:</p>
<pre data-code-wrap="python"><code class="lang-python">torch.tensor([torch.tensor(0.), torch.tensor(1.)], dtype=torch.int64)
# TypeError: only integer tensors of a single element can be converted to an index
torch.tensor([torch.tensor(0.), torch.tensor(1.)]).long() # works
</code></pre>
<p>and you could try to search for such a pattern in your code.</p>

AI_my_dream: <p>I worked on single agent and for initializing its poisitn I used this "    def _initialize_positions(self, rand_init: bool, default_x: float, default_y: float):<br>
“”"<br>
Returns initial x, y positions based on whether we randomly initialize or use default coordinates.<br>
“”"<br>
if rand_init and not self.is_eval:<br>
x_position = (<br>
(torch.rand(self.batch_size, generator=self.rng, device=self.device) - 0.5)<br>
* 2<br>
* self.environment_config[“agent”][“max_x_y”]<br>
)<br>
y_position = (<br>
(torch.rand(self.batch_size, generator=self.rng, device=self.device) - 0.5)<br>
* 2<br>
* self.environment_config[“agent”][“max_x_y”]<br>
)<br>
elif rand_init and self.is_eval:<br>
x_position = torch.tensor(self.x_position, device=self.device)<br>
y_position = torch.tensor(self.y_position, device=self.device)<br>
else:<br>
x_position = torch.tensor(default_x, device=self.device)<br>
y_position = torch.tensor(default_y, device=self.device)<br>
return x_position, y_position" and now for three positions, I want to consider x_position and   y_position three-member lists that cover the coordination of three agents. Do you have any idea for revising it to satisfy three agents positions?</p>