How to flatten arrays via column-major ordering like numpy's "C" vs "F" arguments?

Hi All,

I was wondering if it were possible to specify how flattening or reshaping is done via PyTorch in a similar way to how Numpy (here) allows you to pass a “C” or “F” argument which specifies row or column ordering respectively.

a = torch.Tensor([[1,2], 
                  [3,4]])

a.detach().numpy().flatten('C') #returns array([1., 2., 3., 4.], dtype=float32) (row)
a.detach().numpy().flatten('F') #returns array([1., 3., 2., 4.], dtype=float32) (column)
a.flatten() #returns tensor([1., 2., 3., 4.]) (row)

As you can see .reshape defaults to row ordering, is it possible to specify column majoring in some way? Potentially this might be something to open as an issue?

Thanks in advance!

I don’t think PyTorch supports the Fortran- style flattening, but you should be able to transpose the tensor before flattening it if this order is needed.

1 Like