Lensa ML
Lensa ML

CBOW

Step 1 of 6

Context Window

Selecting the input words

Context window size2
CBOW: predict center word from contextthecat[?]onthematwindow = ±2predict: satContext words → Average embeddings → Predict center wordthecatonthesat
WINDOW
±2
CONTEXT
4 words
TARGET
sat

Window ±2: CBOW averages 4 context word embeddings into a single vector, then predicts the center word "sat". This is the "bag" in CBOW — context word order doesn't matter.

Step 1 of 6: Context Window

Selecting the input words

Context window size2
CBOW: predict center word from contextthecat[?]onthematwindow = ±2predict: satContext words → Average embeddings → Predict center wordthecatonthesat
WINDOW
±2
CONTEXT
4 words
TARGET
sat

Window ±2: CBOW averages 4 context word embeddings into a single vector, then predicts the center word "sat". This is the "bag" in CBOW — context word order doesn't matter.

Step 2 of 6: Averaging Embeddings

From multiple vectors to one

Inspect context word0
Sentence: predict the center word from contextthecatsat▲ targetonthematContext embeddings → Average → Hidden statethecatontheembed("the")d0-0.285d1-0.483d2-0.112d3-0.183d4-0.289d5-0.260Σ/nAverage (hidden)-0.280-0.203+0.034-0.147-0.158+0.016avg = ("the" + "cat" + "on" + "the") / 4
TARGET
sat
INSPECTING
the
EMB DIM
6
CONTEXT SIZE
4

The embedding for "the" is a 6-dimensional vector. Each context word contributes equally to the average — this is the "bag" assumption. Slide to inspect each context word's embedding.

Step 3 of 6: Projection Layer

From hidden state to vocabulary scores

Embedding dimension6
Hidden vector × W_output → Logits over vocabularyavg (6d)-0.04-0.110.09-0.02-0.270.03×W_out (12×6)+6 rows=logits (12)-0.03-0.04-0.12-0.01-0.060.120.08-0.07-0.11-0.02-0.07-0.05Logit scores per vocabulary wordthe-0.03a-0.04cat-0.12dog-0.01sat-0.06ran+0.12on+0.08in-0.07down-0.11mat-0.02bed-0.07big-0.05
EMB DIM
6
VOCAB
12
PARAMS
144
TARGET LOGIT
-0.06

6 dimensions: the averaged context vector is multiplied by the 12×6 output weight matrix to produce one logit per vocabulary word. The target "sat" has logit -0.06 — next, softmax converts these raw scores into probabilities.

Step 4 of 6: Softmax Prediction

Turning scores into probabilities

Temperature1.00
softmax(logits / T) → P(word | context) — T = 1.00.10ran0.09on0.09dog0.08mat0.08the0.08a0.08big0.08sat0.08bed0.08in0.08down0.08catTarget: "sat" — P = 0.0806
TEMPERATURE
1.0
P(sat)
0.081
TOP-1
ran
ENTROPY
3.58

Temperature 1.0: moderate confidence. The target "sat" has probability 0.081. Softmax converts raw logits into a valid probability distribution that sums to 1.

Step 5 of 6: Training with Cross-Entropy

Learning from mistakes

Training step0
2D Embedding SpacetheacatdogsatranonindownmatbedbigLoss curveP("sat" | context)
STEP
0
LOSS
2.80
P(sat)
0.080

Before training: embeddings are random, prediction probability is ~8% (uniform over 12 words). The loss (cross-entropy) is high because the model can't predict the center word.

Step 6 of 6: CBOW vs. Skip-gram

Two sides of the same coin

CBOW: Many → OnethecatontheAveragehiddensatPredictComparison:Direction:Context → CenterCenter → ContextSpeed:Faster (1 update/window)Slower (2C updates/window)Rare words:Averaged outEach gets its own gradientFrequent words:Handles wellNeeds subsamplingBest for:Frequent words, syntaxRare words, semantics
MODEL
CBOW
TRAINING PAIRS
1/window
BEST FOR
Frequent

CBOW predicts the center word from context — it averages all context embeddings into one vector, producing one update per window. This makes it fast and good for frequent words, but rare words get averaged out since they rarely appear as the center target.