Concat Two Tensors

I want to concat two tensors of size a: torch.Size([16, 1]) and b: torch.Size([16, 120]) to be of size torch.Size([16, 121])

could you please help with that?

2

1 Answer

Here you can use the torch.cat() function.

Example:

>>> a = torch.rand([16,1])
>>> b = torch.rand([16,120])
>>> a.size()
torch.Size([16, 1])
>>> b.size()
torch.Size([16, 120])
>>> c = torch.cat((a,b),dim=1)
>>> c.size()
torch.Size([16, 121])

What you want to do is to concatenate the tensors on the first dimension (dim=1).

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest