Concepts / Word2Vec and Similarity in Embedding Space

Word2Vec and Similarity in Embedding Space

The dimensionality of a word embedding is the number of values in the vector that represents one word, and it ranges from one to thousands in practice.

  • Programming
Interactive lab

Try it: Text Vectorization: Tokens to Vectors

The text-to-tensor pipeline: standardize, split into tokens, build a frequency-ranked vocabulary with padding and [UNK], map tokens to integer ids, pad or truncate, then one-hot or multi-hot encode and look the ids up in an embedding table.

How it works

  1. Standardize: lowercase and delete punctuation.
  2. Split on whitespace into tokens.
  3. Build the vocabulary: 0 = padding, 1 = [UNK], then words by frequency (ties: first appearance), capped at max tokens.
  4. Map each token to its id (1 if it did not make the vocabulary), then pad with 0 or truncate to the sequence length.
  5. Encode: one-hot rows per position or one multi-hot row per text; embed: id k selects row k of the embedding table.

Default run (18 steps): Raw text (59 characters): "The movie was great. The plot was great, the acting wasn't!". A model cannot read strings, so it is turned into numbers step by step. … Embedding lookup: id k selects row k of a fixed 8 x 3 table, giving a (8, 3) dense matrix instead of (8, 8) sparse one-hots. Row 0 (padding) is looked up too; a trained Embedding layer would learn these rows.

Simplified: Bounded ASCII text (up to 200 characters, 40 tokens), whitespace splitting only, and a small FIXED embedding table (a real Embedding layer learns its rows). The multi-hot vector keeps the padding slot (always 0) so its positions line up with the ids.

Educational simulation

Loading the simulation…

Asking How Big an Embedding Is

Suppose someone hands you a word embedding and asks how big it is. You might expect the answer to describe the word itself, but an embedding is simply a list of numbers attached to that word. The size of that list is what we call the dimensionality of the embedding. In this article we will trace, step by step, what that size controls, why it matters for the relationships an embedding can express, and why bigger is not automatically better.

What do you think happens?

If a word is represented by a one-dimensional embedding, how many numbers describe that word?

  • One

  • Two

  • As many as the vocabulary has words

Reveal answer
Answer:

One

A one-dimensional embedding gives each word a single number. A two-dimensional embedding gives each word a pair of numbers. Only the count of values per word changes; the words stay the same.

Keep one question in mind as you read: if each word is described by a list of numbers, what happens to the meaning captured by those numbers when the list gets longer or shorter?

Reading the Slots of a Word Vector

Dimensionality is the number of value slots in the vector that represents one word. Each slot holds one number, and the vector is the full ordered list of those numbers. Counting the slots gives the dimensionality, and the source notes that in practice this count ranges from one to thousands.

slot 1one numberslot 2one numberslot 3one numberand so onslot 768one number
What does a vector with 768 values look like, and what does each position in the vector represent? Each numbered slot holds exactly one value, and the total number of slots is the dimensionality.

Read the diagram as a single word. Its 768 slots together form one embedding. Changing the dimensionality changes how many slots exist, but not which word the vector belongs to. A word with a 2-dimensional embedding has two slots, and a word with a 1-dimensional embedding has one.

Why Position Carries Meaning in a Plane

A two-dimensional embedding can be drawn directly. Each word becomes a point on a flat plane, with one number setting its horizontal position and the other setting its vertical position. Once words are points, we can look at which points sit close together. The source explains that when a technique such as Word2Vec is used, words for similar concepts tend to land near one another in the embedding space.

The plotted example in the source groups several kinds of birds in one region of the plane, and it places a country and its capital city as a pair in another region. Comparative forms of the word long form a third group. The useful information is in the arrangement of the points: words that share a meaning or role sit in the same neighborhood.

Nuance Versus Computational Cost

Two numbers per word are enough for a picture, but they leave little room to represent the many ways words relate to one another. The source states that a higher dimensionality might capture more nuanced relationships. Each extra dimension is another value the embedding can use to separate words that differ in subtle ways, so a richer vector has more room to record fine distinctions.

That extra room has a price. The same source notes that higher dimensionality comes at the cost of computational efficiency. Every word now carries more numbers, so storing the embeddings takes more memory, and every calculation that touches them does more arithmetic. Designers therefore weigh how much nuance they need against how much computation they can afford.

In a low-dimensional space, different meanings are forced to overlap because there are few slots to separate them. In a high-dimensional space, words have more room to differ along separate aspects of meaning. The source frames this as a tradeoff rather than a rule that more is always better.

Why High-Dimensional Vectors Cannot Be Drawn

The plane works because two dimensions fit on a page. Our senses and ordinary graphs are limited to three dimensions or fewer, so a vector with hundreds of values cannot be drawn as points in one picture. The source explains that this limit is exactly why the earlier example used a two-dimensional embedding in a two-dimensional scatterplot.

When we move to large language models, the embeddings have a dimensionality far higher than what we can plot. Visualizing them directly is therefore not practical, and you should not expect to see the full vector of a large model as a picture. Any picture of such an embedding has to be a reduced view of it, and that reduction is a separate step from the embedding itself.

Embedding Sizes in GPT-2 and GPT-3

The source uses two members of the GPT family to make the idea concrete. For both GPT-2 and GPT-3, the embedding size, often called the dimensionality of the model's hidden states, changes with the specific variant and size of the model. Larger models generally use larger embeddings, and the source frames the choice as a tradeoff between performance and efficiency.

Model familyEmbedding size point from sourceRelative position
GPT-2 (smallest models)768 dimensionsLower end of the range
GPT-3 (largest model)12,288 dimensionsUpper end of the range

Embedding sizes at the two ends of the range given in the source

Worked example

Estimating the gap between the smallest and largest sizes

The source gives 768 dimensions for the smallest GPT-2 models and 12,288 dimensions for the largest GPT-3 model. How many times larger is the largest embedding than the smallest?

  1. 1
    Identify the two values

    The smallest is 768 and the largest is 12,288.

  2. 2
    Divide the larger by the smaller

    12,288 divided by 768 is 16, since 768 times 16 equals 12,288.

  3. 3
    Interpret the result

    The largest embedding has 16 times as many values per token as the smallest.

Result

The largest GPT-3 embedding is 16 times the dimensionality of the smallest GPT-2 embedding.

Where the Size Is Set in an LLM

Dimensionality is chosen as part of how a large language model turns text into numbers. The source points to the steps that come next: splitting text into words, converting words into tokens, and turning tokens into embedding vectors. The embedding size is the length of the final vector produced in that last step, so it sets the shape that every later stage of the model must handle.

TextWordssplit the textTokensconvert wordsEmbedding vectorssize set here
How does the embedding size enter the path from raw text to the vectors the model works with? The size is decided at the final step, when each token becomes an embedding vector.

Mistakes Learners Often Make

  • Common mistake

    Thinking the dimensionality describes the meaning of the word

    What it looks like

    Assuming a 768-dimensional embedding is a definition of a word written in 768 parts.

    Why it fails

    The source describes an embedding as simply a list of numbers attached to a word. The size of the list is the dimensionality.

    Do this instead

    Treat the dimensionality as a count of value slots, and treat meaning as something captured by the values and their arrangement.

  • Common mistake

    Assuming bigger embeddings are always better

    What it looks like

    Choosing the largest possible size for every application.

    Why it fails

    Higher dimensionality may capture more nuance but costs computational efficiency, so size is a tradeoff.

    Do this instead

    Weigh the nuance needed against the storage and computation available.

  • Common mistake

    Expecting to see a large model's full embedding as a picture

    What it looks like

    Trying to plot a GPT-3 embedding with 12,288 values directly.

    Why it fails

    Human perception and ordinary graphs are limited to three dimensions or fewer.

    Do this instead

    Use a reduced view of the embedding, and remember that the reduction is separate from the embedding itself.

  • Common mistake

    Reading meaning from the axes of a plot

    What it looks like

    Trying to say what the horizontal direction of a word plot stands for.

    Why it fails

    The source notes the axes do not carry a simple label; the arrangement of points is what matters.

    Do this instead

    Look at which points cluster together and which are far apart.

Tracing a Word Through the Size Choice

EASY

A model's word embedding for one token has 768 values. A second model uses 12,288 values per token. Explain, in your own words, which quantity the two numbers describe, and state one benefit and one cost the larger model might have according to the source.

Hints
  • The dimensionality is the number of values in the vector for one token.
  • The source ties higher dimensionality to more nuanced relationships and to lower computational efficiency.

Key Takeaways

  • The dimensionality of an embedding is the number of values in the vector that represents one word, and it ranges from one to thousands in practice.
  • Words with similar meanings or roles tend to land near one another in the embedding space, and in a two-dimensional plot the arrangement of points carries the information.
  • Higher dimensionality may capture more nuanced relationships between words, but it costs computational efficiency, so the size is a tradeoff.
  • GPT-2 smallest models use 768 dimensions and the largest GPT-3 model uses 12,288 dimensions, and high-dimensional embeddings cannot be visualized directly.

Related concepts

More concepts starting with W