1
$\begingroup$

I'm in the middle of learning about Transformer layers, and I feel like I've got enough of the general idea behind them to be dangerous. I'm designing a neural network and my team would like to include them, but we're unsure how to proceed with the encoded sequences and what the right way to plug the them into the next layer in the model would be. We would like to process it such that we can plug the encoded sequence into a FC layer immediately after the Transformer Encoder.

If we just use a batch size of 1, for the sake of the argument, our encoded sequence output after being processed by the Transformer Encoder has shape tuple of (L,E), where L is the input sequence length and E is the embedded dimension size. I've seen some vague description of using some max/avg/conv1d pooling on the Encoded sequence, but nothing super clear about what that means. If I'm following this correctly, would I apply the max/avg/1conv1d pooling such that the pooling result gives me an resulting vector with shape tuple (E,), or would I pool along the other dimension?

$\endgroup$
1
  • $\begingroup$ please consider upvoting and accepting the answer or, alternatively, letting me know if it there is something not clear or needing clarification. $\endgroup$
    – noe
    Jul 23, 2022 at 14:47

1 Answer 1

1
$\begingroup$

The typical approach for this is follow BERT's approach: add an extra special token at the beginning of the input sequence (in BERT it is [CLS]) and only use the output of the network at that position as input to your fully connected layer. The output at the rest of the positions is ignored.

You can see a nice illustration of this approach in the notorious blog post The Illustrated BERT, which explains very visually all the details about BERT:

enter image description here

In the illustration, you can see the model input at the bottom and how it has been added a special [CLS] token at the beginning and then the output of the model at that position is then used for a classification task.

During training, the model will learn to condense the needed information from the whole sentence into the output of the first position.

Another alternative, as you pointed out, is to have global average pooling over all the outputs. This was the norm in the LSTM times before Transformers came. I am not aware of any articles comparing the performance of both approaches but, nowadays, with Transformers, everybody uses the BERT approach.

Both BERT's approach and the global average/max pooling approach achieve your goal: collapsing the variable length sequence of vectors into a single vector that you can then use for classification.

$\endgroup$
2
  • $\begingroup$ Thanks for the feedback and information. I'll need to unpack what you're describing a bit more and read up on BERT. Maybe you can comment on this diagram, taken from this blog post about transformers that I've been reading to learn more about transformers. It looks like the author is applying an average pooling operation down the length of the encoded vectors to get a single vector of length (1,E), where E is the length of the encoded vector, and then passing that on. Is that right? $\endgroup$
    – Rstan
    Jul 24, 2022 at 15:37
  • $\begingroup$ I commented of the average pooling approach that you mentioned, and also extended the information on BERT, with a diagram and a useful reference. $\endgroup$
    – noe
    Jul 25, 2022 at 7:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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