My understanding of Large Language Models like GPT is that they are special kinds of deep neural networks specifically trained to predict the next word, given the beginning of a sentence.

I understand that a key aspect of their architecture is attention, which allows a word representation (a vector) to be mixed-up with the representation of other words in the sentence, the weights being used to make that linear combination representing a notion of proximity as they are derived from the scalar product of the words embedding.

Now, considering a sentence of words like "word1 word2 word3", if we want to feed it to a neural network, which has a fixed number of input nodes, we should represent it as a single input vector of fixed size (same as the number of input nodes). My understanding of attention is that it still produces one vector for each input words, not one vector for the whole sentence.

How is a full sentence turned into a fixed-size vector?

  • $\begingroup$ You might find it good to google SBERT and similar. $\endgroup$ Commented Feb 28 at 14:04
  • $\begingroup$ @DavidHoelzer Is this how it is done in GPT? $\endgroup$
    – Weier
    Commented Feb 28 at 14:42
  • $\begingroup$ letmegooglethat.com/… $\endgroup$ Commented Feb 28 at 19:32
  • $\begingroup$ @DavidHoelzer funny, except that you put the wrong link (and actually, most of Google results are not quite enlightening) $\endgroup$
    – Weier
    Commented Feb 28 at 20:19

2 Answers 2


Aggregating embeddings from pretrained models

If you want a single vector representation from a vanilla language model (i.e., one not specially trained for producing sentence embeddings) like GPT you'll need to do some pooling operation to aggregate information from all the output representations. You could e.g., perform global average pooling to aggregate your (length, embedding) vector to a single (embedding,) vector.

Alternatively, depending on the pretraining task, the output embedding corresponding to certain tokens may also contain aggregate information about the entire text. In this case, you could try using that representation directly. e.g., in GPT-style decoder models, the last token is used to perform next-token prediction---in this sense, it could be aggregating information from all the previous tokens in order to make a prediction.

Also, if you're using a non-specially trained model, you'll probably also want to play around with which layers you extract the embeddings from. Later layers will probably be more related to the pretraining task (i.e., token prediction) rather than be aggregating meaning.

Specialized models for sentence embeddings

The two techniques described above tend to be pretty suboptimal: these models just aren't trained for the task. It's much more common to use specialized sentence/document embedding models e.g., as implemented in the sentence-transformers package. This page in their documentation is very informative for understanding the mechanics of how certain techniques work.

Looking at one particular technique (Sentence-BERT), they pool embeddings/use the embedding of a particular token as I described, but crucially, they train the models so that the embeddings for similar documents/sentences are similar, and vice-versa for non-similar documents/sentences.


A transformer block takes a sequence of tokens of length N and returns another sequence of tokens of length N. When the transformer is configured as an Encoder, assuming the inputs to the transformer are word embeddings, the transformer's output is context embeddings. For instance, if the input sequence is given by $I = W_1, W_2, .., W_N$, the output is given by $O = O_1, O_2,...,O_N$, where $O_i \leftarrow \Pr(W_i|W_{k\ne i})$. Every $O_i$ represents the information about $W_i$ concerning all the other input words $W_{k\ne i}$.

Given this arrangement, if we introduce a dummy token [CLS] at the beginning of every input sequence as $I = W_{[CLS]}, W_1, W_2,..., W_N$ while training the Encoder, the output of the transformer pertaining to the CLS position $O_{[CLS]}$ will have information about the entire input sequence based on the expression $O_{[CLS]} = \Pr(W_{[CLS]} | W_{k\ne[CLS]})$.

To get the embedding of an input sequence, pick the output of the transformer at the first output position, which is usually the position of the $[CLS]$ token on the input side.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .