10
votes

I have list of tensor each tensor has different size how can I convert this list of tensors into a tensor using pytroch

for more info my list contains tensors each tensor have different size for example the first tensor size is torch.Size([76080, 38])

the shape of the other tensors would differ in the second element for example the second tensor in the list is torch.Size([76080, 36])

when I use torch.tensor(x) I get an error ValueError: only one element tensors can be converted to Python scalars

2
Please provide more of your code. - Fábio Perez
for item in features: x.append(torch.tensor((item))) - Omar Abdelaziz
this gives me a list of tensors but each tensor have different size so when I try torch.stack(x) it gives me the same error @FábioPerez - Omar Abdelaziz
your question need more be detailed...where this data came from, if from image, just resize all image in the same shape, before convert to torch type. it more easier to anyone here help if you explain it more detailed in processing the data.. at least we can give strategy advise to process the data - Wahyu Bram
Ran into this problem trying torch.tensor(torch.split(sometuple)). Same applies... variable length doesn't work out. - rocksNwaves

2 Answers

11
votes

tensors cant hold variable length data. you might be looking for cat

for example, here we have a list with two tensors that have different sizes(in their last dim(dim=2)) and we want to create a larger tensor consisting of both of them, so we can use cat and create a larger tensor containing both of their data.

also note that you can't use cat with half tensors on cpu as of right now so you should convert them to float, do the concatenation and then convert back to half

import torch

a = torch.arange(8).reshape(2, 2, 2)
b = torch.arange(12).reshape(2, 2, 3)
my_list = [a, b]
my_tensor = torch.cat([a, b], dim=2)
print(my_tensor.shape) #torch.Size([2, 2, 5])

you haven't explained your goal so another option is to use pad_sequence like this:

from torch.nn.utils.rnn import pad_sequence
a = torch.ones(25, 300)
b = torch.ones(22, 300)
c = torch.ones(15, 300)
pad_sequence([a, b, c]).size() #torch.Size([25, 3, 300])

edit: in this particular case, you can use torch.cat([x.float() for x in sequence], dim=1).half()

4
votes

Tensor in pytorch isn't like List in python, which could hold variable length of objects.

In pytorch, you can transfer a fixed length array to Tensor:

>>> torch.Tensor([[1, 2], [3, 4]])
>>> tensor([[1., 2.],
            [3., 4.]])

Rather than:

>>> torch.Tensor([[1, 2], [3, 4, 5]])
>>> 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-809c707011cc> in <module>
----> 1 torch.Tensor([[1, 2], [3, 4, 5]])

ValueError: expected sequence of length 2 at dim 1 (got 3)

And it's same to torch.stack.