Course contentsShow
← Machine Learning and Deep Learning
Lesson 785 of 3,53819. PyTorch FundamentalsFree lesson

Tensor Concatenation and Stacking

Combine tensors using torch.cat() for concatenation and torch.stack() for adding new dimensions.

Tensor Concatenation and Stacking

What you'll learn: How to combine multiple tensors into one using concatenation and stacking operations in PyTorch.

The Core Idea

When working with tensors, you often need to combine multiple tensors into a single tensor. PyTorch provides two primary methods for this: concatenation (torch.cat()) joins tensors along an existing dimension, while stacking (torch.stack()) creates a new dimension and aligns tensors along it.

Think of concatenation like placing books side-by-side on a shelf — they stay in the same row but extend horizontally. Stacking is like placing books on top of each other — you create a new vertical dimension.

Concatenation with torch.cat()

Concatenation merges tensors along an existing axis. The tensors must have the same shape in all dimensions except the one you're concatenating along.

import torch

a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])

# Concatenate along dimension 0 (rows)
result = torch.cat([a, b], dim=0)
# Shape: (4, 2) - stacked vertically

# Concatenate along dimension 1 (columns)
result = torch.cat([a, b], dim=1)
# Shape: (2, 4) - placed side-by-side

Stacking with torch.stack()

Stacking creates a new dimension and requires all tensors to have identical shapes.

c = torch.tensor([1, 2, 3])
d = torch.tensor([4, 5, 6])

# Stack along new dimension 0
result = torch.stack([c, d], dim=0)
# Shape: (2, 3) - creates a 2D tensor from 1D tensors

Key Takeaway: Use torch.cat() to extend tensors along existing dimensions and torch.stack() to add a new dimension while combining tensors — essential for batch processing and combining model outputs.