Search

Search papers and lessons

Enriching Word Vectors with Subword Information

Piotr Bojanowski, Edouard Grave, Armand Joulin et al. · 2016

fastText represents each word as the sum of vectors for its character n-grams, so rare and unseen words still get useful vectors, especially in languages with rich word endings.

On this page

  • Colour = topic mix
  • Representation learning
  • Discrete math
  • Calculus
  • Linear algebra
  • Probability
  • Neural network basics
  • ReadReady to readNot yet

TL;DR

Standard word vector models give each word its own vector, so a rare word seen only a few times gets a poor vector and a word never seen gets none. This paper changes the skip-gram model (Skip-Gram Model) so that a word's vector is the sum of vectors for its character n-grams, short letter sequences of length 3 to 6. The authors report that training is about 1.5 times slower than the baseline, that the model gives vectors to unseen words, and that it beats the baselines on most word similarity datasets in nine languages and on syntactic analogy tests in four.

Builds on: Skip-Gram Model, Negative Sampling, Stochastic Gradient Descent, Word Embedding · Leads to: Subword Tokenization, word vector methods for morphologically rich languages

Why it mattered

Word vectors became a standard input for language systems, and this paper showed a cheap way to fix their biggest gap, rare words. The method is simple enough to train on a laptop-scale corpus, and the authors released a C++ implementation publicly, as the paper states, which made it easy for others to compare against. The idea that a word can be built from its pieces also fits the subword units that later language models use. The paper's related-work section already cites machine translation work on subword units.

The problem

A word vector model learns one vector per vocabulary word. The paper's introduction names the weakness: most methods "ignore the internal structure of words", which is a serious limitation for "morphologically rich languages, such as Turkish or Finnish". The paper gives a concrete picture: in French or Spanish most verbs have more than forty inflected forms, and Finnish has fifteen cases for nouns. Many of these forms are rare or absent from a training corpus, so each gets too little data to learn a good vector. Yet the forms share pieces, such as a verb stem and an ending, and the pieces follow rules.

The goal is therefore to share statistical strength across words through their shared pieces, and to give a vector to any word, including one never seen in training.

How it works

The paper keeps the skip-gram training procedure and changes one thing: how a word's vector is computed. Here is one word traced from start to finish.

  1. Start with a word. Take the word where and use character n-grams of length 3. (The paper uses all lengths from 3 to 6; length 3 keeps this example short.)
  2. Add boundary markers. Pad the word with a start marker and an end marker, written as the angle bracket characters, so the padded form is the word with a bracket in front and a bracket after. The markers tell a prefix apart from a letter sequence in the middle of a word.
  3. List the n-grams. Slide a window of length 3 across the padded word. You get the pieces that start with the start marker, the pieces in the middle, and the piece that ends with the end marker. The paper's example writes the five pieces as <wh, whe, her, ere, re>.
  4. Add the whole word. The paper also keeps the word itself as a special piece, so each word has its own vector too. This is one extra item on top of the five n-grams.
  5. Look up one vector per piece. Each n-gram g has a vector z_g. The word's representation is the sum of its pieces' vectors. Suppose the first piece has vector (1, 0), the second has (0, 1), and the other pieces are zero for this check. The sum is (1, 1).
  6. Score a context word. Skip-gram asks whether a context word c tends to appear near the word. The score is the dot product of the summed word vector with the context vector v_c. With v_c = (2, 3), the score from the two pieces above is 1×2 + 1×3 = 5.
  7. Train as usual. The score feeds the same logistic loss and negative sampling that skip-gram uses. The gradient flows into each n-gram vector, so a piece shared by many words gets many updates.

Two things follow from step 5. First, the piece for the short word her and the piece her inside where are different, because the boundary markers differ. Second, a word that never appeared in training still has pieces, and those pieces were learned from other words. So its vector is the sum of familiar pieces.

flowchart LR
    W["word: where"] --> B["add boundary markers"]
    B --> N["character n-grams 3 to 6, plus the whole word"]
    N --> S["sum the n-gram vectors"]
    S --> D["dot product with context vector"]
    D --> L["logistic loss with negative samples"]
    L --> U["update every n-gram vector"]

Worked examples

Example
1. Counting the pieces of one word

Take the word where with n-gram lengths 3 to 6.

  1. Write the padded word, which has 7 characters once the two markers are added.
  2. Length 3: start positions 0 to 4 give five pieces: <wh, whe, her, ere and re>.
  3. Length 4: start positions 0 to 3 give four pieces: <whe, wher, here and ere>.
  4. Length 5: start positions 0 to 2 give three pieces: <wher, where and here>.
  5. Length 6: start positions 0 and 1 give two pieces: <where and where>.
  6. Add the whole word as its own piece. Total: 5 + 4 + 3 + 2 + 1 = 15 pieces.

Interpretation: a short word yields many overlapping pieces, and each piece is a shared unit that other words can also use.

Example
2. A score by hand

Let the dimension be 2. Suppose a word has two n-grams, with vectors (1, 0) and (0, 1), and the context vector is (2, 3).

  1. Sum the n-gram vectors: (1, 0) + (0, 1) = (1, 1). This is the word's vector.
  2. Take the dot product with the context vector: (1, 1) · (2, 3) = 1 × 2 + 1 × 3 = 5.
  3. The score is 5. The logistic loss for a true pair is log(1 + e^(−5)), which is about 0.007, so this pair is already well predicted.
  4. Change the context vector to (−1, 0). The score becomes 1 × (−1) + 1 × 0 = −1. The loss for a true pair rises to log(1 + e) ≈ 1.31, so training would push the vectors apart.

Interpretation: the score is a sum over pieces, so a change to one piece changes every word that contains it.

Example
3. A realistic check from the paper's language model results

The paper uses Czech and Russian as its morphologically rich examples. Table 5 gives test perplexity (lower is better) for a language model that starts from pre-trained vectors. For Czech, the skip-gram vectors give 339 and the subword vectors give 312. For Russian, skip-gram gives 237 and subword gives 206.

  1. Czech reduction: 339 − 312 = 27.
  2. Relative Czech reduction: 27 / 339 ≈ 0.080, which is about 8%. The paper reports 8%.
  3. Russian reduction: 237 − 206 = 31.
  4. Relative Russian reduction: 31 / 237 ≈ 0.131, which is about 13%. The paper reports 13%.
  5. The paper says the gain is smaller for Spanish (3%) and French (2%), which fits its claim that the gain is largest for rich morphology.

Try it: for Spanish, skip-gram gives 150 and subword gives 145. What is the relative reduction, and does it match the paper's 3%? Answer: 5 / 150 ≈ 0.033, which is about 3%, so it matches.

The math

The paper first states the baseline skip-gram objective, then changes only the scoring function.

Step 1 in symbols: the skip-gram objective

Given a corpus of words w1,…,wTw_{1}, \dots, w_{T}, skip-gram maximises the log-likelihood ∑t=1T∑c∈Ctlog⁡p(wc∣wt)\sum_{t=1}^{T} \sum_{c \in \mathcal{C}_{t}} \log p(w_{c} \mid w_{t}). Here Ct\mathcal{C}_{t} is the set of positions near position tt, and wcw_{c} is the word at a context position cc.

A full softmax over the vocabulary of size WW would normalise over every word. The paper instead treats each context word as an independent yes-or-no question. For a context word wcw_{c} it uses the logistic loss ℓ(x)=log⁡(1+e−x)\ell(x) = \log(1 + e^{-x}) on the score of the true pair, and the same loss on the negative sampled words nn with flipped sign:

∑t=1T[∑c∈Ctℓ(s(wt,wc))+∑n∈Nt,cℓ(−s(wt,n))]\sum_{t=1}^{T}\left[\sum_{c \in \mathcal{C}_{t}} \ell(s(w_{t}, w_{c})) + \sum_{n \in \mathcal{N}_{t,c}} \ell(-s(w_{t}, n))\right]

Here Nt,c\mathcal{N}_{t,c} is a set of negative samples drawn at random from the dictionary.

Step 5 in symbols: the subword score

The new scoring function is

s(w,c)=∑g∈Gwzg⊤vcs(w, c) = \sum_{g \in \mathcal{G}_{w}} \mathbf{z}_{g}^{\top} \mathbf{v}_{c}

where Gw\mathcal{G}_{w} is the set of n-grams of word ww (including the whole word), zg\mathbf{z}_{g} is the vector of n-gram gg, and vc\mathbf{v}_{c} is the output vector of the context word. The scalar zg⊤vc\mathbf{z}_{g}^{\top}\mathbf{v}_{c} is the dot product. The sum over Gw\mathcal{G}_{w} is step 5 of the walkthrough, and the dot product is step 6.

The paper hashes n-grams into a fixed number of buckets, K=2×106K = 2 \times 10^{6}, to bound memory. Several n-grams can then share a bucket, which is a trade-off the paper accepts for a fixed memory size.

Step 7 in symbols: the step size

The paper uses plain stochastic gradient descent (Stochastic Gradient Descent) with a step size that falls linearly over training. With TT words per pass, PP passes and a starting step γ0\gamma_{0}, the step at time tt is

γt=γ0(1−tTP)\gamma_{t} = \gamma_{0}\left(1 - \frac{t}{TP}\right)

so the step is γ0\gamma_{0} at the start and falls to zero at the end. The paper sets γ0=0.025\gamma_{0} = 0.025 for the skip-gram baseline and γ0=0.05\gamma_{0} = 0.05 for its model and the CBOW baseline.

Results

Table 1 reports the correlation between model similarity scores and human judgements, using Spearman's coefficient as the paper does. On German Gur350 the skip-gram baseline gets 61 and the subword model gets 70. On Russian HJ the scores are 59 and 66. Table 2 reports analogy accuracy: on Czech syntactic analogies the skip-gram baseline gets 52.8 and the subword model gets 77.8. The gains are not uniform. On German semantic analogies the subword model is lower, 62.3 against 66.5 for skip-gram. The paper ties this to the n-gram length it chose, and Table 4 shows that the best range depends on the task.

With little data the model still holds up. Trained on 5% of Wikipedia, the subword model reaches 66 on German Gur350, above the CBOW baseline trained on the full corpus, which gets 62. On English RW, 1% of the corpus gives 45 against 43 for CBOW on the full corpus. The paper calls this a practical advantage for small or specialised datasets.

The limits matter as much as the wins. It is tempting to think that the subword model replaces the whole word with its pieces. In fact the paper keeps the whole word as one extra piece, so that each word still has a representation of its own in addition to its n-grams. The pieces are added to the word, not used instead of it.

It is also tempting to think that more n-grams always help. The paper's experiment on n-gram length (Table 4) shows that the best range depends on language and task. The authors chose 3 to 6 arbitrarily, and they say a proper validation procedure was not used because test data was scarce. Longer n-grams help German compounds, while very short ones (length 2) are not informative for analogy tasks.

The model also does not help everywhere. On the English WS353 similarity set, the paper's model does not beat the baseline, because those words are common enough that good vectors come without subwords. The gain is largest for rare words and for languages with many forms.

Reading guide

The paper is short, so read it with its prerequisites at hand.

Check yourself

QuestionHow many character n-grams of length 3 does the word where have under the paper's rule, once boundary markers are added, and what are they?Show answer

Five: the padded word has 7 characters, so a window of length 3 has 7 − 3 + 1 = 5 start positions. The pieces are the start-marked piece, three middle pieces, and the end-marked piece.

QuestionA word has n-gram vectors (2, 0), (0, 2) and (1, 1). The context vector is (1, 4). What is the score under the paper's model?Show answer

The summed word vector is (2 + 0 + 1, 0 + 2 + 1) = (3, 3). The dot product with (1, 4) is 3 × 1 + 3 × 4 = 15.

QuestionWhy can the subword model give a vector to a word that never appeared in training?Show answer

The word is built from its n-grams, and those n-grams were learned from other words. The new word's vector is the sum of familiar pieces, so it is not empty.

QuestionTable 5 gives Czech perplexity of 339 for skip-gram and 312 for subword vectors. What is the relative reduction, to the nearest whole percent?Show answer

The reduction is 27. Then 27 / 339 ≈ 0.0796, which rounds to 8%.

QuestionScenario: a team builds word vectors for a small corpus of product reviews in English, where most words are common. Would the subword model be the best choice, and why?Show answer

Probably not as the main gain. The paper shows that on common English words the subword model does not beat the baseline, because good vectors come without subwords. The subword model helps most for rare words and rich morphology, so the team would see little benefit on common English vocabulary.

Sources

  • Bojanowski, P., Grave, E., Joulin, A., and Mikolov, T. Enriching Word Vectors with Subword Information. arXiv:1607.04606. https://arxiv.org/abs/1607.04606
  • Bojanowski, P., Grave, E., Joulin, A., and Mikolov, T. Enriching Word Vectors with Subword Information. Transactions of the Association for Computational Linguistics, doi:10.1162/tacl_a_00051. https://doi.org/10.1162/tacl_a_00051

Read the full paper

Piotr Bojanowski, Edouard Grave, Armand Joulin et al., 2016. arXiv:1607.04606