The keras model looks like this

features_input = Input(shape=(features.shape[1],))
inp = Input(shape=(maxlen, ))
x = Embedding(max_features, embed_size, weights=[embedding_matrix], trainable=False)(inp)
x = Bidirectional(LSTM(num_filters, return_sequences=True))(x)
max_pool = GlobalMaxPooling1D()(x)
x = concatenate([x_h, max_pool,features_input])
outp = Dense(6, activation="sigmoid")(x)

What does GlobalMaxPooling1D()(x) really do to the output of LSTM? I know the input to LSTM layer is of dimension (batch_size, steps, features).

Does GlobalMaxPooling1D take max across num_filters/hidden units of each LSTM unit?


1 Answer 1


No, it takes max across steps.

Suppose the output x of your Bidirectional(LSTM()) has shape (batch_size, steps, hidden_size), then after GlobalMaxPooling1D() your max_pool would have shape (batch_size, hidden_size).

  • $\begingroup$ So is it "select batch_size, max(hidden_size) group by batch_size, steps" in terms of SQL? Not able to visualize it $\endgroup$ Commented Sep 19, 2018 at 5:31
  • $\begingroup$ If your hidden size is of 100 dimensions for each word, and you have 20 words in each sentence, then you end up from <batchsize, 20, 100> to <bs, 1, 100>. This can be squeezed to give <bs, 100>. For each dimension you take the max value across all words. The intuition is - each dimension represents something and max_value represents that dimension the strongest. So across the sentence, you get the strongest features(dimensions) represented by that sentence. That is one way of getting a sentence embedding. You could also take an average and then feed both the avg and max to the next layer $\endgroup$
    – Allohvk
    Commented Jun 28, 2021 at 17:42

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.