Convert a 4 dims tensor to a list of 3 dims

Hi all, let’s say I have a 4 dims tensor:
inputs = tensor.randn(N, C, H, W)

I want to get:
inputs = [input_1, input_2, …, input_n]
input_i.shape = [C, H, W]
len(inputs) = N

Is there any elegant way to make this conversion? I can make it by a for loop but just feel not good.
This might be a weird conversion, but I do need it because the upper level of my project needs it.
Thank you for all for any suggestion!

Using a list comprehension is probably faster: inputs = [i for i in inputs]

However, since Pytorch tensors can virtually be used as lists would (iterating, item assignment… I think), I’m really curious about your requirements. Can you reveal more about the “upper level” or is this information too sensitive?

1 Like

Hi Alex, thanks for the answer!
It is because the upper level will zip() my inputs with another list. But I think you are right! I have never thought that zip() could directly work on a tensor (indeed it works)! Thanks a lot!

1 Like