我想在交换要素到另一层之前交换它们。我有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
但是,Permute层期望多维数组对数组进行置换,因此它无法完成工作。反正喀拉拉邦有什么可以解决的吗?
我也试图将流动函数作为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.
[将Lambda
层与某些后端功能一起使用或与slices + concat一起使用。
4,3,2,1:
perm = Lambda(lambda x: tf.reverse(x, axis=-1))(_input)
2,3,2,1:
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)