Use of torch.stack()
t1 = torch.tensor([1,2,3])
t2 = torch.tensor([4,5,6])
t3 = torch.tensor([7,8,9])

torch.stack((t1,t2,t3),dim=1)

When implementing the torch.stack(), I can't understand how stacking is done for different dim.
Here stacking is done for columns but I can't understand the details as to how it is done. It becomes more complicated dealing with 2-d or 3-D tensors.
tensor([[1, 4, 7],
        [2, 5, 8],
        [3, 6, 9]])


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 &quot;added dimension&quot; 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:
&gt;&gt;&gt; x_1d = list(torch.empty(3, 2))     # 3 lines

&gt;&gt;&gt; torch.stack(x_1d, 0).shape         # axis=0 stacking
torch.Size([3, 2])

&gt;&gt;&gt; torch.stack(x_1d, 1).shape         # axis=1 stacking
torch.Size([2, 3])


Similarly for two-dimensional inputs:

With code:
&gt;&gt;&gt; x_2d = list(torch.empty(3, 2, 2))   # 3 2x2-squares

&gt;&gt;&gt; torch.stack(x_2d, 0).shape          # axis=0 stacking
torch.Size([3, 2, 2])

&gt;&gt;&gt; torch.stack(x_2d, 1).shape          # axis=1 stacking
torch.Size([2, 3, 2])

&gt;&gt;&gt; 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.
