If you've been poking around in machine learning, you've probably bumped into tensors. They're basically fancy arrays that hold your data in a format a neural network can chew on. And if you're working with PyTorch, sooner or later you'll want to stitch tensors together. That's where torch.cat() comes in.
This guide breaks down the PyTorch cat function—how it works, what parameters it takes, and how to use it without pulling your hair out. Whether you're building a model on your laptop or running inference on an Android device, the same principles apply.
At a Glance
- Torch.cat() combines two or more tensors along a specified dimension
- All input tensors must share the same shape, except along the dimension you're concatenating
- The dimension parameter (dim) controls whether tensors stack in one direction or another
- The function works with 1D, 2D, and 3D tensors alike
- An optional output tensor can be specified via the "out" parameter
How torch.cat() Works
When you call torch.cat(), you're telling PyTorch to take a sequence of tensors and smoosh them together along one axis. Think of it like stacking cards: you can pile them up or line them side by side. The dimension parameter decides which direction you're stacking.
Here's the basic syntax, according to codeunderscored.com:
Torch.cat((tensor_object1, tensor_object2, ……), dim)
The function accepts at least two tensors as its first argument, wrapped in a tuple. The second argument—dim—tells PyTorch which axis to merge along.
The Parameters That Matter
Three things go into torch.cat():
- Tensors: A sequence of two or more tensors. They all need to be the same type, and they must match in every dimension except the one you're concatenating on.
- Dim: The axis along which the merge happens. Set it to 0 and tensors combine along the first axis. Set it to 1 or -1 and they combine along the second axis (or the last, for higher dimensions).
- Out: Optional. If you want the result dropped into a specific tensor variable instead of a new one, you can specify it here. Defaults to None.
Say you're working with two 2D tensors and you want to attach one to the other. You'd pass dim=0. If you wanted them combined in the other direction instead, dim=1 or dim=-1 would do the trick.
Putting It Together: Code Examples
Let's walk through a practical scenario. Say you have four 1D tensors holding sensor readings from an Android app, and you want to merge them into a single dataset before feeding it into a model:
Import torch
T1 = torch.tensor([1, 2, 3])
T2 = torch.tensor([4, 5, 6])
T3 = torch.tensor([7, 8, 9])
T4 = torch.tensor([10, 11, 12])
Combined = torch.cat((t1, t2, t3, t4), dim=0)
Print(combined)
This produces one long tensor containing all twelve values in sequence.
Now, what about 2D tensors? Say you have two small arrays and you want to combine them:
Import torch
Mat_a = torch.Tensor([[1, 2], [3, 4]])
Mat_b = torch.Tensor([[5, 6], [7, 8]])
Result = torch.cat((mat_a, mat_b), -1)
Print(result)
With dim=-1, the matrices combine along the last axis, creating wider arrays.
Why This Matters for Android Devs
If you're building ML features into an Android app, you might be using PyTorch Mobile to run models directly on-device. That means your tensors need to be shaped correctly before they hit the model. Torch.cat() is an everyday tool for preparing batched input, merging feature maps, or assembling datasets on the fly.
The dimension parameter is especially handy when you're working with image data, where channels, height, and width all need to line up correctly. Mess up the dimension and your model throws a shape mismatch error faster than you can say "tensor."
Common Pitfalls to Avoid
First, make sure your tensors match everywhere except the concatenation axis. If you're merging along dim=0, every other dimension must be identical—same number of columns, same everything. Second, remember that PyTorch won't automatically broadcast your tensors; you have to handle that separately. Third, negative dimensions work exactly like in NumPy—dim=-1 always refers to the last axis, which can save you from doing math on tensor ranks you don't know offhand.
Bottom Line
Torch.cat() is straightforward once you get the dimension parameter sorted. Combine tensors along dimension 0 or dimension 1 (or -1 for the last axis), and always double-check your shapes before you concatenate. It's one of those functions you'll use in almost every PyTorch project, whether you're training on a GPU or running inference on an Android phone.
Key takeaways
- Use torch.cat() to merge tensors along a single axis without changing their other dimensions
- Set dim=0 for stacking along the first axis, dim=1 or -1 for the second or last axis
- All tensors must match in shape except along the concatenation dimension
- The function supports 1D, 2D, and 3D tensors equally well
- On Android, torch.cat() helps prepare and batch data for on-device ML models
Frequently asked questions
What does torch.cat() do in PyTorch?
It concatenates a sequence of tensors along a specified dimension, producing a single tensor that combines all inputs along that axis.
Can torch.cat() merge tensors of different sizes?
No. All tensors must have the same shape except in the dimension you're concatenating along. If they don't match, PyTorch throws a size mismatch error.
What's the difference between dim=0 and dim=-1 in torch.cat()?
Dim=0 combines tensors along the first axis, while dim=-1 combines them along the last axis. In 2D tensors, dim=1 and dim=-1 behave identically.
Is torch.cat() the same as NumPy's concatenate?
Pretty much, yes. PyTorch's implementation mirrors NumPy's in both syntax and behavior, so if you've used numpy.concatenate(), torch.cat() will feel familiar.
Can I use torch.cat() with more than two tensors?
Absolutely. You can pass as many tensors as you like in the tuple, and they'll all be concatenated in order along the specified dimension.