Self-attention: Difference between revisions
Category renamed to the singular form (via update-page on MediaWiki MCP Server) |
No edit summary |
||
| Line 1: | Line 1: | ||
[[Category: | [[Category:Transformer]] | ||
'''Self-attention''' is the process through which a [[Transformer]] enriches its [[token embedding (machine learning)|token embedding vectors]] with contextual information derived from other tokens in the same token sequence.<ref>{{#cite:Q3130}}</ref> | |||
'''Self-attention''' | |||
== Definition == | == Definition == | ||
Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ | Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ be the '''vector embedding matrix''' of [[token (machine learning)|token sequence]] $X$, in which row $t$ is the embedding vector for the token at position $t$. | ||
$$ | $$ | ||
| Line 15: | Line 14: | ||
$$ | $$ | ||
where $\mathbf{W}_Q,\mathbf{W}_K,\mathbf{W}_V\in\mathbb{R}^{d\times d_k}$ are '''weight matrices''' learnt during training (e.g., via [[backpropagation]]). | |||
Then the stadard attention score | |||
$$ | $$ | ||
\operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) | S_\text{attention}=\operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) | ||
= | = | ||
\operatorname{softmax}\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\mathbf{V}, | \operatorname{row-wise softmax}\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\mathbf{V}, | ||
$$ | $$ | ||
<blockquote> | |||
The [[Softmax|softmax]] is taken row-wise so for each embedding vector $\vec x_t$ within $X$, $\sum_{i=1}^{d_k} (S_{\text{attention})_{t,i} =1$. | |||
The scaling factor $1/\sqrt{d_k}$ keeps the scores from growing with the dimension. | |||
Both are [[normalisation function]]s applied for numerical stability. | |||
</blockquote> | |||
<ref>{{#cite:Q3130}}</ref> | |||
== Complexity == | |||
The score matrix $\mathbf{Q}\mathbf{K}^{\top}$ is $T\times T$, so attention over a context of length $T$ costs $\mathcal{O}(T^2 d)$ time and $\mathcal{O}(T^2)$ memory. Generating a sequence recomputes most of this work unless the keys and values computed and cached, such as via [[KV caching]]. | |||
<ref>{{#cite:Q3130}}</ref> | |||
Revision as of 09:14, 12 November 2026
Self-attention is the process through which a Transformer enriches its token embedding vectors with contextual information derived from other tokens in the same token sequence.[1]
Definition
Let $\mathbf{X}\in\mathbb{R}^{T\times d}$ be the vector embedding matrix of token sequence $X$, in which row $t$ is the embedding vector for the token at position $t$.
$$ \mathbf{Q}=\mathbf{X}\mathbf{W}_Q, \qquad \mathbf{K}=\mathbf{X}\mathbf{W}_K, \qquad \mathbf{V}=\mathbf{X}\mathbf{W}_V. $$
where $\mathbf{W}_Q,\mathbf{W}_K,\mathbf{W}_V\in\mathbb{R}^{d\times d_k}$ are weight matrices learnt during training (e.g., via backpropagation).
Then the stadard attention score $$ S_\text{attention}=\operatorname{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V}) = \operatorname{row-wise softmax}\left(\frac{\mathbf{Q}\mathbf{K}^{\top}}{\sqrt{d_k}}\right)\mathbf{V}, $$
The softmax is taken row-wise so for each embedding vector $\vec x_t$ within $X$, $\sum_{i=1}^{d_k} (S_{\text{attention})_{t,i} =1$.
The scaling factor $1/\sqrt{d_k}$ keeps the scores from growing with the dimension.
Both are normalisation functions applied for numerical stability.
Complexity
The score matrix $\mathbf{Q}\mathbf{K}^{\top}$ is $T\times T$, so attention over a context of length $T$ costs $\mathcal{O}(T^2 d)$ time and $\mathcal{O}(T^2)$ memory. Generating a sequence recomputes most of this work unless the keys and values computed and cached, such as via KV caching.