How to create a combination of concatenations with two tensors


#1

I have a tensor X with size (N, R) and a tensor Y with size (M, T).

I need to combine these tensors such that I get a new tensor of size (N x M, R+T) where I have concatenated dim=1 and combined dim = 0.

So for example

X = tensor([ [1,2,3], [4,5,6] ])
Y = tensor([ [7, 8], [9, 10] ] )

# where result would be
# tensor([ [1,2,3,7, 8], [1,2,3,9,10], [4,5,6,7,8], [4,5,6,9,10] ])
# also this is a special case where N == M. But I need to be able to do this with arbitrarily N and M.

I can do this with a loop but I am looking for an efficient operation to do this.

Also, X and Y are results from a neural network (nn.Module). I don’t want to break the computation graph because I need to compute the loss with result. Would creating a new tensor, looping through X and Y and simply appending the concatenations break the computation graph?


(Tumble Weed) #2

This seems to work for me:

X = torch.tensor([ [1,2,3], [4,5,6] ])
Y = torch.tensor([ [7, 8], [9, 10] ] )
X1 = X.unsqueeze(0)
Y1=Y.unsqueeze(1)
print(X1.shape,Y1.shape)
X2 = X1.repeat(Y.shape[0],1,1)
Y2 = Y1.repeat(1,X.shape[0],1)
print(X2.shape,X2.shape)
Z = torch.cat([X2,Y2],-1)
Z = Z.view(-1,z.shape[-1])
print(Z.shape)

#3

Thanks, this gives me something to work with!