Reputation: 659
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.
Upvotes: 1
Views: 1237
Reputation: 24049
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])
Upvotes: 1