Recommended Free Tools
torch.cat joins tensors along an axis they already share, keeping the same number of dimensions. The inputs must match in every other dimension; their sizes along the selected axis are added together. Use torch.stack instead when you need to create a new axis.
What does torch.cat do?
torch.cat(tensors, dim=0, *, out=None) concatenates a non-empty sequence of tensors along the dimension you choose. If you omit dim, PyTorch uses dimension 0. The operation uses an existing axis, so the output has the same rank—the same number of dimensions—as each input. See the official torch.cat reference.
As an Amazon Associate I earn from qualifying purchases.
Tensor order is preserved: along the chosen axis, values from the first tensor come before values from the next. The axis’s length becomes the sum of the input lengths on that axis.
The Tool Desk
Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →How do I predict the output shape?
Keep every dimension unchanged except the one named by dim; add the inputs’ sizes on that dimension. For example, two tensors with shape (2, 3) produce different results depending on the axis:
#1 Best Overall
| Call | Result shape | What changes |
|---|---|---|
torch.cat((a, b), dim=0) |
(4, 3) |
The first dimension grows from 2 to 4. |
torch.cat((a, b), dim=1) |
(2, 6) |
The second dimension grows from 3 to 6. |
For two-dimensional tensors, dimension 0 is often described as rows and dimension 1 as columns. For higher-rank tensors, names such as batch or channel depend on how your application arranges its data; torch.cat itself does not assign those meanings.
Why do my tensor shapes have to match?
All inputs must have the same shape except along the concatenation dimension. For example, tensors shaped (2, 3) and (2, 4) can be concatenated on dimension 1, producing (2, 7). They cannot be concatenated on dimension 0 because their sizes on dimension 1 differ.
Rank #2
Before calling cat, check that the tensors have the same rank and that every non-concatenated dimension matches. The documented exception is a one-dimensional empty tensor with shape (0,), which may be included with otherwise compatible tensors. Concatenation does not automatically pad or reshape inputs; change a shape only if that transformation fits the meaning of your data.
How do I concatenate a list?
Pass a non-empty sequence, such as a list or tuple, as the first argument. Choose dim explicitly when you do not want the default axis:
Rank #3
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])
For feature matrices shaped (batch, features) with matching batch sizes, dim=1 appends features. If the intended axis is not dimension 0, omitting dim may produce a shape error or join along the wrong axis for your purpose.
When should I use torch.stack instead?
Use torch.cat to extend an existing axis. Use torch.stack when you want to insert a new axis, for example to collect same-shaped samples into a batch. The inputs to stack must all have the same size. The official torch.stack reference describes this new-dimension behavior.
Rank #4
| Operation | Axis behavior | Input shape rule | Output rank |
|---|---|---|---|
torch.cat |
Joins on an existing axis | Shapes may differ on that axis; all other dimensions must match | Same as the inputs |
torch.stack |
Inserts a new axis | All input shapes must match | One greater than the inputs |
For example, if a and b are both shaped (2, 3), torch.stack((a, b), dim=0) creates a tensor shaped (2, 2, 3). That extra leading dimension can represent the collection of samples; concatenating on dimension 0 instead would extend the original first dimension.
Can torch.cat recombine split tensors?
Yes. The PyTorch API describes cat as an inverse operation for torch.split() and torch.chunk(). Concatenating the pieces along the axis on which they were split can restore the original arrangement, provided you keep the pieces in order.
Quick Recap
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.




