Self-Attention Summation and Loss of Information

In self-attention, the attention for a word is calculated as:

$$A(q, K, V) = \sum_{i} \frac{exp(q.k^{})}{\sum_{j} exp(q.k^{})}v^{}$$

My question is why we sum over the Softmax*Value vectors. Doesn't this lose information about which other words in particular are important to the word under consideration?

In other words, how does this summed vector point to which words are relevant?

For example, consider two extreme scenarios where practically the entire output depends on the attention vector of word $$x^{}$$, and one where it depends on the vector of word $$x^{}$$. It's possible that $$A(q, K, V)$$ has the exact same values in both scenarios.

I guess there is an assumption that $$A(q, K, V)$$ represents similarity between q and the words in the sentence. Actually $$A(q, K, V)$$ is encoding the relationships from q to the words in the sentence.

The information about which other words in particular are important to the word under consideration has been already encoded by $$q \cdot k^{}$$.

Suppose there is a sentence i love sushi, $$v^{}$$ is a positionally-encoded word-embedding vector for each word, and the word under consideration $$q = v^{<1>}$$ which corresponds with love.

$$v^{<0>} = i\\ v^{<1>} = love\\ v^{<2>} = sushi$$

$$scale(i)=\frac{exp(q.k^{})}{\sum_{j} exp(q.k^{})}$$ quantifies how strong the connection is between love and $$v^{}$$.

$$\frac{exp(q.k^{})}{\sum_{j} exp(q.k^{})}v^{}$$ or $$scale(i)v^{}$$ is scaling each vector $$v^{}$$ proportionally based on how strongly related $$v^{}$$ is to love.

Aggregating all the scaled vectors $$\sum_{i} scale(i)v^{}$$ creates a new vector $$A$$, which is encoding the relationships from q to the words in the sentence.

The vector $$A$$ goes through the neural network and the back-propagation on the scaling and aggregation operations are how the Transformer learns the relations between q and the sentence.

• So $v^{<i>}$ can be read as a word embedding vector? Isn't it calculated using $x^{<i>}$ and the weight matrix $W^{v}$, which is learned within a layer? I can see how the aggregated vector would contain the necessary information if the different $v^{<i>}$ were known outside the layer, but how would it work in this? Jul 29, 2021 at 10:58
• A complexity in Transformer is that there are multiple embeddings. 1. each literal word e.g "love" is converted into an integer $index$. 2. The $index$ extracts a word embedding vector $e$. 3. $e$ is added with the positional encoding into $x$. 4. $x$ and $W^v$ generate $v$. These $e$, $x$, $v$ can all be regarded as embedding. For instance, regarding the positionally encoded vector $x$, "sushi" and "pasta" in the same position in "i love sushi" and "i love pasta" will be in close proximity in it vector space $X$.
– mon
Jul 30, 2021 at 15:33
• If "word embedding" is meant to be in the context of word2vec in which $king - man = queen - woman$ stands, then $v$ is not such a "word embedding" vector. Such "word embedding" is $e$ which exists in a different embedding space from the one where $v$ exists.
– mon
Jul 30, 2021 at 15:44
• I think I understand what you mean. But as far as the layer that the summed values are passed to is concerned, it has the aggregated vector giving information about $v^{<i>}$, and has the values of $x$ for each word. But it doesn't have a mapping between them, right? As in, it doesn't have the information to understand how the different words $x$ lie in the vector space $v$. Jul 30, 2021 at 21:13

Here is a paragraph from Speech and Language Processing Ch 9.

to make effective use of these scores, we’ll normalize them with a softmax to create a vector of weights, $$\alpha_{ij}$$, that indicates the proportional relevance of each input to the input element $$i$$ that is the current focus of attention.

My intuition; the same reason for normalizing between layers. Speeds up and stabilizes the learning process by keeping the output (input for next layer) consistent with in a range.