How to parametrize dimension expansion [:, None, None], [None, None,:],...?

Hello,

I have script where I would like to reshape a tensor based on a dimension parameter dim.
Say the tensor has tensor.shape = [T].

When dim=0, this should be executed:

tensor[:,None,None] # resulting shape is [T,1,1]

when dim=1

tensor[None,:,None] # resulting shape is [1,T,1]

when dim=2

tensor[None,None,:] # resulting shape is [1,1,T]

Is there a corresponding command to do this depending on dim?
I only know unsqueeze, which only works for single dimension adding.

Thanks!
Best, JZ

Does the below snippet help, where x is your original tensor of length 100 (T in your case)?

T = 100
dim = 2
x = torch.randn(T)

newdims = [1, 1, 1]
newdims[dim] = T

x_resized = x.view(tuple(newdims))

x_resized.shape
Out: torch.Size([1, 1, 100])

nice, thats exactly what I need. I did not work with view so far, so I did not have it in mind. Still learning the vocabulary :slight_smile: Thanks!
Best, JZ