FastText › The FastText Training Objective
Language AI HandbookAll chapters FastText › The FastText Training ObjectiveOne section · Sections in this chapter5 / 15 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
FastText adopts Word2Vec's skip-gram framework: predict context words from center words. The modification is entirely in how the center word is represented. Instead of a single embedding lookup, the center word's representation is computed as a sum over its n-gram embeddings. This substitution is the only change to the original skip-gram architecture.
The compatibility score between center word and context word is:
where:
- : the real-valued compatibility score (higher values encourage co-occurrence)
- : the set of n-grams for the center word
- : the n-gram embedding for subword
- : the context embedding for the context word (a separate embedding table from the n-gram table)
This formula is equivalent to first summing all n-gram embeddings of and then computing a single dot product with . Writing it as a sum of dot products makes the gradient computation explicit: every n-gram in receives a gradient proportional to how well the n-gram aligns with the context embedding. The gradient for is the same regardless of which specific word caused the update, which is what lets knowledge flow across all words sharing that n-gram.
Negative Sampling Loss
Computing the full softmax over all vocabulary words is prohibitively slow when vocabularies contain hundreds of thousands of words. FastText uses negative sampling, the same approximation as Word2Vec. For each positive (center, context) pair, the model samples random negative words from the vocabulary and trains a binary classifier to distinguish the true pair from the fake ones.
The core idea behind negative sampling is this: instead of asking "what is the probability of this context word given the center word, compared to all other words?", we ask "can the model tell the difference between an observed co-occurrence pair and a randomly drawn pair?" This binary classification task is much cheaper and, empirically, produces embeddings of comparable quality.
The loss for a single training example is:
where:
- : the per-example objective, which we maximize (equivalently, we minimize )
- : the sigmoid function mapping scores to probabilities in
- : the positive term, rewarding high scores for observed pairs
- : the number of negative samples per positive example (typically 5 to 10)
- : the -th randomly sampled negative context word
- : the negative term, penalizing high scores for random pairs
Maximizing pushes the model to assign high scores to real co-occurrence pairs and low scores to random ones. Because the center word representation is a sum over n-gram embeddings, each gradient update trains one word embedding and all n-gram embeddings that appear in the center word simultaneously. A single training step on "running" updates the embeddings for "<ru," "run," "unn," "nni," "nin," "ing," "ng>," and every other n-gram in the word. This parameter sharing is the mechanism that makes FastText sample-efficient: every observation of any word containing a morpheme contributes to refining the morpheme's embedding.
The negative sampling distribution matters. FastText follows Word2Vec in using a smoothed unigram distribution raised to the power :
where is the word's frequency in the training corpus. Raising to raises the probability of sampling rare words relative to what a pure frequency-proportional distribution would give. This prevents the most common words from dominating the negative samples, which would make training ignore rare words entirely.