Answers.Body: <p>Short answer, yes I believe we can! One way feels more meaningful that the other. First, let's look at some nuance in the definition of attention. If <span class="math-container">$\text{score}(x_i, x_j) = \text{score}(x_j, x_i)$</span>, then the attention matrix is symmetric and naturally has the form of a weighted adjacency matrix. For example, this happens when attention is given by a simple dot product <span class="math-container">$\text{score}(x_i, x_j) = \langle x_i, x_j \rangle = x_i^Tx_j$</span>. This also happens if we have a learnable matrix <span class="math-container">$A$</span> and <span class="math-container">$\text{score}(x_i, x_j) = x_i^TAx_j$</span> if <span class="math-container">$A$</span> is symmetric. We can then view the attention matrix <span class="math-container">$\alpha_{i,j} = \text{Attn}_{i,j}(X)$</span> as a weighted adjacency matrix where the nodes represent input tokens, and edge weights correspond to similarity scores (as defined by the inner product, scaled inner product, or the symmetric matrix <span class="math-container">$A$</span>). Now, for the following definition of attention, this makes a little less sense, as the edge connecting token nodes in graph corresponding to tokens <span class="math-container">$x_i$</span> and <span class="math-container">$x_j$</span> is not the same as the weight on the edge connecting <span class="math-container">$x_j$</span> to <span class="math-container">$x_i$</span>, in general. Suppose <span class="math-container">$X \in \mathbb{R}^{d \times n}$</span> has as columns <span class="math-container">$X_i$</span> the <span class="math-container">$d$</span>-dimensional embeddings of the <span class="math-container">$n$</span>-tokens <span class="math-container">$x_1, x_2, ..., x_n$</span> from your input. Now, let</p>
  <p><span class="math-container">$$W_QX = Q$$</span>
  <span class="math-container">$$W_KX = K$$</span>
  <span class="math-container">$$W_VX = V$$</span></p>
  <p>be the learned weight matrices giving the &quot;query&quot; <span class="math-container">$q_i = W_QX_i$</span>, &quot;key&quot; <span class="math-container">$k_i = W_KX_i$</span>, and &quot;value&quot; <span class="math-container">$v_i = W_VX_i$</span> vectors. Then we can define attention as</p>
  <p><span class="math-container">\begin{align}
  \text{Attn}(X) &amp;= \text{softmax}\left( \frac{Q^TK}{\sqrt{d}} \right)V \\
                 &amp;= \text{softmax}_j\left(\text{score}(x_i, x_j)\right)
  \end{align}</span></p>
  <p>From this we can derive,</p>
  <p><span class="math-container">$$ \text{Attn}_{i,j}(X) = \frac{\exp \left( \frac{\langle q_i, k_j \rangle}{\sqrt{d}}\right)}{\sum_k \exp\left( \frac{\langle q_i, k_k \rangle}{\sqrt{d}} \right)}.$$</span></p>
  <p>Now, note, we have turned each column into a probability distribution by applying the softmax so we have</p>
  <p><span class="math-container">$$
  P(X_i) = \text{softmax}\begin{pmatrix}
  \frac{\langle q_i, k_1 \rangle}{\sqrt{d}}\\
  \frac{\langle q_i, k_2 \rangle}{\sqrt{d}}\\
  \vdots \\
  \frac{\langle q_i, k_n \rangle}{\sqrt{d}}
  \end{pmatrix}.
  $$</span></p>
  <p>Now, if we adjust for masked self attention, we can represent the attention mechanism as a directed graph (with weighted self-loops) as the entries in the attention matrix above the diagonal are zero, and so all edges are directed at token nodes that come &quot;after&quot; them.</p>
  <p>There is another way to understand attention using graphs when we view attention through the lens of (complete) graph attention networks as explained <a href="https://docs.dgl.ai/en/0.8.x/tutorials/models/4_old_wines/7_transformer.html" rel="nofollow noreferrer">here</a>.</p>
  <p>There is also a visualization you can find <a href="https://colab.research.google.com/drive/1hXIQ77A4TYS4y3UthWF-Ci7V7vVUoxmQ?usp=sharing#scrollTo=-QnRteSLP0Hm" rel="nofollow noreferrer">here</a> that shows the attention mechanism as a graph. It is equivalent to the graph attention formulation, but is more interactive and pretty.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-03-14T01:08:20.607Z
Answers.LastActivityDate: 2023-04-03T00:03:33.133Z
Answers.LastEditDate: 2023-04-03T00:03:33.133Z
Answers.UserDisplayName: Amelie Schreiber
Body: <p>Are multi-head attention matrices weighted adjacency matrices?</p>
  <p>The job of the multi-head-attention mechanism in transformer models is to determine how likely a word is to appear after another word. In a sense this makes the resulting matrix a big graph with nodes and edges, where a node represents a word and an edge the likelihood to appear after that. So basically it is an adjacency matrix that is created.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-13T11:57:19.863Z
Comments.Text: Hello. Please, edit your post to provide more context (this post has been flagged as "needs more details") and explain why you're asking this question (what confuses you?).
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-10-18T10:12:13.273Z
Comments.Text: No, that isn't the job of the multi-head-attention mechanism.
Comments.UserDisplayName: Criticize SE actions means ban
Tags: <deep-learning><transformer><attention>
Title: Is the multi-head attention in the transformer a weighted adjacency matrix?
UserDisplayName: MrPlanck
ThreadText: # Is the multi-head attention in the transformer a weighted adjacency matrix?
  
  (Asked by: username_0 on 2021-10-12)
  
  Are multi-head attention matrices weighted adjacency matrices?
  
  The job of the multi-head-attention mechanism in transformer models is to determine how likely a word is to appear after another word. In a sense this makes the resulting matrix a big graph with nodes and edges, where a node represents a word and an edge the likelihood to appear after that. So basically it is an adjacency matrix that is created.
  
  **Tags:** deep-learning, transformer, attention
  
  > **Comments:**
  > **username_1**: Hello. Please, edit your post to provide more context (this post has been flagged as "needs more details") and explain why you're asking this question (what confuses you?).  
  > **username_2**: No, that isn't the job of the multi-head-attention mechanism.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2023-03-14. Score: 1
  
  Short answer, yes I believe we can! One way feels more meaningful that the other. First, let's look at some nuance in the definition of attention. If $\text{score}(x\_i, x\_j) = \text{score}(x\_j, x\_i)$, then the attention matrix is symmetric and naturally has the form of a weighted adjacency matrix. For example, this happens when attention is given by a simple dot product $\text{score}(x\_i, x\_j) = \langle x\_i, x\_j \rangle = x\_i^Tx\_j$. This also happens if we have a learnable matrix $A$ and $\text{score}(x\_i, x\_j) = x\_i^TAx\_j$ if $A$ is symmetric. We can then view the attention matrix $\alpha\_{i,j} = \text{Attn}\_{i,j}(X)$ as a weighted adjacency matrix where the nodes represent input tokens, and edge weights correspond to similarity scores (as defined by the inner product, scaled inner product, or the symmetric matrix $A$). Now, for the following definition of attention, this makes a little less sense, as the edge connecting token nodes in graph corresponding to tokens $x\_i$ and $x\_j$ is not the same as the weight on the edge connecting $x\_j$ to $x\_i$, in general. Suppose $X \in \mathbb{R}^{d \times n}$ has as columns $X\_i$ the $d$-dimensional embeddings of the $n$-tokens $x\_1, x\_2, ..., x\_n$ from your input. Now, let
  
  $$W\_QX = Q$$
  $$W\_KX = K$$
  $$W\_VX = V$$
  
  be the learned weight matrices giving the "query" $q\_i = W\_QX\_i$, "key" $k\_i = W\_KX\_i$, and "value" $v\_i = W\_VX\_i$ vectors. Then we can define attention as
  
  \begin{align}
  \text{Attn}(X) &= \text{softmax}\left( \frac{Q^TK}{\sqrt{d}} \right)V \\
  &= \text{softmax}\_j\left(\text{score}(x\_i, x\_j)\right)
  \end{align}
  
  From this we can derive,
  
  $$ \text{Attn}\_{i,j}(X) = \frac{\exp \left( \frac{\langle q\_i, k\_j \rangle}{\sqrt{d}}\right)}{\sum\_k \exp\left( \frac{\langle q\_i, k\_k \rangle}{\sqrt{d}} \right)}.$$
  
  Now, note, we have turned each column into a probability distribution by applying the softmax so we have
  
  $$
  P(X\_i) = \text{softmax}\begin{pmatrix}
  \frac{\langle q\_i, k\_1 \rangle}{\sqrt{d}}\\
  \frac{\langle q\_i, k\_2 \rangle}{\sqrt{d}}\\
  \vdots \\
  \frac{\langle q\_i, k\_n \rangle}{\sqrt{d}}
  \end{pmatrix}.
  $$
  
  Now, if we adjust for masked self attention, we can represent the attention mechanism as a directed graph (with weighted self-loops) as the entries in the attention matrix above the diagonal are zero, and so all edges are directed at token nodes that come "after" them.
  
  There is another way to understand attention using graphs when we view attention through the lens of (complete) graph attention networks as explained [here](https://docs.dgl.ai/en/0.8.x/tutorials/models/4_old_wines/7_transformer.html).
  
  There is also a visualization you can find [here](https://colab.research.google.com/drive/1hXIQ77A4TYS4y3UthWF-Ci7V7vVUoxmQ?usp=sharing#scrollTo=-QnRteSLP0Hm) that shows the attention mechanism as a graph. It is equivalent to the graph attention formulation, but is more interactive and pretty.
  
  
  ---
  
  