4

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'

Belkacem Thiziri
  • 483
  • 1
  • 6
  • 23

1 Answers1

5

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)
Belkacem Thiziri
  • 483
  • 1
  • 6
  • 23
  • Is there someone who could clarify more? – Belkacem Thiziri Jan 31 '19 at 10:43
  • If you check here, https://www.tensorflow.org/api_docs/python/tf/math/top_k, you will see that ```tf.nn.top_k()``` returns both the values and indices of the top k elements. Hence the ```.values``` specification. – Uzay Macar Jun 18 '19 at 19:05