Append or combine column wise torch tensors of different shapes

Question:

Tensors A and B below share the row size. The 6 and 3 refers to the columns of the 2 data frame, but the B tensor has at each cell a vector of size 256.

A= torch.Size([17809, 6])
B= torch.Size([17809, 3, 256])

How do I append combine these tensors?

More detail:
A column of ‘A’ is a numeric vector like ‘Age’, In B one of the 3 columns has a set of node embeddings (vector) of size 256.

Asked By: sAguinaga

||

Answers:

You can apply torch.nn.Embedding on A to embedding numeric vector then use torch.cat to concat embeding of A and B on the axis=1.

(In the below code I use random tensors).

import torch
from torch import nn

num_embeddings = 10 # fill base Age
embedding_dim = 256 # fill base of B tensor
embedding = nn.Embedding(num_embeddings, embedding_dim)

A = torch.randint(10, (17809, 6))
print(f"A   : {A.shape}")

E_A = embedding(A)
print(f"E_A : {E_A.shape}")

B = torch.rand(17809, 3, 256)
print(f"B   : {B.shape}")

C = torch.cat((E_A, B), 1)
print(f"C   : {C.shape}")

Output:

A   : torch.Size([17809, 6])
E_A : torch.Size([17809, 6, 256])
B   : torch.Size([17809, 3, 256])
C   : torch.Size([17809, 9, 256])
Answered By: I'mahdi
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.