3
$\begingroup$

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?

$\endgroup$

1 Answer 1

2
$\begingroup$

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).

$\endgroup$
2
  • $\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$ 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
    Jun 28, 2021 at 17:42

Your Answer

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

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