Reshaping order in PyTorch - Fortran-like index ordering
In numpy, there is an ordering feature for reshaping arrays, by default it is C, but you can specify other ordering like F:
a = np.arange(6).reshape((3, 2))
f = np.reshape(a, (2, 3), order='F') # Fortran-like index ordering
c = np.reshape(a, (2, 3)) 
print('a= \n', a)
print('f= \n', b)
print('c= \n', c)

the result:
a= 
 [[0 1]
 [2 3]
 [4 5]]
f= 
 [[0 4 3]
 [2 1 5]]
c= 
 [[0 1 2]
 [3 4 5]]

There is no such option in torch.reshape or tensor.view for reshaping in F order.
Is there any way to do that F order reshape in PyTorch? I need everything to be in PyTorch.

I don't think pytorch has built-in support for this. That said you can achieve the desired result using Tensor.permute. Unfortunately I doubt this will be very efficient since AFAIK permute internally makes a copy of the tensor.
def reshape_fortran(x, shape):
    if len(x.shape) &gt; 0:
        x = x.permute(*reversed(range(len(x.shape))))
    return x.reshape(*reversed(shape)).permute(*reversed(range(len(shape))))

Example usage:
a = torch.arange(6).reshape(3, 2)
f = reshape_fortran(a, (2, 3))
c = a.reshape(2, 3)

which results in
a = 
tensor([[0, 1],
        [2, 3],
        [4, 5]])
f =
tensor([[0, 4, 3],
        [2, 1, 5]])
c =
tensor([[0, 1, 2],
        [3, 4, 5]])

