Concatenation/splitting#
Sometimes, it’s necessary to concatenate multiple tensors into one tensor, or conversely, split an existing tensor into a set of separate tensors. This section explores the options in PyTorch that allow you to accomplish such tasks.
import torch
Repeat method#
The torch.Tensor.repeat
function allows you to duplicate a tensor’s values along specific dimensions, creating a new tensor with the replicated values.
For example, consider a tensor created using torch.arange(0, 2)
.
example_array = torch.arange(3)
example_array
tensor([0, 1, 2])
If you use repeat
along a nested axis (e.g., repeat(3)
), the input vector is duplicated three times, resulting in a longer vector.
example_array.repeat(3)
tensor([0, 1, 2, 0, 1, 2, 0, 1, 2])
By replicating along the rows (e.g., repeat(3, 1)
), you will create a matrix with three rows. Each row will contain a copy of the original input vector.
torch.arange(3).repeat(3, 1)
tensor([[0, 1, 2],
[0, 1, 2],
[0, 1, 2]])
Finally, let’s try to duplicate the vector three times vertically and two times in the depth dimension. This will create a tensor with three columns, each containing a duplicated version of the original vector, and two layers of these duplicated columns.
torch.arange(3).repeat(2, 3, 1)
tensor([[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]],
[[0, 1, 2],
[0, 1, 2],
[0, 1, 2]]])