How to concatenate tensors with dimension mismatch?

I have 2 tensors:

a = [Batch x T x Dim]
b = [Batch x Dim]

I want to merge the output somehow by using repeat/cat so that the output is: [Batch x T x (2.Dim)]

Example:

a: 
tensor([[[-0.2837,  0.1196,  0.5304, -1.1025],
         [-1.3174,  1.4882, -2.0489,  0.1606]],

        [[-1.1204,  2.4822, -0.6366, -0.9069],
         [ 0.3157, -1.7497, -1.0131,  1.5231]]])
B: 2, T: 2, Dim: 4

b:
tensor([[ 0.6773, -0.8552,  0.3827, -0.4008],
        [ 1.6068, -0.2427, -0.9487,  1.5622]])
B:2, Dim:4

I’d like to get something like this:

tensor([[[-0.2837,  0.1196,  0.5304, -1.1025, 0.6773, -0.8552,  0.3827, -0.4008],
         [-1.3174,  1.4882, -2.0489,  0.1606, 0.6773, -0.8552,  0.3827, -0.4008]],

        [[-1.1204,  2.4822, -0.6366, -0.9069, 1.6068, -0.2427, -0.9487,  1.5622],
         [ 0.3157, -1.7497, -1.0131,  1.5231, 1.6068, -0.2427, -0.9487,  1.5622]]])

So basically append the B x Dim to each batch element… And make sure that the whole thing is still differentiable finally.

This might work for you.

import torch
a = torch.randn(5,2,4)
b = torch.randn(5,4)
c = torch.cat([x, y.unsqueeze(1).repeat(1, x.shape[1], 1)], dim=2)
1 Like