Chapter 3: PyTorch Tensors

Concatenating tensors

face Luca Grillotti

torch.cat concatenates tensors along the specified dimension.

Concatenating tensors along dimension 0

import torch
t1 = torch.randn(size=(1, 4))
t2 = torch.randn(size=(2, 4))
concatenation = torch.cat(tensors=(t1, t2), dim=0)

print(concatenation)
print(concatenation.size())

produces this kind of output:

tensor([[-1.2413,  0.1362,  0.9370,  2.1812],
        [ 0.5601,  0.0252,  0.4164, -0.6447],
        [-0.4758, -0.2737, -0.0152,  1.5531]])
torch.Size([3, 4])

Note: the code above runs as all tensors have the same size on dimensions other than 0. If t1 was of size (1, 3) instead of (1, 4), the code will not run.

Concatenating tensors along dimension 1

import torch
t1 = torch.randn(size=(3, 1))
t2 = torch.randn(size=(3, 5))
concatenation = torch.cat(tensors=(t1, t2), dim=1)

print(concatenation)
print(concatenation.size())

gives an output similar to this:

tensor([[-0.1497,  0.0853, -0.6608, -1.1509,  0.3870,  0.2287],
        [ 0.3432,  0.6032,  0.0454, -0.3627, -0.6101,  1.1735],
        [ 0.3677, -1.5225, -0.0834,  0.6458,  0.9340,  0.0303]])
torch.Size([3, 6])