I have a 3D tensor and I want to iterate through the second dimension. The easiest approach is to use a for loop on the second dimension. My question is what is the most efficient/appropriate way to do it. For example:
t = torch.rand(2, 5, 3)
print(t)
tensor([[[0.1582, 0.7643, 0.1352],
[0.7157, 0.4503, 0.7935],
[0.9954, 0.6714, 0.0386],
[0.7655, 0.2867, 0.0764],
[0.2259, 0.3079, 0.8390]],
[[0.2655, 0.2578, 0.8421],
[0.7871, 0.7559, 0.2978],
[0.4458, 0.6559, 0.9398],
[0.6346, 0.4585, 0.3734],
[0.8566, 0.1053, 0.0559]]])
for idx in range(t.shape[1]):
print(t[:,idx,:])
tensor([[0.1582, 0.7643, 0.1352],
[0.2655, 0.2578, 0.8421]])
tensor([[0.7157, 0.4503, 0.7935],
[0.7871, 0.7559, 0.2978]])
tensor([[0.9954, 0.6714, 0.0386],
[0.4458, 0.6559, 0.9398]])
tensor([[0.7655, 0.2867, 0.0764],
[0.6346, 0.4585, 0.3734]])
tensor([[0.2259, 0.3079, 0.8390],
[0.8566, 0.1053, 0.0559]])