Answers.Body: <p><a href="https://stackoverflow.com/a/66652733/9067615">Multiple attention heads</a> in a single layer in a transformer is analogous to multiple kernels in a single layer in a CNN: they have the same architecture, and operate on the same feature-space, but since they are separate 'copies' with different sets of weights, they are hence 'free' to learn different functions.</p>
  <p>In a CNN this may correspond to different definitions of visual features, and in a Transformer this may correspond to different definitions of relevance:<sup>1</sup></p>
  <p>For example:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Architecture</th>
  <th>Input</th>
  <th>(Layer 1)<br>Kernel/Head 1</th>
  <th>(Layer 1)<br>Kernel/Head 2</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>CNN</td>
  <td>Image</td>
  <td>Diagonal <a href="https://en.wikipedia.org/wiki/Kernel_(image_processing)#Details" rel="nofollow noreferrer">edge-detection</a></td>
  <td>Horizontal edge-detection</td>
  </tr>
  <tr>
  <td>Transformer</td>
  <td>Sentence</td>
  <td>Attends to next word</td>
  <td>Attends from verbs to their direct objects</td>
  </tr>
  </tbody>
  </table>
  </div><hr />
  <p><strong>Notes:</strong></p>
  <sup>
  <ol>
  <li><p>There is no guarantee that these are human interpretable, but in many popular architectures they do map accurately onto linguistic concepts:</p>
  <blockquote>
  <p>While no single head performs well at many relations, we find that particular heads correspond remarkably well to particular relations. For example, we find heads that find direct objects of verbs, determiners of nouns, objects of prepositions, and objects of possesive pronouns...</p>
  <ul>
  <li><a href="https://www.aclweb.org/anthology/W19-4828/" rel="nofollow noreferrer">What Does BERT Look at? An Analysis of BERT’s Attention</a> (2019)</li>
  </ul>
  </blockquote>
  </li>
  <li><p>Multiple heads was originally proposed as a way to mitigate the lack of descriptive power of a single head in self-attention has:</p>
  <blockquote>
  <p>In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions [...] This makes it more difficult to learn dependencies between distant positions [12]. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention...</p>
  <ul>
  <li><a href="https://arxiv.org/abs/1706.03762" rel="nofollow noreferrer">Attention is All You Need</a> (2017)</li>
  </ul>
  </blockquote>
  </li>
  </ol>
  </sup>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-16T09:34:51.927Z
Answers.LastActivityDate: 2021-03-17T12:19:30.717Z
Answers.LastEditDate: 2021-03-17T12:19:30.717Z
Answers.UserDisplayName: brazofuerte
Answers.Body: <p>The reason each head is different is because they each learn a different set of weight matrices <span class="math-container">$\{ W_i^Q, W_i^K, W_i^V \}$</span> where <span class="math-container">$i$</span> is the index of the head. To clarify, the input to each attention head is the same. For attention head <span class="math-container">$i$</span>:</p>
  <p><span class="math-container">\begin{align}
  Q_i(x) &amp;= x W_i^Q \\
  K_i(x) &amp;= x W_i^K \\
  V_i(x) &amp;= x W_i^V \\
  \text{attention}_i(x) &amp;= \text{softmax} \left(\frac{Q_i(x) K_i(x)^T}{\sqrt{d_k}} \right) V_i(x).
  \end{align}</span></p>
  <p>Notice that the input to each head is <span class="math-container">$x$</span> (either the semantic + positional embedding of the decoder input for the first decoder layer, or the output of the previous decoder layer). <a href="http://jalammar.github.io/illustrated-transformer/" rel="nofollow noreferrer">More info</a></p>
  <p>The question as to why gradient descent learns each set of weight matrices <span class="math-container">$\{ W_i^Q, W_i^K, W_i^V \}$</span> to be different across each attention head is very similar to <a href="https://ai.stackexchange.com/questions/25109/is-there-anything-that-ensures-that-convolutional-filters-end-up-the-same/25111#25111">&quot;Is there anything that ensures that convolutional filters don't end up the same?&quot;</a>, so maybe you might find the answer there helpful for you:</p>
  <blockquote>
  <p>No, nothing really prevents the weights from being different. In practice though they end up almost always different because it makes the model more expressive (i.e. more powerful), so gradient descent learns to do that. If a model has n features, but 2 of them are the same, then the model effectively has n−1 features, which is a less expressive model than that of n features, and therefore usually has a larger loss function.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-12T23:26:09.893Z
Answers.Text: I think this problem would be a bit different from convolution networks. In convolution layers, the input to each kernel is the same. I thin in attention, the inputs are also different. Is it the case that we break the inputs into smaller chunks in attention?
Answers.UserDisplayName: mhsnk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-12T23:58:11.957Z
Answers.Text: The original transformer paper words it a bit confusing, but the input to each attention head is the same. For attention head $i$, $Q_i(x) = x W_i^Q$, $K_i(x) = x W_i^K$, and $V_i(x) = x W_i^V$. The output of attention head $i$ is $attention_i(x) = softmax \left(\frac{Q_i(x) K_i(x)^T}{\sqrt{d_k}} \right) V_i(x)$. The input to each head is $x$ (either the semantic + positional embedding of the decoder input for the first decoder layer, or the output of the previous decoder layer). For more info, read the section "Self-Attention in Detail": http://jalammar.github.io/illustrated-transformer/
Answers.UserDisplayName: user3667125
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-13T00:55:11.437Z
Answers.Text: I did more research into this and it seems that both ways exist in attention literature. We have "narrow self-attention" in which the original input is split into smaller chunks and each head get it's own small input. We also have "wide self-attention" in which the whole input gets fed into each head separately. Wide self-attention gives better results at the cost of memory and computation time. See http://peterbloem.nl/blog/transformers
Answers.UserDisplayName: mhsnk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-13T01:17:56.233Z
Answers.Text: Interesting, thanks for the share! Are there more publications on wide vs narrow self-attention? I wanted to research further, but couldn't find any literature or academic papers on it. I know that the original transformer code in Tensor2Tensor, and also BERT's code, uses attention the way I described (which is considered "wide self-attention")
Answers.UserDisplayName: user3667125
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-13T02:10:26.317Z
Answers.Text: No unfortunately that's all I could find.
Answers.UserDisplayName: mhsnk
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-12T22:39:41.727Z
Answers.LastActivityDate: 2024-08-06T04:48:37.527Z
Answers.LastEditDate: 2024-08-06T04:48:37.527Z
Answers.UserDisplayName: user3667125
Body: <p>I have a difficult time understanding the &quot;multi-head&quot; notion in the original <a href="https://papers.nips.cc/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf" rel="noreferrer">transformer paper</a>. What makes the learning in each head unique? Why doesn't the neural network learn the same set of parameters for each attention head? Is it because we break <em>query, key</em> and <em>value</em> vectors into smaller dimensions and feed each portion to a different head?</p>
  
Tags: <neural-networks><natural-language-processing><papers><transformer><attention>
Title: What is different in each head of a multi-head attention mechanism?
UserDisplayName: mhsnk
ThreadText: # What is different in each head of a multi-head attention mechanism?
  
  (Asked by: username_0 on 2020-12-12)
  
  I have a difficult time understanding the "multi-head" notion in the original [transformer paper](https://papers.nips.cc/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf). What makes the learning in each head unique? Why doesn't the neural network learn the same set of parameters for each attention head? Is it because we break *query, key* and *value* vectors into smaller dimensions and feed each portion to a different head?
  
  **Tags:** neural-networks, natural-language-processing, papers, transformer, attention
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2020-12-12. Score: 12 | Accepted answer
  
  The reason each head is different is because they each learn a different set of weight matrices $\{ W\_i^Q, W\_i^K, W\_i^V \}$ where $i$ is the index of the head. To clarify, the input to each attention head is the same. For attention head $i$:
  
  \begin{align}
  Q\_i(x) &= x W\_i^Q \\
  K\_i(x) &= x W\_i^K \\
  V\_i(x) &= x W\_i^V \\
  \text{attention}\_i(x) &= \text{softmax} \left(\frac{Q\_i(x) K\_i(x)^T}{\sqrt{d\_k}} \right) V\_i(x).
  \end{align}
  
  Notice that the input to each head is $x$ (either the semantic + positional embedding of the decoder input for the first decoder layer, or the output of the previous decoder layer). [More info](http://jalammar.github.io/illustrated-transformer/)
  
  The question as to why gradient descent learns each set of weight matrices $\{ W\_i^Q, W\_i^K, W\_i^V \}$ to be different across each attention head is very similar to ["Is there anything that ensures that convolutional filters don't end up the same?"](https://ai.stackexchange.com/questions/25109/is-there-anything-that-ensures-that-convolutional-filters-end-up-the-same/25111#25111), so maybe you might find the answer there helpful for you:
  
  > No, nothing really prevents the weights from being different. In practice though they end up almost always different because it makes the model more expressive (i.e. more powerful), so gradient descent learns to do that. If a model has n features, but 2 of them are the same, then the model effectively has n−1 features, which is a less expressive model than that of n features, and therefore usually has a larger loss function.
  
  > **Comments:**
  > **username_0**: I think this problem would be a bit different from convolution networks. In convolution layers, the input to each kernel is the same. I thin in attention, the inputs are also different. Is it the case that we break the inputs into smaller chunks in attention?  
  > **username_2**: The original transformer paper words it a bit confusing, but the input to each attention head is the same. For attention head $i$, $Q_i(x) = x W_i^Q$, $K_i(x) = x W_i^K$, and $V_i(x) = x W_i^V$. The output of attention head $i$ is $attention_i(x) = softmax \left(\frac{Q_i(x) K_i(x)^T}{\sqrt{d_k}} \right) V_i(x)$. The input to each head is $x$ (either the semantic + positional embedding of the decoder input for the first decoder layer, or the output of the previous decoder layer). For more info, read the section "Self-Attention in Detail": http://jalammar.github.io/illustrated-transformer/  
  > **username_0**: I did more research into this and it seems that both ways exist in attention literature. We have "narrow self-attention" in which the original input is split into smaller chunks and each head get it's own small input. We also have "wide self-attention" in which the whole input gets fed into each head separately. Wide self-attention gives better results at the cost of memory and computation time. See http://peterbloem.nl/blog/transformers  
  > **username_2**: Interesting, thanks for the share! Are there more publications on wide vs narrow self-attention? I wanted to research further, but couldn't find any literature or academic papers on it. I know that the original transformer code in Tensor2Tensor, and also BERT's code, uses attention the way I described (which is considered "wide self-attention")  
  > **username_0**: No unfortunately that's all I could find.  
  
  ---
  
  ### Answer by username_1 on 2021-03-16. Score: 3
  
  [Multiple attention heads](https://stackoverflow.com/a/66652733/9067615) in a single layer in a transformer is analogous to multiple kernels in a single layer in a CNN: they have the same architecture, and operate on the same feature-space, but since they are separate 'copies' with different sets of weights, they are hence 'free' to learn different functions.
  
  In a CNN this may correspond to different definitions of visual features, and in a Transformer this may correspond to different definitions of relevance:1
  
  For example:
  
  | Architecture | Input | (Layer 1) Kernel/Head 1 | (Layer 1) Kernel/Head 2 |
  | --- | --- | --- | --- |
  | CNN | Image | Diagonal [edge-detection](https://en.wikipedia.org/wiki/Kernel_(image_processing)#Details) | Horizontal edge-detection |
  | Transformer | Sentence | Attends to next word | Attends from verbs to their direct objects |
  
  ---
  
  **Notes:**
  
  1. There is no guarantee that these are human interpretable, but in many popular architectures they do map accurately onto linguistic concepts:
  
     > While no single head performs well at many relations, we find that particular heads correspond remarkably well to particular relations. For example, we find heads that find direct objects of verbs, determiners of nouns, objects of prepositions, and objects of possesive pronouns...
     >
     > * [What Does BERT Look at? An Analysis of BERT’s Attention](https://www.aclweb.org/anthology/W19-4828/) (2019)
  2. Multiple heads was originally proposed as a way to mitigate the lack of descriptive power of a single head in self-attention has:
  
     > In these models, the number of operations required to relate signals from two arbitrary input or output positions grows in the distance between positions [...] This makes it more difficult to learn dependencies between distant positions [12]. In the Transformer this is reduced to a constant number of operations, albeit at the cost of reduced effective resolution due to averaging attention-weighted positions, an effect we counteract with Multi-Head Attention...
     >
     > * [Attention is All You Need](https://arxiv.org/abs/1706.03762) (2017)
  
  
  ---
  
  