Creating 1D vectors over 3D tensors in pytorch
I have the following tensor with dimensions (2, 3, 2, 2) where the dimensions represent (batch_size, channels, height, width):
tensor([[[[ 1.,  2.],
          [ 3.,  4.]],

         [[ 5.,  6.],
          [ 7.,  8.]],

         [[ 9., 10.],
          [11., 12.]]],


        [[[13., 14.],
          [15., 16.]],

         [[17., 18.],
          [19., 20.]],

         [[21., 22.],
          [23., 24.]]]])

I would like to convert this into the following tensor with dimensions (8, 3):
tensor([[ 1,  5,  9],
        [ 2,  6, 10],
        [ 3,  7, 11],
        [ 4,  8, 12],
        [13, 17, 21],
        [14, 18, 22],
        [15, 19, 23],
        [16, 20, 24]])

Essentially I would like to create 1D vector over the elements of the matrices. I have tried many operations such as flatten and reshape, but I cannot figure out how to achieve this reshaping.

You can do it this way:
import torch

x = torch.Tensor(
   [
    [
    [[1,2],[3,4]],
    [[5,6],[7,8]],
    [[9,10],[11,12]]],
    [
    [[13,14],[15,16]],
    [[17,18],[19,20]],
    [[21,22],[23,24]]]
   ]  
)

result = x.swapaxes(0, 1).reshape(3, -1).T

print(result)
# &gt; tensor([[ 1.,  5.,  9.],
# &gt;         [ 2.,  6., 10.],
# &gt;         [ 3.,  7., 11.],
# &gt;         [ 4.,  8., 12.],
# &gt;         [13., 17., 21.],
# &gt;         [14., 18., 22.],
# &gt;         [15., 19., 23.],
# &gt;         [16., 20., 24.]])

