Answers.Body: <p>To answer this question, it makes sense to go over how multi-headed self attention works. Say we have 2 heads defined as follows (I am leaving small details out for simplicity):</p>
  <p><span class="math-container">$$W_q^1, W_k^1, \text{ and } W_v^1$$</span></p>
  <p><span class="math-container">$$W_q^2, W_k^2, \text{ and } W_v^2$$</span></p>
  <p>where the superscript defines the head number. We also have our token embeddings  <span class="math-container">$X$</span>, for our sequence.</p>
  <ol>
  <li>We multiply our embedding by each of the previously defined matrices to get our query, key, and value features for each head.</li>
  </ol>
  <p><span class="math-container">\begin{align}XW^i_q &amp;= Q^i \\ XW^i_k &amp;= K^i \\ XW^i_v &amp;= V^i\end{align}</span></p>
  <ol start="2">
  <li>We then preform the dot product attention between the key and values of each head</li>
  </ol>
  <p><span class="math-container">$$E^i=Q^i K^{i \top}$$</span></p>
  <ol start="3">
  <li>We then apply softmax row-wise to get our attention scores</li>
  </ol>
  <p><span class="math-container">$$A^i=softmax(E^i)$$</span></p>
  <ol start="4">
  <li><p>Then, using each row of attention scores, we get the linear combination of the corresponding value vectors in <span class="math-container">$V^i$</span></p>
  </li>
  <li><p>Then, to combine the outputs from each head, we concatenate or average the head outputs.</p>
  </li>
  <li><p>Finally, we throw these vectors into a linear layer, so that our final representation can encapsulate all the nuance that exists in the outputs from the two heads.</p>
  </li>
  </ol>
  <p><span class="math-container">$\textbf{The answer to your first question specifically pertains to the fourth step.}$</span> In that the attention scores that we created encapsulate the relationships between words, our value vectors hold the meaning to the words, and our linear combination creates a representation, from these aforementioned components that encapsulates the negation of some word. The ability of any transformer-based architecture, with appropriate regularization, to do this increases as we stack these multi-headed attention blocks on top of each other.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-01T19:35:23.717Z
Answers.Text: thank you Jayson. I already read a lot about the theory of how the attention mecanism and a transformer work. But i need a concrete example. That is why i used some fictives numbers. Could you use some concrete fictive numbers for QKV ?
Answers.UserDisplayName: John Smith
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-01T22:59:48.803Z
Answers.Text: I don't think it would be a good idea to just come up with a fictive example.  Rather, why don't you just grab a pretrained language transformer, plot the value vectors, and inspect how the attention scores interact with the value vectors to create expressive representations yourself, so you know exactly what's going on.
Answers.UserDisplayName: Jayson Meribe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-07-01T23:08:00.753Z
Answers.Text: Additionally, I think AttentionViz -- https://attentionviz.com/ -- might help you accomplish this quite easily
Answers.UserDisplayName: Jayson Meribe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-30T04:39:26.080Z
Answers.LastActivityDate: 2024-06-30T04:47:08.563Z
Answers.LastEditDate: 2024-06-30T04:47:08.563Z
Answers.UserDisplayName: Jayson Meribe
Body: <p>I'm trying to understand how transformer models, such as BERT or GPT, handle negation in sentiment analysis. Specifically, I'm curious about how these models manage to correctly interpret sentences where negation changes the sentiment, such as &quot;The movie is not good.&quot;</p>
  <p>A simple model using word embeddings + global averaging fails to handle negation properly. Intuitively, for example, if &quot;good&quot; has a positive sentiment score and &quot;bad&quot; has a negative sentiment score, a model might misinterpret &quot;not good&quot; by simply averaging the scores of &quot;not&quot; and &quot;good&quot;.</p>
  <h3>Example Without Negation</h3>
  <p>Consider the following sentences with sentiment words:</p>
  <ul>
  <li>&quot;The movie is good.&quot;</li>
  <li>&quot;The movie is awesome.&quot;</li>
  <li>&quot;The movie is terrible.&quot;</li>
  </ul>
  <p>Suppose we have the following word embeddings representing sentiment scores:</p>
  <ul>
  <li>&quot;good&quot; = [10]</li>
  <li>&quot;awesome&quot; = [12]</li>
  <li>&quot;terrible&quot; = [-10]</li>
  </ul>
  <p>Neutral words (assuming embeddings around 0):</p>
  <ul>
  <li>&quot;the&quot; = [0]</li>
  <li>&quot;movie&quot; = [0]</li>
  <li>&quot;is&quot; = [0]</li>
  </ul>
  <p>For these sentences, a simple global average of the sentiment scores works well:</p>
  <ul>
  <li>&quot;The movie is good&quot; = average([0, 0, 0, 10]) = 10 / 4 = 2.5 (positive sentiment)</li>
  <li>&quot;The movie is awesome&quot; = average([0, 0, 0, 12]) = 12 / 4 = 3 (positive sentiment)</li>
  <li>&quot;The movie is terrible&quot; = average([0, 0, 0, -10]) = -10 / 4 = -2.5 (negative sentiment)</li>
  </ul>
  <h3>Example With Negation</h3>
  <p>Now, consider the sentence &quot;The movie is not good.&quot; In this case, the sentiment should be negative due to the presence of &quot;not.&quot; However, averaging the scores naively might not handle this correctly. For example:</p>
  <ul>
  <li>&quot;The movie is not good&quot; = average([0, 0, 0, -5 (for not), 10 (for good)]) = (0 + 0 + 0 - 5 + 10) / 5 = 5 / 5 = 1 (incorrectly positive)</li>
  <li>&quot;The movie is not bad&quot; = average([0, 0, 0, -5, -9]) = (0 + 0 + 0 - 5 - 9) / 5 = -14 / 5 = -2.8 (incorrectly negative)</li>
  </ul>
  <h3>How Transformers Handle Negation</h3>
  <p>Can someone explain, with a concrete example, how a transformer model like BERT or GPT can correctly understand and model negation in a sentence? Specifically, I'm interested in:</p>
  <ul>
  <li>How the self-attention mechanism captures the relationship between words like &quot;not&quot; and &quot;good&quot;.</li>
  <li>An example with numerical values to illustrate the process.</li>
  </ul>
  
Tags: <transformer><attention><gpt><bert>
Title: How do transformer models handle negation in sentiment analysis
UserDisplayName: John Smith
ThreadText: # How do transformer models handle negation in sentiment analysis
  
  (Asked by: username_0 on 2024-06-25)
  
  I'm trying to understand how transformer models, such as BERT or GPT, handle negation in sentiment analysis. Specifically, I'm curious about how these models manage to correctly interpret sentences where negation changes the sentiment, such as "The movie is not good."
  
  A simple model using word embeddings + global averaging fails to handle negation properly. Intuitively, for example, if "good" has a positive sentiment score and "bad" has a negative sentiment score, a model might misinterpret "not good" by simply averaging the scores of "not" and "good".
  
  ### Example Without Negation
  
  Consider the following sentences with sentiment words:
  
  * "The movie is good."
  * "The movie is awesome."
  * "The movie is terrible."
  
  Suppose we have the following word embeddings representing sentiment scores:
  
  * "good" = [10]
  * "awesome" = [12]
  * "terrible" = [-10]
  
  Neutral words (assuming embeddings around 0):
  
  * "the" = [0]
  * "movie" = [0]
  * "is" = [0]
  
  For these sentences, a simple global average of the sentiment scores works well:
  
  * "The movie is good" = average([0, 0, 0, 10]) = 10 / 4 = 2.5 (positive sentiment)
  * "The movie is awesome" = average([0, 0, 0, 12]) = 12 / 4 = 3 (positive sentiment)
  * "The movie is terrible" = average([0, 0, 0, -10]) = -10 / 4 = -2.5 (negative sentiment)
  
  ### Example With Negation
  
  Now, consider the sentence "The movie is not good." In this case, the sentiment should be negative due to the presence of "not." However, averaging the scores naively might not handle this correctly. For example:
  
  * "The movie is not good" = average([0, 0, 0, -5 (for not), 10 (for good)]) = (0 + 0 + 0 - 5 + 10) / 5 = 5 / 5 = 1 (incorrectly positive)
  * "The movie is not bad" = average([0, 0, 0, -5, -9]) = (0 + 0 + 0 - 5 - 9) / 5 = -14 / 5 = -2.8 (incorrectly negative)
  
  ### How Transformers Handle Negation
  
  Can someone explain, with a concrete example, how a transformer model like BERT or GPT can correctly understand and model negation in a sentence? Specifically, I'm interested in:
  
  * How the self-attention mechanism captures the relationship between words like "not" and "good".
  * An example with numerical values to illustrate the process.
  
  **Tags:** transformer, attention, gpt, bert
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2024-06-30. Score: 0
  
  To answer this question, it makes sense to go over how multi-headed self attention works. Say we have 2 heads defined as follows (I am leaving small details out for simplicity):
  
  $$W\_q^1, W\_k^1, \text{ and } W\_v^1$$
  
  $$W\_q^2, W\_k^2, \text{ and } W\_v^2$$
  
  where the superscript defines the head number. We also have our token embeddings $X$, for our sequence.
  
  1. We multiply our embedding by each of the previously defined matrices to get our query, key, and value features for each head.
  
  \begin{align}XW^i\_q &= Q^i \\ XW^i\_k &= K^i \\ XW^i\_v &= V^i\end{align}
  
  2. We then preform the dot product attention between the key and values of each head
  
  $$E^i=Q^i K^{i \top}$$
  
  3. We then apply softmax row-wise to get our attention scores
  
  $$A^i=softmax(E^i)$$
  
  4. Then, using each row of attention scores, we get the linear combination of the corresponding value vectors in $V^i$
  5. Then, to combine the outputs from each head, we concatenate or average the head outputs.
  6. Finally, we throw these vectors into a linear layer, so that our final representation can encapsulate all the nuance that exists in the outputs from the two heads.
  
  $\textbf{The answer to your first question specifically pertains to the fourth step.}$ In that the attention scores that we created encapsulate the relationships between words, our value vectors hold the meaning to the words, and our linear combination creates a representation, from these aforementioned components that encapsulates the negation of some word. The ability of any transformer-based architecture, with appropriate regularization, to do this increases as we stack these multi-headed attention blocks on top of each other.
  
  > **Comments:**
  > **username_0**: thank you username_1. I already read a lot about the theory of how the attention mecanism and a transformer work. But i need a concrete example. That is why i used some fictives numbers. Could you use some concrete fictive numbers for QKV ?  
  > **username_1**: I don't think it would be a good idea to just come up with a fictive example.  Rather, why don't you just grab a pretrained language transformer, plot the value vectors, and inspect how the attention scores interact with the value vectors to create expressive representations yourself, so you know exactly what's going on.  
  > **username_1**: Additionally, I think AttentionViz -- https://attentionviz.com/ -- might help you accomplish this quite easily  
  
  ---
  
  