Search code examples
python-3.xkeras-layermax-pooling

How to select top-k elements of a keras dense layer?


I'm trying to perform a k-max pooling in order to select top-k elements of a dense with shape (None, 30). I tried a MaxPooling1D layer but it doesn't work, since keras pooling layers require at least a 2d input shape. I'm using the following Lambda layer, but I got the following error:

layer_1.shape
(None, 30)
layer_2 = Lambda(lambda x: tf.nn.top_k(x, k=int(int(x.shape[-1])/2),
                                                sorted=True, 
                                                name="Top_k_final"))(layer_1)

Error: File "/usr/local/lib/python3.5/dist-packages/keras/engine/base_layer.py", line 474, in call output_shape = self.compute_output_shape(input_shape) File "/usr/local/lib/python3.5/dist-packages/keras/layers/core.py", line 652, in compute_output_shape return K.int_shape(x) File "/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py", line 591, in int_shape return tuple(x.get_shape().as_list()) AttributeError: 'TopKV2' object has no attribute 'get_shape'


Solution

  • Based on this example, I solved the problem. In fact, I solved the problem by adding .values to get the tensor values from the tf.nn.top_k, as follows. But I'm not sure if my solution is correct or not.

    layer_2 = Lambda(lambda x: tf.nn.top_k(x, k=int(int(x.shape[-1])/2),
                                                    sorted=True, 
                                                    name="Top_k_final").values)(layer_1)