Answers.Body: <p>I think that the idea of &quot;reduced effective resolution&quot; from averaging is best understood through seeing how the proposed multi-head attention architecture fixes the issue. Specifically, multi-head attention is a concatenation of independently calculated but lower dimension attention outputs.</p>
  <p>When your data's underlying structure is lower dimensional this should be fine for the most part when it comes to representing the data, but the benefit comes from the feedforward layer right after the attention mechanism — whereas single head attention would return what can be interpreted as some average of features that the layer is attending to, multihead attention outputs what are basically separate lower dimensional vectors, where the span of these is accessible to the ff layer. So while technically the dimension of the output of the layer is the same, it more easily preserves information about the structure of its input for the following layer.</p>
  <p>Sequence aligned recurrences in CNNs and RNNs don't have this exact same issue, but information is still diluted through the potentially many operations (i.e. other layers / neurons) between what a layer is attending to and itself.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-02-20T20:04:27.473Z
Answers.LastActivityDate: 2024-02-27T19:35:51.847Z
Answers.LastEditDate: 2024-02-27T19:35:51.847Z
Answers.UserDisplayName: Andrew Du
Body: <p>I was reading this <a href="http://nlp.seas.harvard.edu/annotated-transformer/" rel="nofollow noreferrer">blog post from Harvard</a> and it says in its background paragraph about transformers that the number of operations required to relate signals from two arbitrary input or output positions doesn't grow in the distance between positions with the transformers architecture <strong>but this comes at the &quot;cost of reduced effective resolution due to averaging attention-weighted positions&quot;</strong>.</p>
  <p>I don't understand why does the averaging of attention-weighted position reduces the effective resolution. I have developed below the calculations needed to get the attention output, for the sake of simplicity I'm only considering one head:</p>
  <p>The definition is:</p>
  <p><span class="math-container">$A = Attention(Q, K, V) = softmax(\frac{QK^{T}}{\sqrt{d_{k}}})V$</span></p>
  <p>I think we can see this in two ways, either we see that <span class="math-container">$softmax(\frac{QK^{T}}{\sqrt{d_{k}}})$</span> is an <span class="math-container">$(m, m)$</span> matrix if <span class="math-container">$m$</span> is the number of input tokens, let us denote this matrix <span class="math-container">$M$</span>, and since <span class="math-container">$V$</span> is a the input matrix <span class="math-container">$X$</span> multiplied by a weight matrix <span class="math-container">$W^{V}$</span>, that gives us <span class="math-container">$A = M*X*W^{V}$</span> and we can clearly see that every element of <span class="math-container">$A$</span> is bound to all element in <span class="math-container">$X$</span> and that makes the resolution as high as possible.</p>
  <p>We can also see it if we completely develop all the calculations inside the matrix <span class="math-container">$A$</span>. I will do just one element here for the sake of simplicity: <span class="math-container">$A_{1,1} = \sum_{j=1}^{m}\frac{e^{Q_{1}K_{j}^{T}}V_{j, 1}}{\beta_{1}}$</span></p>
  <p>Where <span class="math-container">$m$</span> is the number of input tokens, <span class="math-container">$Q_{i}, K_{j}$</span> are the i-th and j-th rows of <span class="math-container">$Q$</span> and <span class="math-container">$K$</span> respectively and <span class="math-container">$v_{j, 1}$</span> is the first element of the j-th row in <span class="math-container">$V$</span>. And <span class="math-container">$\beta_{1} = \sum_{j=1}^{m}e^{Q_{1}K_{j}^{T}}$</span> (the softmax denominator).</p>
  <p>Let's say <span class="math-container">$X$</span>, the input matrix is of shape <span class="math-container">$(m, d_{e})$</span>, then <span class="math-container">$Q = X*W^{Q}$</span> is of shape <span class="math-container">$(m, d_{k})$</span> and <span class="math-container">$K = X*W^{K}$</span> is of shape <span class="math-container">$(m, d_{k})$</span> too if we consider that <span class="math-container">$W^{Q}$</span> and <span class="math-container">$W^{K}$</span> are both of shape <span class="math-container">$(d_{e}, d_{k})$</span>.</p>
  <p>Now, If we examine the product <span class="math-container">$Q_{1}K_{j}^{T}$</span>, it is <span class="math-container">$\sum_{l=1}^{d_{k}}Q_{1,l}K_{j,l}^{T}=\sum_{l=1}^{d_{k}}(\sum_{p=1}^{d_{e}}X_{1,p}W_{p,l}^{Q})(\sum_{p=1}^{d_{e}}X_{j,p}W_{p,l}^{k})$</span>, we can see that with the indices <span class="math-container">$p$</span> going from <span class="math-container">$1$</span> to <span class="math-container">$d_{e}$</span> and the <span class="math-container">$j$</span> index going from <span class="math-container">$1$</span> to <span class="math-container">$m$</span>, we cover all the elements of <span class="math-container">$X$</span> in the first position of <span class="math-container">$A$</span>. Granted that this is inside an exponential and that we divide by the softmax denominator. But if we develop the <span class="math-container">$V_{j, 1}$</span>, it is <span class="math-container">$\sum_{p=1}^{d_{v}}X_{j,p}W_{p,1}^{V}$</span>, and again we cover all the elements of <span class="math-container">$X$</span>.</p>
  <p>My guess is that there is a subtlety in the claim of the article that stems from the &quot;effective resolution&quot;, but I wonder what a full resolution would be in that case, since even in the fully connected networks we have a linear transformation of inputs so we can argue that we're not directly using all the inputs.</p>
  <p>I hope someone can clarify this for me and help me gain more understanding of the transformers architecture.</p>
  <p>Thank you!</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-10-30T08:19:21.817Z
Comments.Text: Exactly the same question occurred to me on reading that para just now. Did you find an answer? The claim of compensation for so-called limited resolution by multi-head attention" was also interesting.
Comments.UserDisplayName: Julian Moore
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-04-02T21:30:35.080Z
Comments.Text: @JulianMoore sorry for my late reply, I didn't have an answer at the time. You can check out the answer to this question. I've also found the section named Reduced effective resolution and Multi-Head Attention [in this blog post](https://reinforcedknowledge.com/transformers-attention-is-all-you-need/) to answer this question as well.
Comments.UserDisplayName: Daviiid
Tags: <neural-networks><transformer><attention><weights><multilayer-perceptrons>
Title: Why does averaging attention-weighted positions reduce the effective resolution in transformers?
UserDisplayName: Daviiid
ThreadText: # Why does averaging attention-weighted positions reduce the effective resolution in transformers?
  
  (Asked by: username_0 on 2023-07-13)
  
  I was reading this [blog post from Harvard](http://nlp.seas.harvard.edu/annotated-transformer/) and it says in its background paragraph about transformers that the number of operations required to relate signals from two arbitrary input or output positions doesn't grow in the distance between positions with the transformers architecture **but this comes at the "cost of reduced effective resolution due to averaging attention-weighted positions"**.
  
  I don't understand why does the averaging of attention-weighted position reduces the effective resolution. I have developed below the calculations needed to get the attention output, for the sake of simplicity I'm only considering one head:
  
  The definition is:
  
  $A = Attention(Q, K, V) = softmax(\frac{QK^{T}}{\sqrt{d\_{k}}})V$
  
  I think we can see this in two ways, either we see that $softmax(\frac{QK^{T}}{\sqrt{d\_{k}}})$ is an $(m, m)$ matrix if $m$ is the number of input tokens, let us denote this matrix $M$, and since $V$ is a the input matrix $X$ multiplied by a weight matrix $W^{V}$, that gives us $A = M\*X\*W^{V}$ and we can clearly see that every element of $A$ is bound to all element in $X$ and that makes the resolution as high as possible.
  
  We can also see it if we completely develop all the calculations inside the matrix $A$. I will do just one element here for the sake of simplicity: $A\_{1,1} = \sum\_{j=1}^{m}\frac{e^{Q\_{1}K\_{j}^{T}}V\_{j, 1}}{\beta\_{1}}$
  
  Where $m$ is the number of input tokens, $Q\_{i}, K\_{j}$ are the i-th and j-th rows of $Q$ and $K$ respectively and $v\_{j, 1}$ is the first element of the j-th row in $V$. And $\beta\_{1} = \sum\_{j=1}^{m}e^{Q\_{1}K\_{j}^{T}}$ (the softmax denominator).
  
  Let's say $X$, the input matrix is of shape $(m, d\_{e})$, then $Q = X\*W^{Q}$ is of shape $(m, d\_{k})$ and $K = X\*W^{K}$ is of shape $(m, d\_{k})$ too if we consider that $W^{Q}$ and $W^{K}$ are both of shape $(d\_{e}, d\_{k})$.
  
  Now, If we examine the product $Q\_{1}K\_{j}^{T}$, it is $\sum\_{l=1}^{d\_{k}}Q\_{1,l}K\_{j,l}^{T}=\sum\_{l=1}^{d\_{k}}(\sum\_{p=1}^{d\_{e}}X\_{1,p}W\_{p,l}^{Q})(\sum\_{p=1}^{d\_{e}}X\_{j,p}W\_{p,l}^{k})$, we can see that with the indices $p$ going from $1$ to $d\_{e}$ and the $j$ index going from $1$ to $m$, we cover all the elements of $X$ in the first position of $A$. Granted that this is inside an exponential and that we divide by the softmax denominator. But if we develop the $V\_{j, 1}$, it is $\sum\_{p=1}^{d\_{v}}X\_{j,p}W\_{p,1}^{V}$, and again we cover all the elements of $X$.
  
  My guess is that there is a subtlety in the claim of the article that stems from the "effective resolution", but I wonder what a full resolution would be in that case, since even in the fully connected networks we have a linear transformation of inputs so we can argue that we're not directly using all the inputs.
  
  I hope someone can clarify this for me and help me gain more understanding of the transformers architecture.
  
  Thank you!
  
  **Tags:** neural-networks, transformer, attention, weights, multilayer-perceptrons
  
  > **Comments:**
  > **username_1**: Exactly the same question occurred to me on reading that para just now. Did you find an answer? The claim of compensation for so-called limited resolution by multi-head attention" was also interesting.  
  > **username_0**: @username_1 sorry for my late reply, I didn't have an answer at the time. You can check out the answer to this question. I've also found the section named Reduced effective resolution and Multi-Head Attention [in this blog post](https://reinforcedknowledge.com/transformers-attention-is-all-you-need/) to answer this question as well.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2024-02-20. Score: 3 | Accepted answer
  
  I think that the idea of "reduced effective resolution" from averaging is best understood through seeing how the proposed multi-head attention architecture fixes the issue. Specifically, multi-head attention is a concatenation of independently calculated but lower dimension attention outputs.
  
  When your data's underlying structure is lower dimensional this should be fine for the most part when it comes to representing the data, but the benefit comes from the feedforward layer right after the attention mechanism — whereas single head attention would return what can be interpreted as some average of features that the layer is attending to, multihead attention outputs what are basically separate lower dimensional vectors, where the span of these is accessible to the ff layer. So while technically the dimension of the output of the layer is the same, it more easily preserves information about the structure of its input for the following layer.
  
  Sequence aligned recurrences in CNNs and RNNs don't have this exact same issue, but information is still diluted through the potentially many operations (i.e. other layers / neurons) between what a layer is attending to and itself.
  
  
  ---
  
  