Home / Natural Language Processing

How are Embeddings Generated?

Embeddings start as random noise. Training pulls co-occurring words together — press Train and watch clusters form in real time.

Training Corpus

Words that appear in the same sentence are treated as context pairs — the engine of the distributional hypothesis.

Training

Training State

Epoch

0

Avg pair distance

-

Embedding Space During Training

RANDOM INIT
Each epoch: words that co-occur in a sentence attract; words that never co-occur repel slightly. No labels, no dictionary — structure emerges from raw co-occurrence alone.

Overview

Embeddings start as random noise.

In this guide
  1. The distributional hypothesis
  2. Skip-gram and CBOW
  3. Negative sampling
  4. Learned from context, not assigned
  5. Word2Vec, concretely
  6. What the window size decides
  7. Trained by prediction, not by design
  8. Guided tests
  9. Common mistakes
  10. What to recall
  11. Context embeddings, and why they superseded fixed ones
  12. Training your own
  13. Questions people ask
  14. Recap in one screen

How are Embeddings Generated?: A Practical Guide

Nobody hand-labels an embedding space. It falls out of a simple prediction task: guess a word from its neighbours, and the vectors that make you good at guessing turn out to encode meaning.

The distributional hypothesis

The whole field rests on one claim: a word is characterised by the company it keeps. Words appearing in close contexts tend to mean close things.

It is easy to check. Cat and dog both appear near pet, feed, vet and fur. Bulldozer does not. So if you build vectors that predict a word’s context well, words with close contexts must end up with close vectors. The semantic structure is a by-product of the prediction task, never an objective in itself. Word2Vec offers two designs: Skip-gram (predict context words from a centre word) and CBOW (predict the centre word from its context). Skip-gram works better on small datasets and rare words; CBOW is faster and works better on frequent words. Both give a weight matrix whose rows are the word embeddings. GloVe takes a different approach: it factorises the global word co-instance matrix. This captures the same distributional data but in a single matrix factorisation rather than iterative gradient updates. FastText extends Word2Vec by embedding character n-grams, so it can give vectors for words never seen in training by combining their subword pieces. All three give static embeddings (one vector per word no matter context). Context embeddings from transformers supersede them for most downstream tasks but are much more costly to compute. This page trains a small Skip-gram model on a sample corpus, shows the embedding vectors converging during training, and shows the vector-arithmetic property.

Skip-gram and CBOW

Word2Vec offers two ways to set up the prediction:

  • Skip-gram — given the centre word, predict the nearby words. From “the cat sat on the mat”, given sat, predict the, cat, on, the. Slower, and better on rare words because each instance makes a few training cases.
  • CBOW — given the nearby words, predict the centre. Faster, and better on frequent words.

The design is on purpose trivial: an input embedding layer, no hidden layer. And an output projection. The embedding matrix is the only thing anybody wants; the output layer is discarded after training. The vectors are a side effect of a task nobody cares about the answers to.

Negative sampling

The naive setup predicts a chance distribution over the whole vocabulary. This means a softmax over 50,000 words for each training case. That is prohibitively costly.

Negative sampling replaces it with a much cheaper question. Instead of “which of 50,000 words is the context?”, ask “is this pair a real (word, context) pair or a fabricated one?” — a binary classification. For each true pair, draw perhaps 5 to 20 random words as negatives and train the model to score the real pair high and the fakes low.

Cost drops from 50,000 output steps to about 20, and quality is comparable. This is what made training on billions of words practical.

Learned from context, not assigned

Nobody writes embedding vectors by hand. They are learned, and each method rests on the same observation: words that appear in close contexts tend to mean close things. Fill in "I poured myself a cup of ___" and the plausible answers. Tea, coffee, water. Are related just because they share contexts.

Turn that into a training objective and the vectors fall out.

MethodObjectiveOutput
Word2Vec skip-gramPredict surrounding words from a centre wordOne vector per word
Word2Vec CBOWPredict the centre word from its contextOne vector per word
GloVeFactorise a co-occurrence count matrixOne vector per word
FastTextSkip-gram over character n-gramsHandles unseen words
BERT-stylePredict masked tokens in contextA different vector per occurrence
Sentence encodersBring related sentences togetherOne vector per sentence

Word2Vec, concretely

Skip-gram takes a centre word and tries to predict the words around it within a window. Training on "the quick brown fox jumps" with a window of 2 gives pairs: (brown, the), (brown, quick), (brown, fox), (brown, jumps).

The model is a shallow network: an embedding lookup. Then a projection to vocabulary size. After training, the projection layer is discarded and the embedding table is the product. The prediction task was only ever a means to shape those vectors.

The practical problem is the output layer. A softmax over 100,000 words for each training pair is prohibitively costly. So Word2Vec uses negative sampling: instead of scoring all words, score the true context word and a handful (5–20) of random ones, and train the model to tell them apart. That turns a 100,000-way softmax into a few binary classifications and is the trick that made the method practical.

CBOW is the mirror image — predict the centre word from the average of its context. It trains faster and does slightly worse on rare words, since averaging washes them out.

GloVe takes a different route fully: build a matrix of how often each word co-occurs with each other word. Then factorise it so that the dot product of two vectors approximates the logarithm of their co-instance count. Global stats rather than local windows, with comparable results.

What the window size decides

This is the parameter that changes what "close" means. And it is worth knowing because it is easy to get wrong.

A small window (2–5) captures syntactic likeness — words that are alike in a sentence. "Run" ends up near "walk" and "jog", and near "runs" and "ran".

A large window (10–15) captures topical likeness — words that appear in the same subject matter. "Run" ends up near "race", "marathon" and "athlete".

Neither is correct broadly. For a part-of-speech task, small. For document retrieval or topic work, large. Setting it without thinking about which kind of likeness the task needs is how embeddings end up subtly unfit for purpose.

Other parameters that matter: dimension (100–300 for word vectors), minimum count (drop words appearing fewer than five times), and subsampling of very frequent words. This stops "the" ruling each context.

Trained by prediction, not by design

Nobody writes an embedding table. It falls out of training a model on a prediction task. And this trains one with skip-gram negative sampling -- the word2vec objective -- so you can watch the geometry appear.

example_01.pyNumPy
Output

Building on the same setup, the next block shows the left cases.

example_02.pyNumPy
Output

The below block covers the left steps.

example_03.pyNumPy
Output

The next block goes on with the left demonstrations.

example_04.pyNumPy
Output

Guided tests

  1. Start from noise. Press Re-randomize and look at the layout. Vectors are random, so the arrangement is meaningless — no structure exists before training.
  2. Train one epoch. Press Train 1 Epoch and watch points shift slightly. Each step nudges words that co-occur closer together and pushes unrelated ones apart.
  3. Let clusters form. Press Train 20 Epochs. Related words gather into groups. Nothing told the model these words were related. It only ever tried to predict context.
  4. Re-randomize and train again. The clusters re-form, but in different spots and orientations. The absolute coordinates are arbitrary; only the relative geometry carries meaning. This is why you cannot compare vectors from two apart trained models.

Common mistakes

  • Comparing vectors across models. Two training runs give different, incompatible spaces. Vectors are only meaningful next to others from the same run.
  • Too little data. These methods need tens of millions of tokens. On a small corpus the vectors are noise, and a pretrained set is almost always better.
  • Leaving frequent words unsubsampled. The and of co-occur with all of it and carry almost no data. Word2Vec subsamples them aggressively, which improves both speed and quality.
  • Assuming a bigger window is better. Small windows (2–5) capture syntactic likeness; large windows (10+) capture topical relatedness. They are different notions of “close” and the right one depends on the task.

What to recall

Embeddings are learned by training a on purpose simple model on a proxy task. Predict a word from its context. Or the context from the word. And keeping the embedding matrix while discarding all of it else. Semantic structure emerges because words in close contexts need close vectors to make the prediction work. Negative sampling replaces the vocabulary-wide softmax with a cheap binary decision. This is what made the whole approach scale.

Context embeddings, and why they superseded fixed ones

Word2Vec gives "bank" one vector, averaging the river and the financial senses into something that is neither.

A transformer gives a different vector for each instance, computed from the nearby words. In "the river bank" the vector has absorbed "river". In "the savings bank" it has absorbed "savings". Polysemy is resolved by construction.

The training objective is what makes them:

  • Masked language modelling (BERT) hides 15% of tokens and predicts them from both directions.
  • Next-token prediction (GPT) predicts each token from those before it.

Either way, the hidden states of the trained model are the context embeddings. And no separate embedding-training step is needed.

For sentence-level vectors, taking BERT's [CLS] output right works poorly. It was not trained to make close sentences nearby. Sentence-BERT fixes this by fine-tuning with a contrastive objective on pairs of related and unrelated sentences. This is why purpose-built embedding models much beat pooled hidden states from a general model.

Training your own

Two situations justify it, and neither is the common case.

An embedding layer inside your model. This is the everyday one. A high-cardinality categorical feature (product id, user id) replaced by a learned dense vector, trained jointly with the rest of the network. No separate pretraining, no corpus needed.

emb = nn.Embedding(num_embeddings=50_000, embedding_dim=64)
vecs = emb(item_ids)          # learned along with everything else

Domain-set text embeddings. When your vocabulary truly differs from web text. Clinical notes, legal filings, internal product codes. Fine-tuning an existing sentence encoder on your own pairs beats a general model. Contrastive fine-tuning needs pairs of related and unrelated text, which can often be mined from existing structure (a question and its accepted answer, a ticket and its resolution).

Training word vectors from scratch on a small corpus is almost always worse than using a pretrained model. Embeddings need a great deal of text.

Questions people ask

Do I need to train embeddings myself? Rarely for text. Use a pretrained sentence encoder. Yes for categorical features in your own model.

Skip-gram or CBOW? Skip-gram for rare words and smaller corpora; CBOW when speed matters and the corpus is large.

What is negative sampling? Replacing a full softmax over the vocabulary with a few binary checks against random words. It is what made Word2Vec tractable.

Why do I get different vectors on each run? Random initialisation and sampling. The geometry is stable even though the coordinates are not. So similarities are reproducible and single dimensions are not.

Can embeddings handle words not in the vocabulary? Word2Vec cannot. FastText can, by composing character n-grams. Subword tokenisers sidestep the problem fully.

How much text do I need? Millions of words for usable word vectors from scratch. Almost none if you fine-tune a pretrained model.

Recap in one screen

  • Embeddings are learned from the principle that words in close contexts have close meanings.
  • Skip-gram predicts context from a word; CBOW does the reverse; GloVe factorises co-instance counts.
  • Negative sampling replaces an unaffordable softmax with a few binary checks.
  • Window size chooses between syntactic and topical likeness. Pick it for the task.
  • Context embeddings from transformers give a different vector per instance, which resolves polysemy.

Recall check

0 of 3

Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.

  1. What does this module say about “The distributional hypothesis”?

  2. What does this module say about “Negative sampling”?

  3. What does this module say about “Learned from context, not assigned”?

Cheat sheet

How are Embeddings Generated?

Predict a word from its neighbours with Skip-gram or CBOW and watch the weight matrix converge into vectors where meaning is geometry.

NLP · vizlearn.in/natural_language_processing/how_are_embeddings_generated.html

Further reading

About the author

Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.