Token embedding (machine learning): Difference between revisions
Fix spelling and an unclosed delimiter that broke rendering of the blockquote (via update-page on MediaWiki MCP Server) |
Align the example with the notation: the embedding vector is e_1, not the index i (via update-page on MediaWiki MCP Server) |
||
| (4 intermediate revisions by the same user not shown) | |||
| Line 5: | Line 5: | ||
== Embedding and unembedding [[function]]s == | == Embedding and unembedding [[function]]s == | ||
Let $t_i \in T$ be [[token (machine learning)|token]]s obtained from [[tokenisation (machine learning)|tokenisation]] of raw data. | Let $t_i \in \mathcal{T}$ be [[token (machine learning)|token]]s obtained from [[tokenisation (machine learning)|tokenisation]] of raw data. | ||
Let $\mathbb{E}\equiv \mathbb{R}^ | Let $\mathbb{E}\equiv \mathbb{R}^d$ be a [[vector space]]. | ||
Then '''vector embedding function''' | Then '''vector embedding function''' | ||
$$ | $$ | ||
\operatorname{E}: T \to \mathbb{E}\\ | \operatorname{E}: \mathcal{T} \to \mathbb{E}\\ | ||
\forall t_i \in T: t_i \mapsto \vec{e}_i | \forall t_i \in \mathcal{T}: t_i \mapsto \vec{e}_i | ||
$$ | $$ | ||
$\mathbb{E}$ is then known as the | $\mathbb{E}$ is then known as the $d$-dimensional '''embedding space''' of $\operatorname{E}$, which contains '''embedding vectors''' $\vec e_i$. | ||
Similarly, | Similarly, | ||
$$ | $$ | ||
\operatorname{U}: \mathbb{E} \to T \\ | \operatorname{U}: \mathbb{E} \to \mathcal{T} \\ | ||
\forall \vec e_i \in \mathbb{E}: \vec e_i \mapsto t_i | \forall \vec e_i \in \mathbb{E}: \vec e_i \mapsto t_i | ||
$$ | $$ | ||
| Line 31: | Line 31: | ||
=== Embedding matrix === | === Embedding matrix === | ||
Let $\mathbf{W}_E \in \mathbb{R}^{|\mathcal{V}| \times | Let $\mathbf{W}_E \in \mathbb{R}^{|\mathcal{V}| \times d}$ be a '''vector embedding matrix'''. | ||
$$ | $$ | ||
| Line 41: | Line 41: | ||
Let $\vec o$ denote the output vector. | Let $\vec o$ denote the output vector. | ||
Let $\mathbf{W}_U \in \mathbb{R}^{ | Let $\mathbf{W}_U \in \mathbb{R}^{d \times |\mathcal{V}|}$ be a '''vector unembedding matrix'''. | ||
$$ | $$ | ||
| Line 94: | Line 94: | ||
$$ | $$ | ||
\vec | \vec e_1 = \mathbf{W}_E[1]=[0.2,\ 0.5,\ -0.1] | ||
$$ | $$ | ||
| Line 109: | Line 109: | ||
<blockquote> | <blockquote> | ||
With a weight-tied $\mathbf{W}_U, unembedding is essentially calculating the [[vector dot product]]s $\ell_i$ of output $\vec o$ with the embedding vectors $\mathbf{W}_E[i]$ for each [[token]] $t_i$ in the vocabulary. As $\vec o \cdot \mathbf{W}_E[ | With a weight-tied $\mathbf{W}_U$, unembedding is essentially calculating the [[vector dot product]]s $\ell_i$ of output $\vec o$ with the embedding vectors $\mathbf{W}_E[i]$ for each [[token]] $t_i$ in the vocabulary. As $\vec o \cdot \mathbf{W}_E[i] = \| \vec o \| \| \mathbf{W}_E[i] \| \cos \theta$, where $\theta$ is the angle between $\vec o$ and $\mathbf{W}_E[i]$, one expects $\ell_i$ to be larger when the directions of $\vec o$ and $\mathbf{W}_E[i]$ align, signifying a higher possibility the model's output predicts token $t_i$. | ||
</blockquote> | </blockquote> | ||
Latest revision as of 15:43, 11 November 2026



Embedding and unembedding functions
Let $t_i \in \mathcal{T}$ be tokens obtained from tokenisation of raw data.
Let $\mathbb{E}\equiv \mathbb{R}^d$ be a vector space.
Then vector embedding function
$$\begin{gathered} \operatorname{E}: \mathcal{T} \to \mathbb{E}\\ \forall t_i \in \mathcal{T}: t_i \mapsto \vec{e}_i \end{gathered}$$
$\mathbb{E}$ is then known as the $d$-dimensional embedding space of $\operatorname{E}$, which contains embedding vectors $\vec e_i$.
Similarly,
$$\begin{gathered} \operatorname{U}: \mathbb{E} \to \mathcal{T} \\ \forall \vec e_i \in \mathbb{E}: \vec e_i \mapsto t_i \end{gathered}$$
is the unembedding function.
Matrix representation
Embedding matrix
Let $\mathbf{W}_E \in \mathbb{R}^{|\mathcal{V}| \times d}$ be a vector embedding matrix.
$$ \vec{e}_i = \mathbf{W}_E[t_i]. $$
Unembedding matrix
Let $\vec o$ denote the output vector.
Let $\mathbf{W}_U \in \mathbb{R}^{d \times |\mathcal{V}|}$ be a vector unembedding matrix.
$$ \mathrm{logits} = \vec o \mathbf{W}_U. $$
Frequently, one sets $\mathbf{W}_U=\mathbf{W}_E^T$, a practice known as weight-tying.