Need for Improvement:How to get every index of a tensor

Forceless: <p>Suppose we have [3,2,2] matrix<br>
So, I need all index like:<br>
[[0,0,0],[0,0,1]…[2,1,1]]</p>

Matias_Vasquez: <p>If I understand correctly, you want something like this.</p>
<p>If not, please let me know.</p>
<p>Hope this helps <img src="https://discuss.pytorch.org/images/emoji/apple/blush.png?v=12" title=":blush:" class="emoji" alt=":blush:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-python">import torch
import itertools

t = torch.rand((3, 2, 2))
shape = t.shape

y = torch.tensor(list(itertools.product(range(len(shape)), repeat=len(shape))))

for i, max in enumerate(shape):
    y = y[ y[:, i] &lt; max ]

print(y)
</code></pre>
<pre><code class="lang-python"># Output:
tensor([[0, 0, 0],
        [0, 0, 1],
        [0, 1, 0],
        [0, 1, 1],
        [1, 0, 0],
        [1, 0, 1],
        [1, 1, 0],
        [1, 1, 1],
        [2, 0, 0],
        [2, 0, 1],
        [2, 1, 0],
        [2, 1, 1]])
</code></pre>

Forceless: <p>Yes, that’s what I need, you helped a lot.<br>
<img src="https://discuss.pytorch.org/images/emoji/apple/hugs.png?v=12" title=":hugs:" class="emoji only-emoji" alt=":hugs:" loading="lazy" width="20" height="20"><br>
If this process could be simpler, that’s the best.<br>
Thank you</p>

Matias_Vasquez: <p>If what you want is to access every value consecutively, then you can do something like this:</p>
<pre><code class="lang-python">t = torch.rand((3, 2, 2))

print(t)

for i in range(len(t.view(-1))):
    print(t.view(-1)[i])
</code></pre>

Forceless: <p>Yeah, I know that.<br>
Unfortunately, I can’t access these values directly.<br>
Anyway, thanks and wish you all be good.</p>

Forceless: <p><a class="mention" href="/u/matias_vasquez">@Matias_Vasquez</a><br>
Hi, I find that there might have been a bug existing,</p>
<pre><code class="lang-python">shape = [64,3,7,7]
idx = torch.tensor(list(itertools.product(range(len(shape)), repeat=len(shape))))
for i,right_b in enumerate(shape):
    idx = idx[ idx[:,i]&lt;right_b]
</code></pre>

Matias_Vasquez: <p>Oops, inside the product it has to be the maximum number, not the length</p>
<pre><code class="lang-python">shape = [64,3,7,7]
idx = torch.tensor(list(itertools.product(range(max(shape)), repeat=len(shape))))
for i,right_b in enumerate(shape):
    idx = idx[ idx[:,i]&lt;right_b]
</code></pre>

Matias_Vasquez: <p>This might be more efficient, since the other answer has to create all of the different permutations, taking the maximum in every dimension.</p>
<pre><code class="lang-python">
t = torch.rand((64, 3, 7, 7))

B, C, H, W = t.shape

y = torch.tensor([[b, c, h, w] for b in range(B) for c in range(C) for h in range(H) for w in range(W)])

print(y)
</code></pre>

Forceless: <p>Yes, but dimensions of tensor might be changed, so I need some adaptive ways to solve that.</p>

Matias_Vasquez: <p>Here should be a generic solution for <code>n</code> dimensions.</p>
<p>Not the most elegant, but also works.</p>
<pre><code class="lang-python">import torch
import numpy as np


t = torch.rand((64, 3, 7, 7))
z = torch.tensor([[0] * len(t.shape)] * np.prod(t.shape))

shape = t.shape

for i in range(-1, -1-len(shape), -1):
    if i == -1:
        z[:, i] = torch.tensor(
            [j for j in range(shape[i])] * (int)(np.prod(shape)/(np.prod(shape[i:]))) 
        )
    else:
        z[:, i] = torch.tensor(
            [j for j in range(shape[i]) for _ in range(np.prod(shape[i+1:]))] * (int)(np.prod(shape)/(np.prod(shape[i:]))) 
        )

print(z)
</code></pre>