2

I have a list of embeddings. The list has N lists with M embedding (tensors) each.

list_embd = [[M embeddings], [M embeddings], ...]

(Each embedding is a tensor with size (1,512))

What I want to do is create a tensor size (N, M), where each "cell" is one embedding.

Tried this for numpy array.

array = np.zeros(n,m)

for i in range(n):
    for j in range(m):
        array[i, j] = list_embd[i][j]

But still got errors.

In pytorch tried to concat all M embeddings into one tensor size (1, M), and then concat all rows. But when I concat along dim 1 two of those M embeddings, I get a tensor shaped (1, 1028) instead (1, 2).

final = torch.tensor([])
    for i in range(n):
        interm = torch.tensor([])
        for j in range(m):
            interm = torch.cat((interm, list_embd[i][j]), 0)
        final = = torch.cat((final, interm), 1)

Any ideas or suggestions? I need a matrix with the embeddings in each cell.

  • Does this answer your question? [Convert a list of tensors to tensors of tensors pytorch](https://stackoverflow.com/questions/61359162/convert-a-list-of-tensors-to-tensors-of-tensors-pytorch) – iacob Jun 03 '21 at 07:43

1 Answers1

1

You can use torch.cat and torch.stack to create a final 3D tensor of shape (N, M, 512):

final = torch.stack([torch.cat(sub_list, dim=0) for sub_list in list_embd], dim=0)

First, you use torch.cat to create a list of N 2D tensors of shape (M, 512) from each list of M embeddings. Then torch.stack is used to stack these N 2D matrices into a single 3D tensor final.

Shai
  • 111,146
  • 38
  • 238
  • 371