keras中一维阵列的排列特征

keras中一维阵列的排列特征,keras,swap,layer,tf.keras,permute,Keras,Swap,Layer,Tf.keras,Permute,我想在将这些功能提供给另一层之前交换它们。 我有4个变量,因此我的输入数组大小为(#samples,4) 假设特征是:x1、x2、x3、x4 例外输出: 交换1:x4、x3、x2、x1 交换2:x2,x3,x2,x1 …。等 这是我试过的 def toy_model(): _input = Input(shape=(4,)) perm = Permute((4,3,2,1)) (_input) dense = Dense(1024)(perm) output =

我想在将这些功能提供给另一层之前交换它们。 我有4个变量,因此我的输入数组大小为(#samples,4)

假设特征是:x1、x2、x3、x4

例外输出:

交换1:x4、x3、x2、x1

交换2:x2,x3,x2,x1

…。等

这是我试过的

def toy_model():    
   _input = Input(shape=(4,))
   perm = Permute((4,3,2,1)) (_input)
   dense = Dense(1024)(perm)
   output = Dense(1)(dense)

   model = Model(inputs=_input, outputs=output)
   return model

   toy_model().summary()
   ValueError: Input 0 is incompatible with layer permute_58: expected ndim=5, found ndim=2
但是,排列层希望多个维度数组排列这些数组,因此它不执行此任务。 在keras中是否有任何方法可以解决这个问题

我还尝试将流动函数作为Lambda层进行填充,但得到了一个错误

def permutation(x):
   x = keras.backend.eval(x)
   permutation = [3,2,1,0]
   idx = np.empty_like(x)
   idx[permutation] = np.arange(len(x))
   permutated = x[:,idx]
   return K.constant(permutated)

ValueError: Layer dense_93 was called with an input that isn't a symbolic tensor. Received type:                                                
<class 'keras.layers.core.Lambda'>. Full input: [<keras.layers.core.Lambda object at 
0x7f20a405f710>]. All inputs to the layer should be tensors.
def置换(x):
x=keras.backend.eval(x)
排列=[3,2,1,0]
idx=np.empty_like(x)
idx[置换]=np.arange(len(x))
置换=x[:,idx]
返回K.常数(置换)
ValueError:调用Layer dense_93时使用的输入不是符号张量。接收类型:
. 完整输入:[]。层的所有输入都应该是张量。

使用带有一些后端功能的
Lambda
层,或使用slices+concat

4,3,2,1

perm = Lambda(lambda x: tf.reverse(x, axis=-1))(_input)
def perm_2321(x):
    x1 = x[:, 0]
    x2 = x[:, 1]
    x3 = x[:, 2]

    return tf.stack([x2,x3,x2,x1], axis=-1)

perm = Lambda(perm_2321)(_input)
2,3,2,1

perm = Lambda(lambda x: tf.reverse(x, axis=-1))(_input)
def perm_2321(x):
    x1 = x[:, 0]
    x2 = x[:, 1]
    x3 = x[:, 2]

    return tf.stack([x2,x3,x2,x1], axis=-1)

perm = Lambda(perm_2321)(_input)

我把事情弄得更复杂了!这很好用,很管用。感恩节:如果你认为这回答了你的问题,请把它标记为回答。