How to convert a list in list to torch tensor?

Question:

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?

Asked By: Dang

||

Answers:

Welcome to StackOverflow

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]])
Answered By: Jiya

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]
])
Answered By: Plagon
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.

Answered By: Lorewalker Cho
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.