Using reshape or view in a certain fashion
import torch
import numpy as np
a = torch.tensor([[1, 4], [2, 5],[3, 6]])
bb=a.detach().numpy()
b = a.view(6).detach().numpy()

Element b is like:
[1 4 2 5 3 6]

How do I reshape back to the following:
[1 2 3 4 5 6]

This is just an example, want some generic answers, even 3D.

If you want to remain in PyTorch, you can view b in a's shape, then apply a transpose and flatten:
&gt;&gt;&gt; b.view(-1,2).T.flatten()
tensor([1, 2, 3, 4, 5, 6])

In the 3D case, you can perform similar manipulations using torch.transpose which enables you to swap two axes. You get the desired result by combining it with torch.view:

First case (extra dimension last):
&gt;&gt;&gt; b = a.view(-1, 1).expand(-1,3).flatten()
tensor([1, 1, 1, 4, 4, 4, 2, 2, 2, 5, 5, 5, 3, 3, 3, 6, 6, 6])

&gt;&gt;&gt; b.view(-1,2,3).transpose(0,1).flatten()
tensor([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6])


Second case (extra dimension first):
&gt;&gt;&gt; b = a.view(1,-1).expand(3,-1).flatten()
tensor([1, 4, 2, 5, 3, 6, 1, 4, 2, 5, 3, 6, 1, 4, 2, 5, 3, 6])

&gt;&gt;&gt; b.view(3,-1).T.view(-1,2,3).transpose(0,1).flatten()
tensor([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6])



