DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowHalloween DealsAmazon USHalloween week deals: useful picks before month-endAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk3 min

torch.cat in PyTorch: Join Tensors Along an Existing Dimension

torch.cat joins tensors along an existing axis and sums their sizes on that axis. Learn the shape rules, examples, and when torch.stack is the better choice.
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. 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.

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

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:

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.

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.

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

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:

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.