kerasswaplayertf.keraspermute

Permute features of 1d array in keras


I want to swap the features before I feed them to another layer. I have 4 variables so my input array is of size (#samples, 4)

Let's say the features are: x1, x2, x3, x4

Excepted output:

Swapping1: x4, x3, x2, x1

Swapping2: x2, x3, x2, x1

…. etc

Here is what I tried

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

However, Permute layer is expecting multiple dimensions arrays to permute the arrays so it does not do the job. Is there anyway can solve this in keras?

I also tried to feed the flowing functions as a Lambda layer and I get an error

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.

Solution

  • Use a Lambda layer with some backend function or with 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)