Here is a solution if you want to index a tensor in an arbitrary dimension and select a set of tensors from that dimension (an example is say we want to compute some average of the first 3 layers):
# selecting indices arbitrarily i.e. x[*,indicies,*] were * denotes that the rest of the layers are kept the same
# but for only the last 3 layers [T, L] -> [1]
x = torch.randn(5, 4)
# compute average of first 3 layer
L = x.size(1)
indices = torch.tensor(range(L-1))
xx = torch.index_select(x, dim=1, index=indices)
print(f'{x=}')
print(f'{xx=}')
print(xx.size())
x=tensor([[-0.1588, -1.2282, -0.3142, -0.0458],
        [-0.3095, -1.1847,  1.4570, -0.1697],
        [ 0.8444,  0.6528,  0.5663,  2.4564],
        [ 0.3549,  1.8841, -0.6826,  0.0283],
        [ 0.4958,  0.0893,  2.1306, -0.4176]])
xx=tensor([[-0.1588, -1.2282, -0.3142],
        [-0.3095, -1.1847,  1.4570],
        [ 0.8444,  0.6528,  0.5663],
        [ 0.3549,  1.8841, -0.6826],
        [ 0.4958,  0.0893,  2.1306]])
torch.Size([5, 3])

the docs are fantastically clean though: torch.index_select — PyTorch master documentation 154

actually you can also do:
x.index_select(dim, index)