A word embedding is a dense, fixed-length, real-valued vector representation of a word. More formally, an embedding model is a parameterized function \(\mathcal{W} \rightarrow \mathbb{R}^n\) that maps every word \(w \in \mathcal{W}\) to a point in a high-dimensional vector space.

Word embeddings have become a core component of natural language processing (NLP) systems. Text is composed of units such as words and characters, but computers cannot interpret those symbols in a human sense. To process language with machine-learning models, we first need to turn it into numerical representations. Ideally, those representations should capture useful semantic and syntactic properties rather than merely assign an arbitrary number to every word.

The simplest approach is one-hot encoding. Given a vocabulary of \(N\) words, we assign an index \(i \in \{1, \ldots, N\}\) to each word. A word is then represented by an \(N\)-dimensional vector containing zeros everywhere except at its index, where the value is one.

One-hot vectors are easy to construct, but they have two major limitations. First, their dimensionality grows with the vocabulary: more dimensions mean more parameters, more training data, and more computation. Second, they encode no similarity between words. Any two distinct one-hot vectors are equally distant, whether they represent cat and dog or cat and database. A useful embedding space should instead place semantically related words near one another.

Comparison between one-hot vectors and semantically meaningful word embeddings
Fig. 1: Two different approaches for encoding words into vector spaces. One-hot encoding on the left-hand side; semantically-meaningful word embedding on the right-hand side.
(Image source: Antoine Louis, 2020)

This goal is closely related to the distributional hypothesis, introduced by Zellig S. Harris: words that occur in similar contexts tend to have similar meanings.1 John R. Firth later summarized the idea with the famous observation that a word is characterized by the company it keeps.2

Most embedding methods rely on this principle in one way or another. They can be organized into three broad families:

  • Count-based models derive representations from corpus-wide co-occurrence statistics.
  • Prediction-based models learn representations by predicting a word from its context, or the context from a word.
  • Deep contextual models generate a different representation for a token depending on the sentence in which it appears.

1. Count-based models

Count-based models start with a co-occurrence matrix. This matrix is built by scanning a large collection of documents and recording how frequently words occur together.

Two common variants are:

  • A word-document matrix \(\mathbf{X} \in \mathbb{N}^{N \times D}\), where \(N\) is the vocabulary size, \(D\) is the number of documents, and \(\mathbf{X}_{ij}\) records how often word \(i\) appears in document \(j\).
  • A window-based matrix \(\mathbf{X}' \in \mathbb{N}^{N \times N}\), where \(\mathbf{X}'_{ij}\) records how often word \(i\) occurs within a fixed-size context window around word \(j\).
Window-based word co-occurrence matrix built from three short sentences
Fig. 2: Example of a window-based co-occurrence matrix computed with a window size of 3 (one word on either side of the input word, in addition to the input word itself), over the following corpus: “I enjoy flying.”, “I like NLP.” and “I like deep learning.”.
(Image source: Antoine Louis, 2020)

Because these matrices are large and sparse, their dimensionality is usually reduced. One classical approach is Singular Value Decomposition (SVD), which factorizes the matrix as

\[\mathbf{X} = \mathbf{U}\mathbf{S}\mathbf{V}^{\top}.\]

The rows of a truncated \(\mathbf{U}\) matrix can then serve as compact word embeddings. Well-known count-based approaches include Latent Semantic Analysis (LSA),3 Hyperspace Analogue to Language (HAL),4 Correlated Occurrence Analogue to Lexical Semantics (COALS),5 and Hellinger PCA.6

These models use global corpus statistics effectively and often capture word similarity well. However, they generally perform less well on relationships such as analogies, suggesting that their vector spaces do not encode every kind of linguistic structure equally well.

2. Prediction-based models

Prediction-based approaches do not construct embeddings directly from a count matrix. Instead, they learn representations as parameters of a model trained to predict words from their context.

2.1. Neural network language model

The neural network language model proposed by Bengio and colleagues jointly learns word vectors and a statistical language model.7 It uses a feed-forward neural network with a linear projection layer and a non-linear hidden layer.

Given a sequence of words \(w_1, \ldots, w_n\) from a vocabulary \(V\), the objective is to estimate the probability of the next word from the preceding words:

\[f\left(w_t, \ldots, w_{t-n+1}\right) = p\left(w_t \mid w_{t-1}, \ldots, w_{t-n+1}\right).\]

The model can be understood as the composition of two mappings, \(C\) and \(g\):

\[f\left(i, w_{t-1}, \ldots, w_{t-n+1}\right) = g\left(i, C(w_{t-1}), \ldots, C(w_{t-n+1})\right).\]
  • \(C\) is the embedding lookup table. In practice, it is a \(\lvert V\rvert \times m\) matrix of learned parameters mapping each word index to an \(m\)-dimensional vector.
  • \(g\) receives the context vectors and returns a probability distribution over the vocabulary for the next word, typically through a softmax layer.

Training maximizes the average log-likelihood of the corpus:

\[\begin{aligned} \mathcal{L} &= \frac{1}{T}\sum_t \log f\left(w_t, w_{t-1}, \ldots, w_{t-n+1}; \boldsymbol{\theta}\right) \\ &= \frac{1}{T}\sum_t \log p\left(w_t \mid w_{t-1}, \ldots, w_{t-n+1}\right). \end{aligned}\]
Architecture of a feed-forward neural network language model
Fig. 3: Neural network language model (NNLM) architecture. \(f\left(i, w_{t-1}, \ldots, w_{t-n+1}\right)=g\left(i, C\left(w_{t-1}\right), \ldots, C\left(w_{t-n+1}\right)\right)\), where \(g\) is the neural network and \(C(i)\) is the \(i\)-th word feature vector.
(Image source: Bengio et al., 2003)

The principal drawback is the final softmax layer. Computing a probability for every possible output word has a cost proportional to \(\lvert V\rvert\), which may contain hundreds of thousands or even millions of entries. Training such a model can therefore be computationally expensive.

2.2. Continuous bag-of-words

The continuous bag-of-words model (CBOW), one of the two original Word2Vec architectures, predicts a target word from the words surrounding it.8 Unlike a standard language model, it can use context on both sides of the target because its purpose is to learn useful word representations rather than generate text from left to right.

For a context window of size \(n\), its objective can be written as

\[\mathcal{L} = \frac{1}{T}\sum_t \log p\left( w_t \mid w_{t-n}, \ldots, w_{t-1}, w_{t+1}, \ldots, w_{t+n} \right).\]

CBOW removes the non-linear hidden layer of the earlier feed-forward language model, reducing its computational cost. Let \(\mathbf{W} \in \mathbb{R}^{\lvert V\rvert \times d}\) denote the weights between the input and projection layers. Each row of \(\mathbf{W}\) is the \(d\)-dimensional representation of one vocabulary word.

For a one-hot input vector \(\mathbf{x}\) whose non-zero entry is at position \(k\),

\[\mathbf{h} = \mathbf{W}^{\top}\mathbf{x} = \mathbf{W}_{(k, \cdot)}^{\top} = \mathbf{v}_{w_I}^{\top}.\]

The multiplication simply selects the corresponding row of the embedding matrix. With multiple context words, CBOW averages their representations:

\[\mathbf{h} = \frac{1}{n}\mathbf{W}^{\top} \left(\mathbf{x}_1 + \mathbf{x}_2 + \cdots + \mathbf{x}_n\right) = \frac{1}{n} \left(\mathbf{v}_{w_1} + \mathbf{v}_{w_2} + \cdots + \mathbf{v}_{w_n}\right)^{\top}.\]

A second matrix \(\mathbf{W}' \in \mathbb{R}^{d \times \lvert V\rvert}\) maps the hidden representation to an output score for every vocabulary word:

\[s_j = {\mathbf{v}'_{w_j}}^{\top}\mathbf{h}.\]

The softmax operation turns these scores into a probability distribution:

\[p(w_j \mid w_I) = y_j = \frac{\exp(s_j)}{\sum_{j'=1}^{\lvert V\rvert}\exp(s_{j'})}.\]

Once training is complete, \(\mathbf{W}\), \(\mathbf{W}'\), or an average of the two can be used as the final word embedding matrix.

2.3. Skip-gram

The skip-gram model reverses the CBOW prediction task: instead of predicting the center word from its context, it predicts the surrounding words from the center word.8

Its objective sums the log probabilities of the words within a context window of radius \(c\):

\[\mathcal{L} = \frac{1}{T}\sum_{t=1}^{T} \sum_{-c \leq j \leq c,\,j \neq 0} \log p\left(w_{t+j} \mid w_t\right).\]

The transition from the input to the projection layer works as in the single-word CBOW case: the one-hot input selects one row of \(\mathbf{W}\). The difference lies in the output, where the model predicts multiple context-word distributions rather than a single center word.

Continuous bag-of-words architecture predicting a target word from surrounding context words
(a) Continuous bag-of-words model.
Skip-gram architecture predicting surrounding context words from a center word
(b) Skip-gram model.
Fig. 4: Continuous bag-of-words (CBOW) and skip-gram (SG) architectures. The CBOW architecture predicts the current word based on the context, and SG predicts surrounding words given the current word.
(Image source: Rong, 2014)

CBOW and skip-gram form Word2Vec.9 Their success came not only from these simple architectures but also from efficient approximations to the full softmax, most notably negative sampling and hierarchical softmax.8

Negative sampling updates the model using a small set of sampled words rather than summing over the entire vocabulary. Hierarchical softmax organizes the output vocabulary as the leaves of a binary tree, reducing probability computation to a path through that tree. Both techniques make large-scale embedding training considerably faster.10

Skip-gram also forms the basis of FastText.11 Instead of learning only one vector per word, FastText represents a word through character n-grams and combines their representations. For example, with \(n=3\), artificial contains sequences such as art, rti, and tif. This subword structure enables the model to construct representations for rare or previously unseen words, something standard Word2Vec cannot do directly.

3. Deep contextual models

Count-based models and Word2Vec assign one fixed vector to each word. That representation captures the word’s most common usages across the training corpus, but it cannot resolve polysemy: the same spelling may have different meanings in different contexts. The word bank, for example, should not have exactly the same representation in river bank and investment bank.

Deep contextual models address this limitation by assigning each token a representation that depends on the entire input sentence. Early contextual approaches included Context2Vec,12 CoVe,13 ELMo,14 and ULMFiT,15 many of which used bidirectional recurrent networks to encode context.

The Transformer architecture16 subsequently enabled a new generation of pretrained contextual language models, including BERT,17 XLNet,18 and ERNIE.19 Unlike static embedding methods, these models can produce different vectors for the same word according to its surrounding words. That ability to represent meaning in context is one of the reasons pretrained language models have proved effective across such a wide range of NLP tasks.


References

  1. Harris. Distributional structure. Word, 10(2–3):146–162, 1954. 

  2. Firth. A synopsis of linguistic theory, 1930–1955. Studies in Linguistic Analysis, 1957. 

  3. Landauer and Dumais. A solution to Plato’s problem: The latent semantic analysis theory of acquisition, induction, and representation of knowledge. Psychological Review, 104(2):211, 1997. 

  4. Lund and Burgess. Producing high-dimensional semantic spaces from lexical co-occurrence. Behavior Research Methods, Instruments, & Computers, 28(2):203–208, 1996. 

  5. Rohde et al. An improved method for deriving word meaning from lexical co-occurrence. Cognitive Psychology, 7:573–605, 2004. 

  6. Lebret and Collobert. Word embeddings through Hellinger PCA. arXiv preprint arXiv:1312.5542, 2013. 

  7. Bengio et al. A neural probabilistic language model. Journal of Machine Learning Research, 3:1137–1155, 2003. 

  8. Mikolov et al. Distributed representations of words and phrases and their compositionality. Advances in Neural Information Processing Systems, pages 3111–3119, 2013.  2 3

  9. Mikolov et al. Efficient estimation of word representations in vector space. arXiv preprint arXiv:1301.3781, 2013. 

  10. Rong. word2vec parameter learning explained. arXiv preprint arXiv:1411.2738, 2014. 

  11. Bojanowski et al. Enriching word vectors with subword information. Transactions of the Association for Computational Linguistics, 5:135–146, 2017. 

  12. Melamud et al. context2vec: Learning generic context embedding with bidirectional LSTM. Proceedings of CoNLL, pages 51–61, 2016. 

  13. McCann et al. Learned in translation: Contextualized word vectors. Advances in Neural Information Processing Systems, pages 6294–6305, 2017. 

  14. Peters et al. Deep contextualized word representations. arXiv preprint arXiv:1802.05365, 2018. 

  15. Howard and Ruder. Universal language model fine-tuning for text classification. arXiv preprint arXiv:1801.06146, 2018. 

  16. Vaswani et al. Attention is all you need. Advances in Neural Information Processing Systems, pages 5998–6008, 2017. 

  17. Devlin et al. BERT: Pre-training of deep bidirectional transformers for language understanding. arXiv preprint arXiv:1810.04805, 2018. 

  18. Yang et al. XLNet: Generalized autoregressive pretraining for language understanding. Advances in Neural Information Processing Systems, pages 5754–5764, 2019. 

  19. Zhang et al. ERNIE: Enhanced language representation with informative entities. arXiv preprint arXiv:1905.07129, 2019.