Answers.Body: <h2>Aggregating embeddings from pretrained models</h2>
  <p>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 <code>(length, embedding)</code> vector to a single <code>(embedding,)</code> vector.</p>
  <p>Alternatively, depending on the pretraining task, the output embedding corresponding to certain tokens <em>may</em> 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---<a href="https://stackoverflow.com/a/64180317">in this sense, it <em>could</em> be aggregating information from all the previous tokens in order to make a prediction</a>.</p>
  <p>Also, if you're using a non-specially trained model, <a href="https://stackoverflow.com/a/63464865">you'll probably also want to play around with which layers you extract the embeddings from.</a> Later layers will probably be more related to the pretraining task (i.e., token prediction) rather than be aggregating meaning.</p>
  <h2>Specialized models for sentence embeddings</h2>
  <p>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 <a href="https://www.sbert.net/" rel="nofollow noreferrer">sentence-transformers</a> package. <a href="https://www.sbert.net/docs/training/overview.html" rel="nofollow noreferrer">This page</a> in their documentation is very informative for understanding the mechanics of how certain techniques work.</p>
  <p><a href="https://arxiv.org/pdf/1908.10084.pdf" rel="nofollow noreferrer">Looking at one particular technique</a> (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.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-29T00:17:23.517Z
Answers.LastActivityDate: 2024-02-29T00:17:23.517Z
Answers.UserDisplayName: Alexander Wan
Answers.Body: <p>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 <span class="math-container">$I = W_1, W_2, .., W_N$</span>, the output is given by <span class="math-container">$O = O_1, O_2,...,O_N$</span>, where <span class="math-container">$O_i \leftarrow \Pr(W_i|W_{k\ne i})$</span>.  Every <span class="math-container">$O_i$</span> represents the information about <span class="math-container">$W_i$</span> concerning all the other input words <span class="math-container">$W_{k\ne i}$</span>.</p>
  <p>Given this arrangement, if we introduce a dummy token [CLS] at the beginning of every input sequence as <span class="math-container">$I = W_{[CLS]}, W_1, W_2,..., W_N$</span> while training the Encoder, the output of the transformer pertaining to the <code>CLS</code> position <span class="math-container">$O_{[CLS]}$</span> will have information about the entire input sequence based on the expression <span class="math-container">$O_{[CLS]} = \Pr(W_{[CLS]} | W_{k\ne[CLS]})$</span>.</p>
  <p>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 <span class="math-container">$[CLS]$</span> token on the input side.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-29T06:09:13.907Z
Answers.LastActivityDate: 2024-02-29T06:09:13.907Z
Answers.UserDisplayName: sudar
Body: <p>My understanding of <em>Large Language Models</em> like <em>GPT</em> is that they are special kinds of <em>deep neural networks</em> specifically trained to predict the next word, given the beginning of a sentence.</p>
  <p>I understand that a key aspect of their architecture is <em>attention</em>, 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.</p>
  <p>Now, considering a sentence of words like &quot;word1 word2 word3&quot;, if we want to feed it to a neural network, which has a fixed number of input nodes, we should represent it as a <strong>single</strong> input vector of fixed size (same as the number of input nodes). My understanding of <em>attention</em> is that it still produces one vector for each input words, not one vector for the whole sentence.</p>
  <p>How is a full sentence turned into a fixed-size vector?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-28T14:04:41.320Z
Comments.Text: You might find it good to google SBERT and similar.
Comments.UserDisplayName: David Hoelzer
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-28T14:42:59.510Z
Comments.Text: @DavidHoelzer Is this how it is done in GPT?
Comments.UserDisplayName: Weier
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-28T19:32:37.473Z
Comments.Text: https://letmegooglethat.com/?q=https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dhow%2Bdoes%2BGPT%2Bgenerate%2Bsentence%2Bvectors%26rlz%3D1C5CHFA_enUS872US872%26oq%3Dhow%2Bdoes%2BGPT%2Bgenerate%2Bsentence%2Bvectors%26gs_lcrp%3DEgZjaHJvbWUyBggAEEUYOdIBCDkzNDlqMGo3qAIAsAIA%26sourceid%3Dchrome%26ie%3DUTF-8
Comments.UserDisplayName: David Hoelzer
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-02-28T20:19:33.747Z
Comments.Text: @DavidHoelzer funny, except that you put the wrong link (and actually, most of Google results are not quite enlightening)
Comments.UserDisplayName: Weier
Tags: <attention><large-language-models><chatgpt><word-embedding><linear-algebra>
Title: How are sentences turned into a vector in LLM
UserDisplayName: Weier
ThreadText: # How are sentences turned into a vector in LLM
  
  (Asked by: username_0 on 2024-02-28)
  
  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?
  
  **Tags:** attention, large-language-models, chatgpt, word-embedding, linear-algebra
  > **Comments:**
  > **username_1**: You might find it good to google SBERT and similar.  
  > **username_0**: @DavidHoelzer Is this how it is done in GPT?  
  > **username_1**: https://letmegooglethat.com/?q=https%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dhow%2Bdoes%2BGPT%2Bgenerate%2Bsentence%2Bvectors%26rlz%3D1C5CHFA_enUS872US872%26oq%3Dhow%2Bdoes%2BGPT%2Bgenerate%2Bsentence%2Bvectors%26gs_lcrp%3DEgZjaHJvbWUyBggAEEUYOdIBCDkzNDlqMGo3qAIAsAIA%26sourceid%3Dchrome%26ie%3DUTF-8  
  > **username_0**: @DavidHoelzer funny, except that you put the wrong link (and actually, most of Google results are not quite enlightening)  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-02-29. Score: 1
  
  ## 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](https://stackoverflow.com/a/64180317).
  
  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.](https://stackoverflow.com/a/63464865) 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](https://www.sbert.net/) package. [This page](https://www.sbert.net/docs/training/overview.html) in their documentation is very informative for understanding the mechanics of how certain techniques work.
  
  [Looking at one particular technique](https://arxiv.org/pdf/1908.10084.pdf) (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.
  
  
  ---
  
  ### Answer by username_3 on 2024-02-29. Score: 0
  
  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.
  
  
  ---
  
  