Python numpy整形函数的PyTorch等价

Python numpy整形函数的PyTorch等价,python,neural-network,pytorch,reshape,tensor,Python,Neural Network,Pytorch,Reshape,Tensor,嗨,我有这些函数来展平我的复杂类型数据,将其提供给NN,并将NN预测重建为原始形式 def flatten_input64(Input): #convert (:,4,4,2) complex matrix to (:,64) real vector Input1 = Input.reshape(-1, 32, order='F') Input_vector=np.zeros([19957,64],dtype = np.float64) Input_vector[:,0:32] =

嗨,我有这些函数来展平我的复杂类型数据,将其提供给NN,并将NN预测重建为原始形式

def flatten_input64(Input): #convert (:,4,4,2) complex matrix to (:,64) real vector
  Input1 = Input.reshape(-1, 32, order='F')
  Input_vector=np.zeros([19957,64],dtype = np.float64)
  Input_vector[:,0:32] = Input1.real
  Input_vector[:,32:64] = Input1.imag
  return Input_vector

def convert_output64(Output): #convert (:,64) real vector to (:,4,4,2) complex matrix  
  Output1 = Output[:,0:32] + 1j * Output[:,32:64]
  output_matrix = Output1.reshape(-1, 4 ,4 ,2 , order = 'F')
  return output_matrix
我正在写一个定制的丢失,要求所有操作都在torch中,我应该在PyTorch中重写我的转换函数。问题是Pytork没有“F”顺序整形。我试着写我自己版本的F reorder,但它不起作用。 你知道我犯了什么错吗

def convert_output64_torch(input):
   # number_of_samples = defined
   for i in range(0, number_of_samples):
     Output1 = input[i,0:32] + 1j * input[i,32:64]
     Output2 = Output1.view(-1,4,4,2).permute(3,2,1,0)
   if i == 0:
     Output3 = Output2
   else:
     Output3 = torch.cat((Output3, Output2),0)
return Output3
更新:在@a_guest评论之后,我尝试用转置和整形重新创建矩阵,我得到的代码与numy中的F顺序整形相同:

def convert_output64_torch(input):
   Output1 = input[:,0:32] + 1j * input[:,32:64]
   shape = (-1 , 4 , 4 , 2)
   Output3 = torch.transpose(torch.transpose(torch.reshape(torch.transpose(Output1,0,1),shape[::-1]),1,2),0,3)
return Output3

在Numpy和PyTorch中,您可以通过以下操作获得等效值:
a.T.reformate(shape[:-1]).T
(其中
a
是数组或张量):

a=np.arange(16)。重塑(4,4) >>>a 数组([[0,1,2,3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15]]) >>>形状=(2,8) >>>a.重塑(形状,顺序='F') 数组([[0,8,1,9,2,10,3,11], [ 4, 12, 5, 13, 6, 14, 7, 15]]) >>>a.T.重塑(形状[:-1]).T 数组([[0,8,1,9,2,10,3,11], [ 4, 12, 5, 13, 6, 14, 7, 15]])
谢谢,@a_客人!我需要另一个转置,使它为我的最终维度工作,我不知道为什么。我将在更新后的帖子中分享代码。