Extracting tensor content of the same index over multiple dimensions

Newbie here.

Wondering if anyone knows how to efficiently iterate over a tensor of size 128xAxB? (Where A and B vary).

I’d specifically like to extract the individual values of each index from across dimensions, resulting in a set of 128-element tensors which maintain the order of the original input.

E.g. tensor[0] would extract values in the 0 index across all 128 dimensions, giving a tensor of 128 0-index values. tensor [1] would extract all values in the index 1 across all 128 dimensions, giving another 128-element tensor, and so on.

1 Like

Ah. Good old slicing does the trick:

myTensor = torch.randn(128, 56, 99) #128-dimensional tensor
sliced = myTensor[:, i, j]

Increment i and j and you’re good to go.

Sometimes you have to type these things out.