lethienhoa / DenseNet-NLP

Tensorflow implementation of Very Deep Convolutional Networks for Natural Language Processing
79 stars 23 forks source link

k-max pooling #1

Closed NaveenAri closed 7 years ago

NaveenAri commented 7 years ago

The VDCNN paper's k-max pooling finds the top k values while preserving the order in which they appear in the original sequence. I don't think tensorflow's tf.nn.top_k does that.

This is what I get when I use tf.nn.top_k on a simple example:

import tensorflow as tf

def k_max_pool(value, k):
    sorted_tensor = tf.nn.top_k(value, k, sorted=True)
    unsorted_tensor = tf.nn.top_k(value, k, sorted=False)
    return sorted_tensor, unsorted_tensor

x = [[1,3,2],[4,5,6],[9,8,7]]
print 'input', x

sess = tf.Session()
X = tf.placeholder("int32")
sorted_tensor, unsorted_tensor = k_max_pool(X, 2)
sorted_value, unsorted_value = sess.run([sorted_tensor, unsorted_tensor], feed_dict={X: x})
print 'sorted', sorted_value
print 'unsorted', unsorted_value
input [[1, 3, 2], [4, 5, 6], [9, 8, 7]]
sorted TopKV2(values=array([[3, 2],
       [6, 5],
       [9, 8]], dtype=int32), indices=array([[1, 2],
       [2, 1],
       [0, 1]], dtype=int32))
unsorted TopKV2(values=array([[2, 3],
       [5, 6],
       [8, 9]], dtype=int32), indices=array([[2, 1],
       [1, 2],
       [1, 0]], dtype=int32))
lethienhoa commented 7 years ago

yes, thank you NaveenAri. I've asked about this on tensorflow's github but I didn't received the answer yet. For the moment, I don't use top_k max pooling but a normal max pooling instead. It's hard to know if this operation can give better result or not (or redesigning the architecture will give higher effect). Anyway, thanks for the clarification !