🌐
Data Science Weekly
datascienceweekly.org › tutorials › pytorch-stack-turn-a-list-of-pytorch-tensors-into-one-tensor
PyTorch Stack: Turn A List Of PyTorch Tensors Into One Tensor
So the default of torch.stack is that it’s going to insert a new dimension in front of the 2 here, so we’re going to end up with a 3x2x3 tensor.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pytorch-stack-method
Python PyTorch stack() method - GeeksforGeeks
February 28, 2022 - PyTorch torch.stack() method joins (concatenates) a sequence of tensors (two or more tensors) along a new dimension. It inserts new dimension and concatenates the tensors along that dimension.
🌐
Codecademy
codecademy.com › docs › pytorch › tensor operations › .stack()
PyTorch | Tensor Operations | .stack() | Codecademy
November 20, 2024 - In PyTorch, the .stack() method stacks given tensors along a specified dimension to create a combined tensor.
🌐
Medium
medium.com › data-scientists-diary › pytorchs-stack-method-a-practical-guide-for-data-scientists-2af8088f40a5
PyTorch’s stack() Method: A Practical Guide for Data Scientists
January 19, 2025 - Let’s start by creating a few identical tensors and stacking them along different dimensions. This will give you a feel for how dim impacts the output shape, which is essential when you’re preparing data for models or preprocessing pipelines. import torch # Define three 2x3 tensors tensor1 = torch.tensor([[1, 2, 3], [4, 5, 6]]) tensor2 = torch.tensor([[7, 8, 9], [10, 11, 12]]) tensor3 = torch.tensor([[13, 14, 15], [16, 17, 18]]) # Stack along the first dimension (dim=0) stack_dim0 = torch.stack([tensor1, tensor2, tensor3], dim=0) print("Stack along dim=0:", stack_dim0.shape) # Output: torc
🌐
TutorialsPoint
tutorialspoint.com › how-to-join-tensors-in-pytorch
How to join tensors in PyTorch?
September 14, 2023 - Use torch.stack() to create a new dimension and stack tensors, which is particularly useful for batch operations in deep learning. Shahid Akhtar Khan · Updated on: 2026-03-26T18:39:50+05:30 · 35K+ Views · Torch - Torch Tensors Torch · PyTorch ...
🌐
APXML
apxml.com › courses › getting-started-with-pytorch › chapter-2-advanced-tensor-manipulations › joining-splitting-tensors
Join & Split PyTorch Tensors | Cat, Stack, Chunk
Combining tensors is a frequent operation, especially when dealing with batches of data or merging feature representations. PyTorch offers two primary ways to join tensors: concatenation (torch.cat) and stacking (torch.stack).
🌐
Python Guides
pythonguides.com › pytorch-stack
How to Use PyTorch Stack?
June 19, 2025 - This ensures compatibility and prevents errors that would otherwise occur when trying to stack tensors with different data types. One of my favorite applications of the stack is in building ensemble models: # Simulating outputs from 3 different models, each predicting probabilities for 5 classes model1_output = torch.softmax(torch.randn(10, 5), dim=1) # 10 samples, 5 classes model2_output = torch.softmax(torch.randn(10, 5), dim=1) model3_output = torch.softmax(torch.randn(10, 5), dim=1) # Stack the outputs ensemble_outputs = torch.stack([model1_output, model2_output, model3_output], dim=0) print(f"Ensemble outputs shape: {ensemble_outputs.shape}") # torch.Size([3, 10, 5]) # Average the predictions (simple ensemble) ensemble_prediction = torch.mean(ensemble_outputs, dim=0) print(f"Final prediction shape: {ensemble_prediction.shape}") # torch.Size([10, 5])
🌐
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 - torch.stack() takes a sequence (list or tuple) of tensors and concatenates them along a new dimension, creating a higher-dimensional tensor.
🌐
Torch for R
torch.mlverse.org › docs › reference › torch_stack
Stack — torch_stack • torch
torch_stack(tensors, dim = 1L) tensors · (sequence of Tensors) sequence of tensors to concatenate · dim · (int) dimension to insert. Has to be between 0 and the number of dimensions of concatenated tensors (inclusive) Concatenates sequence of tensors along a new dimension.
Find elsewhere
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!

🌐
Sling Academy
slingacademy.com › article › stacking-tensors-in-pytorch
Stacking Tensors in PyTorch: Tutorials & Examples - Sling Academy
July 23, 2023 - You can see that the tensors a and b are concatenated along a new dimension in the middle of the tensor d. The first column of d along dim=1 is equal to a, and the second column is equal to b. The torch.vstack() function is used to stack tensors in sequence vertically (row wise).
🌐
DEV Community
dev.to › hyperkai › stack-in-pytorch-1bp1
stack in PyTorch - DEV Community
November 5, 2024 - stack() can get the 1D or more D stacked tensor of zero or more elements from the one or more 0D or more D tensors of zero or more elements as shown below: ... The 1st argument with torch is tensors(Required-Type:tuple or list of tensor of int, ...
🌐
Codecademy
codecademy.com › docs › pytorch › tensor operations › .hstack()
PyTorch | Tensor Operations | .hstack() | Codecademy
November 2, 2024 - torch.hstack(tensors) -> Tensor · tensors: A sequence of tensors with the same number of rows. All tensors must have the same number of dimensions and the same size in all dimensions except for the dimension corresponding to the horizontal stacking.
🌐
GitHub
github.com › pytorch › pytorch › issues › 71479
[numpy compat] torch.stack and torch.tensor doesn't support nested list+tensors (NumPy does support) - at least document the difference in the error message · Issue #71479 · pytorch/pytorch
January 19, 2022 - [numpy compat] torch.stack and torch.tensor doesn't support nested list+tensors (NumPy does support) - at least document the difference in the error message#71479
Author: pytorch
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-join-tensors-in-pytorch
How to join tensors in PyTorch? - GeeksforGeeks
February 28, 2022 - whereas the torch.stack() function allows us to stack the tensors and we can join two or more tensors in different dimensions such as -1 dimension and 0 dimensions,
🌐
Codecademy
codecademy.com › docs › pytorch › tensors › .column_stack()
PyTorch | tensors | .column_stack() | Codecademy
December 10, 2024 - In PyTorch, the .column_stack() function creates a new tensor by stacking tensors as columns in a 2D tensor, converting 1D tensors into 2D if necessary.