Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

torch.cat joins tensors along an axis they already share, keeping the same number of dimensions. All input shapes must match except along the axis being joined, with one documented exception: a one-dimensional empty tensor of shape (0,). If you need to create a new axis instead, use torch.stack.

What does torch.cat do?

torch.cat(tensors, dim=0, *, out=None) concatenates a non-empty sequence of tensors along the dimension selected by dim. The default is dim=0. Values from each tensor appear in sequence along that axis, so the first tensor’s values come before the next tensor’s values there. The result has the same number of dimensions as each input.

Dimension names such as rows, columns, batch, and channels describe how an application uses an axis; PyTorch does not assign those meanings. Choose dim based on the layout of your own tensors.

How do I predict the output shape?

Keep every dimension unchanged except the concatenation dimension. Along that dimension, add the sizes of all input tensors. For example, tensors of shapes (2, 3) and (2, 4) can be concatenated with dim=1, producing (2, 7). They cannot be concatenated with dim=0, because their sizes on dimension 1 differ.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Joining along dimension 0

If a and b each have shape (2, 3), then torch.cat((a, b), dim=0) produces shape (4, 3). The first dimension grows from 2 to 4; the second remains 3.

Joining along dimension 1

With those same input shapes, torch.cat((a, b), dim=1) produces (2, 6). The second dimension grows from 3 to 6; the first remains 2.

For tensors shaped (batch, features), use dim=1 to append features when the batch sizes match. These labels are conventions: the shape rule, not the label, determines whether concatenation is valid.

How do I concatenate a list?

Pass a non-empty sequence, such as a list or tuple, as the first argument. For example:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import torch

a = torch.ones(2, 3)
b = torch.zeros(2, 3)
joined = torch.cat([a, b], dim=0)
print(joined.shape)  # torch.Size([4, 3])

Omitting dim selects dimension 0. Specify it explicitly when you intend to join along another axis. The official PyTorch torch.cat reference also demonstrates joining a tensor with itself along dimensions 0 and 1.

When should I use torch.stack instead?

Use torch.cat to extend an existing axis. Use torch.stack when the inputs should become entries along a newly created axis. Stacking requires all input tensors to have the same size and increases the result’s rank by one.

Operation Axis Input shape requirement Result rank
torch.cat Joins along an existing axis Shapes match except on the joined axis; a one-dimensional empty tensor of shape (0,) is also permitted by the documented exception. Same as input rank
torch.stack Inserts a new axis All input tensors must have the same size. One greater than input rank

For example, if a and b are same-shaped samples and you want an axis representing the two samples, torch.stack((a, b), dim=0) creates that axis. The PyTorch torch.stack reference documents its same-size requirement and new-dimension behavior.

Why do my tensor shapes have to match?

Concatenation only combines values along one selected axis; it does not reconcile differences on the other axes. Check that the tensors have the same rank and that every dimension other than dim has the same size. The documented exception is a one-dimensional empty tensor with shape (0,).

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • For dim=0, the dimensions after dimension 0 must match.
  • For dim=1, dimensions 0 and 2 onward must match.
  • For a different dim, every other dimension must match.

torch.cat does not automatically pad, reshape, or otherwise make incompatible tensors fit. Any shape change should reflect the meaning of your data. Padding can be appropriate for a particular task, but it is not an automatic behavior or universal fix.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

How do I recombine tensors split earlier?

The PyTorch API describes torch.cat as an inverse operation for torch.split() and torch.chunk(). To reassemble pieces, concatenate them along the same axis on which they were split, provided their other dimensions satisfy the shape requirement.

For the full signature and documented examples, see the official torch.cat API reference. A basic tensor example is also included in PyTorch’s beginner tensor tutorial.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.