2

Pytorch provides API to concatenate tensors, like cat, stack. But does it provide any API to concatenate pytorch tensors alternatively?

For example,enter image description here

suppose input1.shape = C*H*W, a1.shape = H\*W, and output.shape = (3C)*H*W

This can be achieved using a loop, but I am wondering if any Pytorch API can do this

2
  • Where is a1 in your illustrative example? Commented Mar 26, 2022 at 14:02
  • a1 = the first line of input1. Forgot to add this. Commented Mar 27, 2022 at 6:45

1 Answer 1

1

I will try to do it with small example:

input1 = torch.full((3, 3), 1)
input2 = torch.full((3, 3), 2)
input3 = torch.full((3, 3), 3)

out = torch.concat((input1,input2, input3)).T.flatten()
torch.stack(torch.split(out, 3), dim=1).reshape(3,-1)

#output

tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3]])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.