CBOW Model
How does a neural network learn a rich vector representation for a word by predicting it from its surrounding context?
The Continuous Bag-of-Words (CBOW) model provides an elegant solution by training a shallow neural network to predict a target word based on its surrounding context words.
The Intuition: Context is King
The words that appear around a target word often provide strong clues about its meaning and grammatical role. For example, if you see the words "hot" and "coffee" frequently together, you can infer a relationship. The CBOW model leverages this fundamental idea: a word's meaning can be inferred from its neighbors.
Instead of trying to predict context from a target word, CBOW takes the surrounding words as input and attempts to predict the central word. This approach helps the model learn dense, numerical representations for words, capturing their semantic and syntactic properties based on how they are used in language.
The CBOW Architecture
The CBOW model operates with a simple, shallow neural network. It begins by taking a window of context words around a target word. Each of these context words is first converted into a one-hot encoded vector, a sparse representation where a single element is 1 and all others are 0, corresponding to its position in the vocabulary.
These one-hot vectors are then multiplied by an input weight matrix, effectively looking up their corresponding word vectors. In the projection layer, these individual context word vectors are averaged together to form a single, dense context vector. This averaging step is crucial as it creates a unified representation of the surrounding words, which then serves as the input for the next layer.
The combined context vector from the projection layer is then fed into the output layer. This layer uses a softmax function to produce a probability distribution over all words in the vocabulary. The goal is for the model to assign the highest probability to the actual target word that was originally surrounded by the input context words.
During training, the model adjusts its internal weights (which are the word embeddings themselves) to maximize the probability of predicting the correct target word. This process allows the model to learn meaningful vector representations that encode the contextual relationships between words.
Learning Word Relationships
The CBOW model minimizes the negative log-likelihood of predicting the target word given its context words . This is typically achieved using the cross-entropy loss function:
Here, is the output vector of the target word, is the averaged context vector from the projection layer, and is the entire vocabulary. The term normalizes the probabilities across all words.
Visualizing Learned Embeddings
The heatmap above provides a glimpse into the structure of word embeddings. Each row is a word's embedding vector, and each column is a dimension. While individual dimensions might not have a human-interpretable meaning, the overall pattern of values within a vector is what matters.
Crucially, words with similar meanings or that appear in similar contexts tend to have similar embedding vectors. This means that in the high-dimensional space, "king" and "queen" would be close to each other, and "apple" and "banana" would also be close, but "king" and "apple" would be far apart. This proximity reflects the semantic relationships learned by the CBOW model.
CBOW vs. Skip-gram: A Quick Look
| Feature | CBOW (Continuous Bag-of-Words) | Skip-gram |
|---|---|---|
| Input/Output | Predicts target word from context words | Predicts context words from target word |
| Training Speed | Generally faster for large corpora | Generally slower, especially with large window sizes |
| Performance on Rare Words | Less effective at representing rare words | Better at capturing representations for rare words |
| Context Handling | Averages context word vectors | Treats each context word independently |
The CBOW model learns word embeddings by predicting a target word from its surrounding context words.
Its architecture involves an input layer for context words, a projection layer that averages context vectors, and an output layer that uses softmax to predict the target word.
Training involves minimizing the negative log-likelihood (cross-entropy loss) of the target word given the context.
The model iteratively updates word vectors (embeddings) through backpropagation, refining their ability to capture semantic relationships.
Learned word embeddings are dense numerical vectors where words with similar meanings or contexts have similar vector representations.
CBOW is generally faster to train than Skip-gram, making it suitable for large corpora, though it may be less effective with rare words.