(One intermediate revision by the same user not shown) 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}^{n \times |\mathcal{V}|}$ be a '''vector unembedding matrix'''.
Let $\mathbf{W}_U \in \mathbb{R}^{d \times |\mathcal{V}|}$ be a '''vector unembedding matrix'''.
$$
$$
Line 94:
Line 94:
$$
$$
\vec i = \mathbf{W}_E[1]=[0.2,\ 0.5,\ -0.1]
\vec e_1 = \mathbf{W}_E[1]=[0.2,\ 0.5,\ -0.1]
$$
$$
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
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 .
Example
Setup
Vocabulary size $|\mathcal{V}|=4$
Tokens: cat, dog, fish, and bird
Embedding dimension: $d=3$
Embedding matrix $\mathbf{W}_E$ (shape $4 \times 3$):
dim1
dim2
dim3
cat
$0.2$
$0.5$
$-0.1$
dog
$-0.3$
$0.1$
$0.4$
fish
$0.6$
$-0.2$
$0.3$
bird
$-0.1$
$0.4$
$0.2$
Unembedding matrix (weight-tied) $\mathbf{W}_U = \mathbf{W}_E^\top$ (shape $3 \times 4$):
cat
dog
fish
bird
dim1
$0.2$
$-0.3$
$0.6$
$-0.1$
dim2
$0.5$
$0.1$
$-0.2$
$0.4$
dim3
$-0.1$
$0.4$
$0.3$
$0.2$
Embedding
Embed cat
$$
\vec e_1 = \mathbf{W}_E[1]=[0.2,\ 0.5,\ -0.1]
$$
Unembedding
Suppose after some operations, one obtains $\vec o=[-0.42,0.15,0.41]$.
Compute logits:
$$\begin{gathered}
\vec \ell = \vec o \mathbf{W}_U \\
= [-0.42,\ 0.15,\ 0.41] \begin{bmatrix} 0.2 & -0.3 & 0.6 & -0.1 \\ 0.5 & 0.1 & -0.2 & 0.4 \\ -0.1 & 0.4 & 0.3 & 0.2 \end{bmatrix} = [-0.05,\ 0.305,\ -0.159,\ 0.184].
\end{gathered}$$
With a weight-tied $\mathbf{W}_U$, unembedding is essentially calculating the vector dot products $\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$.
Calculate softmax probabilities:
$$
\vec{p} = \text{softmax}(\vec{\ell}) \approx [0.218,\ 0.311,\ 0.196,\ 0.276].
$$
Token
Probability $p_j$
cat
$0.218$
dog
$0.311$
fish
$0.196$
bird
$0.276$
Thus the model somehow transformed a cat into something that is between a cat, a dog, a fish, and a bird.