Convert list to torch.tensor

list_x = []
x = torch.Size([4,3,32,32])
y = torch.Size([6,3,32,32])
list_x.append(x)
list_x.append(y)
list_x = torch.tensor(list_x)

x and y have different shape of dimension 0.
In this case how to convert list to torch.tensor?

I dont think you can convert it like this .

what result (i.e. tensor shape) you expect to see after adding those two tensors?

@Tahir
Thanks for you answer.
I want to add only inputs that satisfy the conditions.
So the size(dim==0) depends on whether the inputs are satisfied or not.

list_x = []
for batch_idx, input in enumerate(data_loader):
   list_x.append(input) # save input images under some conditions

new_data_loader = DataLoader(list_x, ...)
   

In this purpose, Is there any soultions ?

Maybe… torch.cat ?

@Eta_C
Thanks for you reply.
I’m using torch.cat but I think it is too slow.

Actually, if your memory is large enough, you can try this

dataset = torch.empty_like(data_loader.dataset)
cur_ind = 0
for batch_idx, input in enumerate(data_loader):
    dataset[cur_ind:cur_ind+len(input)] = input
    cur_ind += len(input)
new_data_loader = DataLoader(dataset[:cur_ind, ...], ...)

What is data_loader.dataset ?