3

I want to do max pooling in my network, like this:

enter image description here

My network is 1D (you can think the above example is one sentence with 6 words while every word has 3 embedding )I don't know the length of feature(not every sentence has the same length), so I can't set the pool_size in tf.layers.MaxPooling1D(https://www.tensorflow.org/api_docs/python/tf/layers/MaxPooling1D)

I just want to pooling every half features(or half sentence), Is there any function or method to do that?

1
  • How should the "halfPolling" handle odd numbers of words? both half include, both half exclude or one include and one exclude the middle word?
    – KrisR89
    Jul 11, 2019 at 8:22

1 Answer 1

0

(Note: My previous answer had an error that would have result in incorrect pooling windows. This one should be fine.)

Here is one possible way written in "low level" tensorflow. You might need to wrap this in a keras layer (or just use Lambda) to integrate it into your model.

x = ...  # input, shape batch x n_words x features
x = tf.reshape(x, [batch, 2, n_words//2, features])  # need to get these dimensions, can get them from tf.shape(x) as well
x = tf.reduce_max(x, axis=2)

This would implement max pooling; you could also use reduce_mean for average pooling, for example.

This has one limitation, namely it's not going to work if n_words is odd. In that case, you might have to check whether it is and use tf.pad to add one element in the word axis to make it even.

1
  • one brilliant idea ,through has some limitation, thanks
    – andy
    Jul 11, 2019 at 20:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.