Whats the most efficient way to generate a tensor like below?
[[0, 0],
[0, 1],
[0, 2],
…
[0, X],
[1, 0],
…
[1, X],
…
…
[X, X]]
Whats the most efficient way to generate a tensor like below?
[[0, 0],
[0, 1],
[0, 2],
…
[0, X],
[1, 0],
…
[1, X],
…
…
[X, X]]
Not at my computer to test, but maybe something like this?
tens_list = list()
for x in range(10):
for y in range(100):
tens_list.append((x, y))
tens = torch.tensor(tens_list, [data types if needed])
I was trying to avoid for-loops if possible.
For aesthetic or functional reasons? If aesthetics, the following is equivalent to the for loop implementation above:
tens_list = [(x, y) for x in range(10) for y in range(100)]
If functional, I’m not sure what the fastest method would be. I’d guess that creating a “template” object with something like the above and then generating a new tensor from it as needed elsewhere in your code would be a contender for “fastest”. Unfortunately, I’m pretty new at all this stuff, so I could be way off…
I mean I was thinking if I can use some builtin pytorch functions rather than looping myself.
Maybe something like this ?
l = torch.arange(X+1)
r = torch.arange(X+1)
l = l.view(X+1, 1).repeat(1, X+1).view((X+1) * (X+1))
r = r.view(1, X+1).repeat(X+1, 1).view((X+1) * (X+1))
m = torch.stack([l, r], 1)