Imagine have n tensors. If we stay in 3D, those correspond to volumes, namely rectangular cuboids. Stacking corresponds to combining those n volumes on an additional dimension: here a 4th dimension is added to host the n 3D volumes. This operation is in clear contrast with concatenation, where the volumes would be combined on one of the existing dimensions. So concatenation of three-dimensional tensors would result in a 3D tensor.

Here is a possible representation of the stacking operations for limited dimensions sizes (up to three-dimensional inputs):

Where you chose to perform the stacking defines along which new dimension the stack will take place. In the above examples, the newly created dimension is last, hence the idea of "added dimension" makes more sense.

In the following visualization, we observe how tensors can be stacked on different axes. This in turn affects the resulting tensor shape

  • For the 1D case, for instance, it can also happen on the first axis, see below:

    With code:

    >>> x_1d = list(torch.empty(3, 2))     # 3 lines
    
    >>> torch.stack(x_1d, 0).shape         # axis=0 stacking
    torch.Size([3, 2])
    
    >>> torch.stack(x_1d, 1).shape         # axis=1 stacking
    torch.Size([2, 3])
    
  • Similarly for two-dimensional inputs:

    With code:

    >>> x_2d = list(torch.empty(3, 2, 2))   # 3 2x2-squares
    
    >>> torch.stack(x_2d, 0).shape          # axis=0 stacking
    torch.Size([3, 2, 2])
    
    >>> torch.stack(x_2d, 1).shape          # axis=1 stacking
    torch.Size([2, 3, 2])
    
    >>> torch.stack(x_2d, 2).shape          # axis=2 stacking
    torch.Size([2, 2, 3])
    

With this state of mind, you can intuitively extend the operation to n-dimensional tensors.

Answer from Ivan on Stack Overflow
🌐
DEV Community
dev.to › hyperkai › stack-in-pytorch-1bp1
stack in PyTorch - DEV Community
November 5, 2024 - Buy Me a Coffee☕ *Memos: My post explains hstack() and column_stack(). My post explains vstack()... Tagged with python, pytorch, stack, function.
🌐
Torch for R
torch.mlverse.org › docs › reference › torch_vstack
Vstack — torch_vstack • torch
Stack tensors in sequence vertically (row wise) · This is equivalent to concatenation along the first axis after all 1-D tensors have been reshaped by torch_atleast_2d()
Top answer
1 of 3
20

Imagine have n tensors. If we stay in 3D, those correspond to volumes, namely rectangular cuboids. Stacking corresponds to combining those n volumes on an additional dimension: here a 4th dimension is added to host the n 3D volumes. This operation is in clear contrast with concatenation, where the volumes would be combined on one of the existing dimensions. So concatenation of three-dimensional tensors would result in a 3D tensor.

Here is a possible representation of the stacking operations for limited dimensions sizes (up to three-dimensional inputs):

Where you chose to perform the stacking defines along which new dimension the stack will take place. In the above examples, the newly created dimension is last, hence the idea of "added dimension" makes more sense.

In the following visualization, we observe how tensors can be stacked on different axes. This in turn affects the resulting tensor shape

  • For the 1D case, for instance, it can also happen on the first axis, see below:

    With code:

    >>> x_1d = list(torch.empty(3, 2))     # 3 lines
    
    >>> torch.stack(x_1d, 0).shape         # axis=0 stacking
    torch.Size([3, 2])
    
    >>> torch.stack(x_1d, 1).shape         # axis=1 stacking
    torch.Size([2, 3])
    
  • Similarly for two-dimensional inputs:

    With code:

    >>> x_2d = list(torch.empty(3, 2, 2))   # 3 2x2-squares
    
    >>> torch.stack(x_2d, 0).shape          # axis=0 stacking
    torch.Size([3, 2, 2])
    
    >>> torch.stack(x_2d, 1).shape          # axis=1 stacking
    torch.Size([2, 3, 2])
    
    >>> torch.stack(x_2d, 2).shape          # axis=2 stacking
    torch.Size([2, 2, 3])
    

With this state of mind, you can intuitively extend the operation to n-dimensional tensors.

2 of 3
3

Very simple! I will use 4 varibles for this example. The function torch.stack is very similar to numpy (vstack and hstack). Example :

t1 = torch.tensor([1,2,3])
t2 = torch.tensor([4,5,6])
t3 = torch.tensor([7,8,9])
t4 = torch.tensor([10,11,12])

if you try this cmd

>> torch.stack((t1,t2,t3,t4),dim=1).size()
>> torch.Size([3, 4])

if you change dim=1 with dim=0

>> torch.stack((t1,t2,t3,t4),dim=0).size()
>> torch.Size([4, 3])

In first case you have a tensor with 3x4 dimensional but in last case you have a 4x3 tensor. Test this code without using .size() !

>> torch.stack((t1,t2,t3,t4),dim=1)
>> tensor([[ 1,  4,  7, 10],
        [ 2,  5,  8, 11],
        [ 3,  6,  9, 12]])

>> torch.stack((t1,t2,t3,t4),dim=0)
>> tensor([[ 1,  2,  3],
        [ 4,  5,  6],
        [ 7,  8,  9],
        [10, 11, 12]])

Happy Coding!

🌐
GitHub
github.com › mrdbourke › pytorch-deep-learning › discussions › 813
Udemy - Section 2 - Lecture 32: Differences between `torch.stack`, `torch.vstack` and `torch.hstack` · mrdbourke/pytorch-deep-learning · Discussion #813
Hi, I just wanted to mention that in the lecture video in Section 2 - Lecture 32, timestamp 3:28, the following is said: Okay, so maybe not stack or V stack. We can just define what dimension we'd ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › deep learning › difference-between-torchstack-and-torchcat-functions
What's the Difference Between torch.stack() and torch.cat() Functions? - GeeksforGeeks
July 23, 2025 - Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pytorch-stack-method
Python PyTorch stack() method - GeeksforGeeks
February 28, 2022 - Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
🌐
Whytorch
whytorch.org › torch.stack
torch.stack | WhyTorch
stack joins a sequence of tensors along a new dimension dim · All tensors must have the same shape. The result has one more dimension than the input tensors
🌐
Medium
shubamsumbria.medium.com › pytorch-functions-ad32fda9293d
5 PyTorch Functions. A short introduction about PyTorch and… | by Shubam Sumbria | Medium
January 19, 2021 - A short introduction about PyTorch and its functions(cat, split, hstack, vstack, transpose). “PyTorch Functions” is published by Shubam Sumbria.
🌐
GitHub
github.com › torchstack
TorchStack - Overview
TorchStack has one repository available. Follow their code on GitHub.
🌐
DEV Community
dev.to › hyperkai › hstack-and-columnstack-in-pytorch-2mfb
hstack and column_stack in PyTorch - DEV Community
November 5, 2024 - Buy Me a Coffee☕ *Memos: My post explains stack(). My post explains vstack() and dstack(). My... Tagged with python, pytorch, hstack, columnstack.
🌐
GitHub
alband.github.io › doc_view › generated › torch.stack.html
torch.stack — PyTorch master documentation
Concatenates a sequence of tensors along a new dimension · All tensors need to be of the same size
🌐
Medium
medium.com › @garybao › understanding-torch-stack-in-pytorch-when-to-use-and-when-not-to-af3ce8711967
Understanding torch.stack() in PyTorch: When to Use and When Not To | by Gary Bao | Medium
June 24, 2025 - Understanding torch.stack() in PyTorch: When to Use and When Not To PyTorch is the go-to deep learning framework for many researchers and practitioners. One of its key features is flexible tensor …
🌐
Glaringlee
glaringlee.github.io › generated › torch.stack.html
torch.stack — PyTorch master documentation
Concatenates sequence of tensors along a new dimension · All tensors need to be of the same size
🌐
YouTube
youtube.com › watch
Stack vs Concat in PyTorch, TensorFlow & NumPy - Deep Learning Tensor Ops - YouTube
Welcome to this neural network programming series. In this episode, we will dissect the difference between concatenating and stacking tensors together. We'll...
Published: July 18, 2019