Jump to content

Token embedding (machine learning): Difference between revisions

From Wikibase
Harmonise notation: token set becomes script T, embedding dimension n becomes d, so that T means sequence length and d the width throughout (via update-page on MediaWiki MCP Server)
Embedding dimension n to d (via update-page on MediaWiki MCP Server)
Line 31: Line 31:
=== Embedding matrix ===
=== Embedding matrix ===


Let $\mathbf{W}_E \in \mathbb{R}^{|\mathcal{V}| \times n}$ be a '''vector embedding matrix'''.
Let $\mathbf{W}_E \in \mathbb{R}^{|\mathcal{V}| \times d}$ be a '''vector embedding matrix'''.


$$
$$

Revision as of 15:43, 11 November 2026

Languages: English · français · Esperanto

A visual representation of embedding of sub-word tokens in natural language processing (NLP). · Open in a new tab
Each dimension of an embedding space encodes some semantic meaning. As such, words with similar meanings are spatially adjacent to each other. · Open in a new tab
Since each dimension encodes some semantic meaning, vector addition/subtraction can add/extract meaning to tokens and relate them to other tokens. · Open in a new tab

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}^{n \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.

Example