Combinatorial Concatenation

Hi,

I have tensor X of shape (a,b) and tensor Y of shape (c,d)
I would like the output tensor Z to be of shape (a,c,(b+d)), such that

Z[i][j] = [ith row of X, jth row of Y]

What is the best way to do this?

Best,
Soumyadip

Hi ,

Try the following solution.

import torch

a, b, c, d = 2, 3, 4, 4
A = torch.tensor([[10, 20, 30], [40, 50, 60]]) # a = 2, b = 3
B = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]) # c = 4 , d = 4

A = A [:, None, :]
B = B [None, :, :]

A = A.repeat_interleave(c, 1)
B = B.repeat_interleave(a, 0)

C = torch.cat((A, B), dim=2)

print(C.size())

Thanks

1 Like