What is CBOW (continuous bag of words)?
CBOW, or continuous bag of words, is the word2vec architecture that predicts a word from the words around it. It looks up the embedding of each context word, averages them into one vector, and uses that vector to score every vocabulary word as the missing center word. Training adjusts the embeddings until the right word gets a high score.
CBOW is the faster of the two word2vec architectures, which made it a common choice for training word vectors on very large corpora. It is the default mode in tools such as gensim's Word2Vec, and its averaging idea reappears in fastText's text classifier, which averages word and n-gram vectors to label documents. The fill-in-the-middle task it uses is also an early form of the masked word prediction that BERT later scaled up.
Key ideas
- CBOW predicts the center word from the average of its context word embeddings.
- Averaging makes the context a bag: word order inside the window is ignored.
- Scores for every vocabulary word come from one matrix product with the averaged vector.
- Training minimizes cross-entropy on the true center word.
- Hierarchical softmax or negative sampling avoids normalizing over the full vocabulary.
- CBOW trains faster than skip-gram but learns weaker vectors for rare words.
Common questions
Is CBOW supervised or unsupervised?
It is self-supervised. The labels come from the text itself: every position in the corpus provides a context and the word that belongs in the middle, so no human annotation is needed. The prediction task is only a means to an end, since the useful output is the embedding matrix.
How do you train a CBOW model in Python?
In gensim, Word2Vec(sentences, sg=0) trains CBOW, and sg=0 is the default. The main settings are vector_size (dimensions), window (context size on each side), min_count (drop rare words), and negative (number of negative samples). The cbow_mean option chooses between averaging and summing the context vectors.
Can CBOW be used as a language model?
Not directly. It predicts a missing word from words on both sides, so it cannot generate text left to right, and it ignores order inside the window. It is a training trick for embeddings rather than a model of sentence probability.
What is the difference between CBOW and bag-of-words?
Bag-of-words represents a whole document as sparse word counts and involves no learning. CBOW learns a dense vector for each word by predicting it from a small window of neighbors. Both ignore order, but only CBOW places related words near each other.
Related explainers
- Word2Vec & Word Embeddings — Learn dense vector representations where similar words live near each other in space
- Skip-gram — Predict context words from a center word — the architecture that powers Word2Vec with negative sampling
- Softmax, Logits & Temperature — Turning raw scores into probabilities
- Embeddings — Turning words and categories into vectors
- Bag of Words — Turn text into numbers — count word occurrences to create the simplest document representation in NLP
- Loss Functions — How networks measure their mistakes
References
- Mikolov et al. (2013), "Efficient Estimation of Word Representations in Vector Space"
- Mikolov et al. (2013), "Distributed Representations of Words and Phrases and their Compositionality", NeurIPS
- Rong (2014), "word2vec Parameter Learning Explained"
- Joulin et al. (2016), "Bag of Tricks for Efficient Text Classification"
Last updated