Merge two tensor in pytorch
Tensor a:

tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])


Tensor b:

tensor([4,4,4,4])


Question 1:

How to merge two tensors and get result c:

tensor([[1, 2, 3, 4],
        [1, 2, 3, 4],
        [1, 2, 3, 4],
        [1, 2, 3, 4]])


Question 2: How to divide tensor c and get original a and b.

Question 1: Merge two tensors -   

torch.cat((a, b.unsqueeze(1)), 1)
&gt;&gt;&gt; tensor([[1, 2, 3, 4],
            [1, 2, 3, 4],
            [1, 2, 3, 4],
            [1, 2, 3, 4]])


First, we use torch.unsqueeze to add single dim in b tensor to match a dim to be concanate. Then use torch.cat Concatenates tensors a and b.

Question 2:

a = c[:][:,:-1]
a
&gt;&gt;&gt; tensor([[1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3]])

b = c[:][:,-1:].squeeze(1)
b
&gt;&gt;&gt; tensor([4, 4, 4, 4])

