How Embedding Turns Each Token into a Vector
From one-hot to distributed representations: how the table is learned, what its geometry means, and why it does not yet contain context.
Contents
- Why an id is not enough
- Where embeddings came from
- What an embedding table looks like
- Vector similarity and arithmetic
- How the table is trained with a Transformer
- How much the table costs to store
- The token vector does not include the input context
- Other input types: images and audio
- Summary: what leaves Embedding
- References
The tokenizer returns a sequence such as [0, 4]. Those numbers are identifiers, not measurements: id 0 is not more similar to id 1 than to any other token. Adding two ids does not combine their meanings either; it is just arithmetic on labels. To take advantage of neural network operations, we need a different representation.
Embedding places each token in a continuous numerical space with many dimensions. The word refers to embedding a discrete object as a point in a vector space. Hi is no longer just id 0: it acquires coordinates such as [0.80, 0.70, 0.10, …].
In that space, comparing vectors, adding them, or multiplying them by matrices has an operational meaning that ids lack. The network can measure alignment, combine information, and transform learned features. These operations do not guarantee a clean semantic interpretation, but they let the model use whatever relationships training has encoded in the coordinates.
Why an id is not enough
Before dense embeddings, one-hot vectors were a common way to represent categories. With a four-item vocabulary, each item receives a length-four vector containing a single one:
Hi → [1, 0, 0, 0]
Hello → [0, 1, 0, 0]
! → [0, 0, 1, 0]
How → [0, 0, 0, 1]
A real vocabulary requires one position per token, almost all of them zero. Distinct tokens also end up equally separated: the dot product between any two different one-hot vectors is zero. Nothing in that representation says that Hi and Hello can occur in similar contexts or that Hi and ! play different roles.
A dense embedding uses far fewer dimensions than the vocabulary and allows nonzero values throughout them. Instead of an expanded label, the network receives a point in a continuous space where it can compute dot products, sums, and transformations.
One dimension would only order tokens along a line. Many dimensions let them differ along several directions and share some properties without being identical in others. Training decides which directions are useful. We do not need to assign a concept to every axis for these coordinates to support operations that isolated ids cannot.
Where embeddings came from
An n-gram estimates the next word using only the preceding words. In the dog barks, a bigram uses dog to estimate barks; a trigram can use the dog. Learning means counting continuations in a corpus. As the context grows, finding the exact same sequence in the data becomes less likely, even when similar meanings occur in different sentences.
Learning coordinates to share information between those contexts did not start with Transformers. In 2003, Bengio and his collaborators introduced a neural language model that learned distributed word representations while estimating sequence probabilities [1]. Unlike n-grams, two contexts could share knowledge when their words had nearby representations, even if the exact combination was absent from the data.
Word2Vec made it much easier to train vectors for reuse in other tasks. In its skip-gram variant, training starts with a central word and learns to predict nearby words. With negative sampling, the vectors are adjusted to distinguish observed contexts from sampled negative examples [2]. Words occurring in similar environments tend to receive compatible representations because they help solve similar exercises.
GloVe took another route in 2014. Rather than relying only on local windows, it used global co-occurrence statistics and trained vectors to capture relationships among those counts [3]. Neither Word2Vec nor GloVe assigns word properties by hand. They learn parameters from corpus regularities with different objectives.
These methods allowed a table to be trained separately and connected to another model later. That separation was useful when general pretraining and the final task were decoupled. A modern LLM can reuse previously learned representations, but its input table is usually adjusted together with the whole network for its own training objective.
What an embedding table looks like
An embedding table is a matrix: one row per token and one column per coordinate. The columns are not named “animal” or “grammar”; they contain numbers. If we show just the beginning of two rows, the structure looks like this, with invented values for the example:
| Token (id) | |||||
|---|---|---|---|---|---|
Hi (0) | 0.80 | 0.70 | 0.10 | −0.30 | |
! (4) | 0.10 | −0.20 | −0.60 | 0.40 |
Each complete row is a vector. The ellipses stand for coordinates we are not showing, not a special operation. The number of columns is , the model width, and it varies considerably between architectures:
| Model | Dimensions per vector |
|---|---|
| GPT-2 small [6] | 768 |
| Llama 3 8B [7] | 4,096 |
| Llama 3 70B [7] | 8,192 |
| Llama 3 405B [7] | 16,384 |
So when we write [0.80, 0.70, 0.10, …], we are only showing the beginning of a row that may contain thousands of values. There is no standard number of dimensions shared by all models.
Call the table . The representation for token id is obtained by selecting its row:
Multiplying a one-hot vector by would select exactly the same row. Implementations do not need to materialize that huge vector or perform the full multiplication: they read the corresponding row directly, an operation known as lookup or gather.
For Hi!, the block retrieves two rows and stacks them in input order. If each vector has width , the output has shape ; in general, sequence length × model width. Neither row reads the other and the block does not compare tokens.
During ordinary inference, the values in stay fixed. They were adjusted during training like the model’s other weights. Every occurrence of the same id retrieves the same row until another layer changes that representation.
Vector similarity and arithmetic
Tokens occurring in similar contexts can end up with nearby vectors. A common measure of closeness is cosine similarity:
It compares vector directions and ranges from to . High similarity does not make two rows synonyms. hot and cold can be close because they occur in similar environments despite describing opposite temperatures. Not all embeddings are normalized, and cosine is not the only useful measure of closeness.
The Word2Vec authors showed that some relationships could be recovered through vector arithmetic: king - man + woman produced a vector close to queen [9]. To see what “close” means, we can repeat the calculation with another table: GloVe trained on Wikipedia and Gigaword, distributed by Gensim as 400,000 vectors with 50 dimensions [10]. This is neither the original paper’s table nor the token table of a particular LLM; the result always depends on the vectors used.
We calculate coordinate by coordinate. The table shows only the first five coordinates; the arithmetic and similarities use all fifty:
| Vector | |||||
|---|---|---|---|---|---|
king | 0.50451 | 0.68607 | −0.59517 | −0.02280 | 0.60046 |
man | −0.09439 | 0.43007 | −0.17224 | −0.45529 | 1.64470 |
woman | −0.18153 | 0.64827 | −0.58210 | −0.49451 | 1.54150 |
| Result | 0.41737 | 0.90427 | −1.00503 | −0.06202 | 0.49726 |
Actual queen | 0.37854 | 1.82330 | −1.26480 | −0.10430 | 0.35829 |
The result is not identical to queen: compare , where the arithmetic gives 0.90427 while queen has 1.82330. Across all fifty coordinates, their cosine similarity is 0.861; vectors pointing in exactly the same direction would score 1. The vectors differ, but queen can still be the nearest neighbor. Comparing with the entire vocabulary, excluding the three input words, produces this ranking:
| Rank | Word | Cosine similarity with |
|---|---|---|
| 1 | queen | 0.861 |
| 2 | daughter | 0.768 |
| 3 | prince | 0.764 |
| 4 | throne | 0.763 |
| 5 | princess | 0.751 |
The sign therefore does not mean that the equation returns the exact queen vector. It means that, for this table, a nearest-neighbor search finds queen first. Another table, different words, or another analogy offer no guarantee of the expected result.
The learned space can also preserve associations and biases from its corpus because training does not distinguish a useful regularity from one we would prefer not to reproduce. These operations expose statistical regularities; they do not turn every direction into a stable concept or make the table a reliable knowledge base.
How the table is trained with a Transformer
Initially, the rows contain small random values. On each training example, Embedding supplies the input vectors, the Transformer blocks transform them, and LM head produces a prediction. The error between that prediction and the target token propagates backward, so one update adjusts the rows used in the example as well as the matrices throughout the rest of the network.
Nobody first writes a coordinate for “animal”, another for “gender”, and another for “age”. The dimensions emerge as part of a numerical solution to the training objective. Some directions may become interpretable after analysis, but column names are not assigned in advance and one property can be distributed across many components.
I find it useful to think of the vector as a way to represent what does not fit in one label. The Spanish word sobremesa needs an explanation in English: the time spent talking around the table after a meal. The German word Schadenfreude also needs more than a one-word translation. A vector does not automatically capture those nuances, but it offers many coordinates for encoding regularities instead of reducing every token to a discrete symbol.
A row also corresponds to a token, not necessarily a word or complete concept. If the tokenizer splits a word into three subwords, Embedding retrieves three rows. The meaning of the complete word must be constructed from that sequence. Rare tokens can also have less reliable representations if they appear only a few times during training.
How much the table costs to store
With vocabulary tokens and components per vector, the table contains parameters. With the 128,000-token vocabulary and published for Llama 3 8B [7], that is 524.288 million values. Stored as 16-bit floating-point numbers, they would occupy about 1.05 GB in decimal units before counting other model structures.
More dimensions allow richer representations, but they increase memory and the width of the vectors processed by every layer. They do not guarantee better quality. The appropriate size depends on the vocabulary, data, objective, and training and inference budgets.
Some models reduce parameters by sharing the table with the output. At the input, supplies a token vector; at the output, the same rows can score the final hidden vector through dot products. This choice is called weight tying. It connects input and output representations but is not mandatory: other models learn a separate output matrix [5].
The token vector does not include the input context
A row can encode learned properties without yet representing the sentence being processed. The vector for bank contains regularities about how that token is used and how it relates to others, but the lookup receives only its id: it does not see the surrounding words. It therefore returns the same initial vector in river bank and bank account.
Patterns associated with different senses can coexist in that row. What does not yet exist is a representation of this occurrence of bank, selecting the relevant properties from the concrete input. The vector has a learned distributed meaning, but it does not yet have the sentence context.
Contextual representations were developed to address this limitation. ELMo, for example, built vectors from the internal states of a bidirectional language model, so a word’s representation changed with its use [4]. In a Transformer, the initial lookup is still static; later layers combine the vectors and produce different representations for each occurrence.
Position is not stored in the row either. Order is incorporated through separate mechanisms: some models add positional information to the input, while others introduce it inside Attention. Embedding therefore preserves an ordered stack of rows, but it does not yet determine which positions should consult one another.
Other input types: images and audio
Imagine a photo of a spider. A vision encoder does not have to turn it directly into the word “spider”. It can divide the image into a grid of patches: some contain part of a leg, others the background, and others the body. It then transforms each patch into a vector and processes that sequence while accounting for spatial layout.
For audio, the example uses a real 0.615-second recording of “hola”. I resampled it to 16 kHz and, without padding the ends, divided it into 25 ms windows with a 10 ms hop. The overlapping windows produce 60 frames. For each one, I calculated the log energy in 40 mel frequency bands. The result is a matrix: sixty vectors containing forty numbers each.
The spectrogram does not assign a letter or phoneme to each window. It shows how energy changes across time and frequency. These values are computed acoustic features, not learned embeddings yet: a later encoder transforms them into representations useful for its objective. Other models may use different parameters or learn discrete units instead of consuming this matrix directly.
This is a conceptual pattern, not a universal recipe. The Gemini 1.0 report describes Google models trained on text, images, audio, and video from the beginning, accepting interleaved modalities [8]. Video enters as a sequence of frames, audio can be ingested directly through USM features at 16 kHz, and visual resolution can vary to allocate more compute to fine detail. This differs from attaching an encoder to an already finished text model.
The report does not disclose every internal tokenization detail. The fixed grid and windows in the diagram should therefore not be read as a literal description of Gemini, but as a way to understand how a non-textual signal can become a processable sequence.
Summary: what leaves Embedding
Embedding turns every id into a learned dense row. For Hi!, it returns two independent vectors stacked into a matrix with shape sequence length × model width. Those rows can now participate in geometric operations and share learned regularities, but neither contains information about the other yet.
The next problem is building context without losing positions. Attention lets each vector incorporate information from other allowed positions.
References
- Bengio et al., “A Neural Probabilistic Language Model,” Journal of Machine Learning Research, 2003 (opens in a new tab).
- Mikolov et al., “Distributed Representations of Words and Phrases and their Compositionality,” NeurIPS 2013 (opens in a new tab).
- Pennington, Socher, and Manning, “GloVe: Global Vectors for Word Representation,” EMNLP 2014 (opens in a new tab).
- Peters et al., “Deep Contextualized Word Representations,” NAACL 2018 (opens in a new tab).
- Press and Wolf, “Using the Output Embedding to Improve Language Models,” EACL 2017 (opens in a new tab).
- OpenAI, GPT-2 small configuration:
n_embdandvocab_size(opens in a new tab). - Llama Team, “The Llama 3 Herd of Models,” 2024, Table 3 (opens in a new tab).
- Gemini Team, “Gemini: A Family of Highly Capable Multimodal Models,” 2023, updated 2025 (opens in a new tab).
- Mikolov et al., “Efficient Estimation of Word Representations in Vector Space,” ICLR 2013 (opens in a new tab).
- Gensim Data, “glove-wiki-gigaword-50”: 400,000 50-dimensional vectors trained on Wikipedia 2014 and Gigaword (opens in a new tab).