Answers.Body: <p>A GPT produces output based on its own previous output, so it must be able to understand its output.</p>
  <p>The learning input is provided as a stream of tokens, and these tokens are defined before learning starts. So it has to use the same set of tokens to understand its own output. The set of possible output tokens is fixed, it learns only to assign probabilities of the next token.</p>
  <p>Looking on it in a per token perspective, when it gets a token as input, it learns that it has a non-zero probability to be following the previous input. If the same happens again, it learned that the probability is higher than previously assumed. The probability is expressed as a number of occurrences in the input, and a number per token in the output, but both are about the relative probability of tokens following the previous text.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-20T16:10:06.737Z
Answers.LastActivityDate: 2023-04-20T16:10:06.737Z
Answers.UserDisplayName: Volker Siegel
Answers.Body: <p>Yes, GPT uses the same embedding matrix. See <a href="https://www.cs.ubc.ca/%7Eamuham01/LING530/papers/radford2018improving.pdf" rel="nofollow noreferrer">here</a>.</p>
  <p><a href="https://i.sstatic.net/K4q6K.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/K4q6K.png" alt="enter image description here" /></a></p>
  <p>Regarding your second question - on the input side, a given token selects one row from the token embedding matrix; it is not clear that higher magnitude signifies anything. On the output side, the magnitude of the output vector <span class="math-container">$h_n$</span> also doesn't signify anything. However, once you take the dot product of the output vector with each of the previous &quot;rows&quot; of the token embedding matrix, this gives you a dot-product similarity of the computed output with the pre-stored embeddings. A large value for a given token element means the model is more likely to predict that token.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-21T15:04:15.713Z
Answers.Text: Please don't upload text as images, it isn't searchable. Use [MathJax](https://quantumcomputing.meta.stackexchange.com/a/152/278) whenever possible. Thanks.
Answers.UserDisplayName: Rob
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-08-21T10:50:37.973Z
Answers.LastActivityDate: 2022-08-21T10:50:37.973Z
Answers.UserDisplayName: Que
Body: <p>My understanding is that GPT uses the <strong>same embedding matrix</strong> for both inputs and output: Let <span class="math-container">$V$</span> be the vocab size, <span class="math-container">$D$</span> the number of embedding dimensions, and <span class="math-container">$E$</span> be a <span class="math-container">$V \times D$</span> embedding matrix:</p>
  <ul>
  <li>On input, if <span class="math-container">$x$</span> is a one-hot <span class="math-container">$V$</span>-dimensional vector, GPT uses <span class="math-container">$Ei$</span>.</li>
  <li>On output, if <span class="math-container">$\hat y$</span> is a <span class="math-container">$D$</span>-dimensional prediction vector, GPT uses softmax(<span class="math-container">$E^\top{\hat y}$</span>) as its predictions.</li>
  </ul>
  <h2>Q1. Is the above correct?</h2>
  <p>I cannot find this stated clearly in the paper, but it is stated explicitly <a href="https://www.lesswrong.com/posts/AcKRB8wDpdaN6v6ru/interpreting-gpt-the-logit-lens" rel="nofollow noreferrer">here</a>.  It's also clearly implied by the parameter count listed <a href="https://jalammar.github.io/illustrated-gpt2/" rel="nofollow noreferrer">here</a>, and argued for as best practice <a href="https://paperswithcode.com/method/weight-tying" rel="nofollow noreferrer">here</a>.  Yet, for example, Karpathy's mini-GPT implementation seems to use two different matrices:</p>
  <pre><code>self.tok_emb = nn.Embedding(config.vocab_size, config.n_embd) # &lt;--- This would be E
  self.pos_emb = nn.Parameter(torch.zeros(1, config.block_size, config.n_embd))
  self.drop = nn.Dropout(config.embd_pdrop)
  # transformer
  self.blocks = nn.Sequential(*[Block(config) for _ in range(config.n_layer)])
  # decoder head
  self.ln_f = nn.LayerNorm(config.n_embd)
  self.head = nn.Linear(config.n_embd, config.vocab_size, bias=False) # &lt;--- This has the same dimensions as Etranspose but is clearly a different matrix
  
  </code></pre>
  <h2>Q2. If it is correct, how does can it work?</h2>
  <p>This seems to be tasking <span class="math-container">$E$</span> with two very different, even opposing, functions:</p>
  <ul>
  <li>Map vocab to their <em>meaning</em> on the input side; higher magnitude indicates &quot;more meaning&quot;</li>
  <li>Map meaning to the <em>most likely</em> vocab on the output side; higher magnitude indicates greater likelihood</li>
  </ul>
  <p>When outputting, we want the softmax to be highest when the word is most likely; magnitude of the output matrix should be roughly proportional to how likely the word is two appear.</p>
  <p>Yet, when inputting, magnitude has <em>nothing to do with likelihood</em>.  Magnitude on the input side captures some element of meaning: perhaps how extreme or intense the meaning is, perhaps another aspect (not necessarily easily interpreted).</p>
  
Tags: <natural-language-processing><transformer><word-embedding><embeddings><gpt>
Title: How does GPT use the same embedding matrix for both input and output?
UserDisplayName: SRobertJames
ThreadText: # How does GPT use the same embedding matrix for both input and output?
  
  (Asked by: username_0 on 2022-07-04)
  
  My understanding is that GPT uses the same embedding matrix for both inputs and output: Let $V$ be the vocab size, $D$ the number of embedding dimensions, and $E$ be a $V \times D$ embedding matrix:
  
  On input, if $x$ is a one-hot $V$-dimensional vector, GPT uses $Ei$.
  On output, if $\hat y$ is a $D$-dimensional prediction vector, GPT uses softmax($E^\top{\hat y}$) as its predictions.
  
  Q1. Is the above correct?
  I cannot find this stated clearly in the paper, but it is stated explicitly here.  It's also clearly implied by the parameter count listed here, and argued for as best practice here.  Yet, for example, Karpathy's mini-GPT implementation seems to use two different matrices:
  self.tok_emb = nn.Embedding(config.vocab_size, config.n_embd) # <--- This would be E
  self.pos_emb = nn.Parameter(torch.zeros(1, config.block_size, config.n_embd))
  self.drop = nn.Dropout(config.embd_pdrop)
  # transformer
  self.blocks = nn.Sequential(*[Block(config) for _ in range(config.n_layer)])
  # decoder head
  self.ln_f = nn.LayerNorm(config.n_embd)
  self.head = nn.Linear(config.n_embd, config.vocab_size, bias=False) # <--- This has the same dimensions as Etranspose but is clearly a different matrix
  
  
  Q2. If it is correct, how does can it work?
  This seems to be tasking $E$ with two very different, even opposing, functions:
  
  Map vocab to their meaning on the input side; higher magnitude indicates "more meaning"
  Map meaning to the most likely vocab on the output side; higher magnitude indicates greater likelihood
  
  When outputting, we want the softmax to be highest when the word is most likely; magnitude of the output matrix should be roughly proportional to how likely the word is two appear.
  Yet, when inputting, magnitude has nothing to do with likelihood.  Magnitude on the input side captures some element of meaning: perhaps how extreme or intense the meaning is, perhaps another aspect (not necessarily easily interpreted).
  
  **Tags:** natural-language-processing, transformer, word-embedding, embeddings, gpt
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2023-04-20. Score: 2 | Accepted answer
  
  A GPT produces output based on its own previous output, so it must be able to understand its output.
  
  The learning input is provided as a stream of tokens, and these tokens are defined before learning starts. So it has to use the same set of tokens to understand its own output. The set of possible output tokens is fixed, it learns only to assign probabilities of the next token.
  
  Looking on it in a per token perspective, when it gets a token as input, it learns that it has a non-zero probability to be following the previous input. If the same happens again, it learned that the probability is higher than previously assumed. The probability is expressed as a number of occurrences in the input, and a number per token in the output, but both are about the relative probability of tokens following the previous text.
  
  
  ---
  
  ### Answer by username_2 on 2022-08-21. Score: 0
  
  Yes, GPT uses the same embedding matrix. See [here](https://www.cs.ubc.ca/%7Eamuham01/LING530/papers/radford2018improving.pdf).
  
  [![enter image description here](https://i.sstatic.net/K4q6K.png)](https://i.sstatic.net/K4q6K.png)
  
  Regarding your second question - on the input side, a given token selects one row from the token embedding matrix; it is not clear that higher magnitude signifies anything. On the output side, the magnitude of the output vector $h\_n$ also doesn't signify anything. However, once you take the dot product of the output vector with each of the previous "rows" of the token embedding matrix, this gives you a dot-product similarity of the computed output with the pre-stored embeddings. A large value for a given token element means the model is more likely to predict that token.
  
  > **Comments:**
  > **username_3**: Please don't upload text as images, it isn't searchable. Use [MathJax](https://quantumcomputing.meta.stackexchange.com/a/152/278) whenever possible. Thanks.  
  
  ---
  
  