0

I have 2 tensors,

their format currently is [13, 2] respectively. I am trying to combine both into a 3 dimensional tensor with dimensions [2, 13, 2] such that they stack on top of each other, however are separated as batches.

here is an example of one of the tensors in format [13, 2]:

tensor([[[-1.8588,  0.3776],
         [ 0.1683,  0.2457],
         [-1.2740,  0.5683],
         [-1.7262,  0.4350],
         [-1.7262,  0.4350],
         [ 0.1683,  0.2457],
         [-1.0160,  0.5940],
         [-1.3354,  0.5565],
         [-0.7497,  0.5792],
         [-0.2024,  0.4251],
         [ 1.0791, -0.2770],
         [ 0.3032,  0.1706],
         [ 0.8681, -0.1607]])

I would like to maintain the shape, but have them in 2 groups in the same tensor. below is an example of the format I am after:

tensor([[[-1.8588,  0.3776],
         [ 0.1683,  0.2457],
         [-1.2740,  0.5683],
         [-1.7262,  0.4350],
         [-1.7262,  0.4350],
         [ 0.1683,  0.2457],
         [-1.0160,  0.5940],
         [-1.3354,  0.5565],
         [-0.7497,  0.5792],
         [-0.2024,  0.4251],
         [ 1.0791, -0.2770],
         [ 0.3032,  0.1706],
         [ 0.8681, -0.1607]],

         [[-1.8588,  0.3776],
         [ 0.1683,  0.2457],
         [-1.2740,  0.5683],
         [-1.7262,  0.4350],
         [-1.7262,  0.4350],
         [ 0.1683,  0.2457],
         [-1.0160,  0.5940],
         [-1.3354,  0.5565],
         [-0.7497,  0.5792],
         [-0.2024,  0.4251],
         [ 1.0791, -0.2770],
         [ 0.3032,  0.1706],
         [ 0.8681, -0.1607]]])

Anyone have any ideas on how to do this using concatination? I have tried using .unsqueeze when using torch.cat((a, b.unsqueeze(0)), dim=-1) however it changed the format to [13, 4, 1] which is not the format I am after.

solution below works, however, my idea was that I would keep stacking to y via a loop without being restricted by the shape. Sorry for not projecting my idea clearly enough.

They will all be of size [13,2] so it will go up in the form of [1,13,2], [2,13,2], [3,13,2], [4,13,2] etc...

Innat
  • 16,113
  • 6
  • 53
  • 101
Loai Alnouri
  • 83
  • 1
  • 1
  • 8
  • Just a clarification : you have two tensors of shape (13,2), and you want to make a tensor of size (2,26,2) out of them ? Not (2, 13, 2) nor (26, 2), you sure ? Because that means you need more than concatenation, there are just not enough numbers in your tensors to make a (2,26,2) tensor. You want to duplicate them is some way as well ? – trialNerror Apr 07 '21 at 23:16
  • ahh sorry my bad, I think I have confused myself, I will change it now but my ideal result would be [2, 13, 2], hope this makes a little more sense? – Loai Alnouri Apr 07 '21 at 23:17

1 Answers1

2

In this case, you need torch.stack rather than torch.cat, at least it's more convenient :

x1 = torch.randn(13,2)
x2 = torch.randn(13,2)
y = torch.stack([x1,x2], 0) # creates a new dimension 0
print(y.shape)
>>> (2, 13, 2)

You can indeed use unsqueeze and cat though, but you need to unsqueeze both input tensors :

x1 = torch.randn(13,2).unsqueeze(0) # shape: (1,13,2)
x2 = torch.randn(13,2).unsqueeze(0) # same
y = torch.cat([x1, x2], 0)
print(y.shape)
>>> (2,13,2)

Here is a useful thread to understand the difference : difference between cat and stack

If you need to stack more tensors, it's not really much harder, stack works on an arbitrary number of tensors:

# This list of tensors is what you will build in your loop
tensors = [torch.randn(13, 2) for i in range(10)]
# Then at the end of the loop, you stack them all together
y = torch.stack(tensors, 0)
print(y.shape)
>>> (10, 13, 2)

Or, if you don't want to use the list :

# first, build the y tensor to which the other ones will be appended
y = torch.empty(0, 13, 2)
# Then the loop, and don't forget to unsqueeze
for i in range(10):
    x = torch.randn(13, 2).unsqueeze(0)
    y = torch.cat([y, x], 0)

print(y.shape)
>>> (10, 13, 2)
trialNerror
  • 3,255
  • 7
  • 18
  • amazing the stack function worked, Thank you! – Loai Alnouri Apr 07 '21 at 23:41
  • sorry to bother again, but a follow up question, this was meant to be part of a loop such that it would not be x1 and x2, but rather a whole bunch of groups I would be adding on to y. It seems like your method only works for combining 2, is there a way to keep adding to y via a loop without being restricted by the shape? they will all be of size [13,2] so it will go up in the form of [1,13,2], [2,13,2], [3,13,2], [4,13,2] etc... – Loai Alnouri Apr 07 '21 at 23:48
  • Please accept the answer in order to close the thread then. Glad to have helped – trialNerror Apr 08 '21 at 12:14