Reputation: 21
I want to convert a list of list to torch.LongTensor.
The element in a list of sequence means embedding index, and each list has different size.
For example,
tmp = [[7, 1], [8, 4, 0], [9]]
tmp = torch.LongTensor(tmp)
This occrus TypeError: not a sequence
How can I convert different sizes of a list in list to torch Tensor?
Upvotes: 1
Views: 1121
Reputation: 875
This will solve your issue:
tmp = torch.LongTensor(list(map(lambda x: x + [0] * (max(map(len, tmp)) - len(x)), tmp)))
Explanation:
# 1
map(lambda x: x + [0] * (max(map(len, tmp)) - len(x)), tmp)
# -> [[7, 1, 0], [8, 4, 0], [9, 0, 0]]
# 2
torch.LongTensor([[7, 1, 0], [8, 4, 0], [9, 0, 0]])
# -> tensor([[7, 1, 0],
# [8, 4, 0],
# [9, 0, 0]])
Upvotes: 0
Reputation: 70
import torch
import itertools
tmp = [[7, 1], [8, 4, 0], [9]]
tmp = torch.LongTensor(list(itertools.chain(*tmp)))
If you don't need to maintain the shape, this could be help.
Upvotes: 0
Reputation: 3145
You are looking for nested tensors (see docs).
import torch
tmp = [[7, 1], [8, 4, 0], [9]]
tmp = list(map(torch.as_tensor, tmp))
tmp = tmp = torch.nested.as_nested_tensor(tmp, dtype=torch.long)
tmp
>>> nested_tensor([
tensor([7, 1]),
tensor([8, 4, 0]),
tensor([9])
])
Alternatively, you can also pad the tensor to the same length:
tmp = torch.nested.to_padded_tensor(tmp, 0).long()
tmp
>>> tensor([
[7, 1, 0],
[8, 4, 0],
[9, 0, 0]
])
Upvotes: 3