Skip-gram Model › The Objective Function
Language AI HandbookAll chapters Skip-gram Model › The Objective FunctionOne section · Sections in this chapter5 / 14 ↓
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
Skip-gram maximizes the log-likelihood of observing the actual context words given each center word. For a training corpus of words, the objective is:
where:
- : total number of words in the corpus
- : the window size (context radius in each direction)
- : the center word at position
- : a context word at offset from the center
- : all model parameters (both embedding matrices and )
The outer sum iterates over every word position in the corpus. The inner sum iterates over every context offset within the window, generating up to training signals per center word. For window size , this means up to four context words per center word (offsets , where valid), giving up to four gradient updates per word occurrence.
The log transform converts products of probabilities into sums, which is both numerically more stable and mathematically cleaner. Maximizing this log-likelihood is equivalent to minimizing the negative log-likelihood, which is the loss function:
Substituting the softmax expression for :
where:
- : the dot-product score for the true context word
- : the log-partition function, which penalizes the model for spreading probability mass across many words rather than concentrating it on the true context word
The loss structure has a clean intuition. The first term increases when the true context word's score increases: the model is rewarded for making true pairs score high. The second term increases whenever any word scores high: the model is penalized for assigning high scores to words that are not the true context. Training pulls these in opposition, and the equilibrium is a model that assigns high scores to semantically related words.
Gradient Computation
To understand how Skip-gram learns, it helps to see the gradient of the loss with respect to the embeddings. For a single training pair , the gradient with respect to the context embedding of vocabulary word is:
where:
- : the predicted probability that word is the context word
- : an indicator that equals 1 if is the true context word and 0 otherwise
- : the center word embedding
This gradient has an elegant interpretation. If the model assigns high probability to the true context word (), the error signal is nearly zero and the embedding barely changes. If it assigns low probability (), the error is large and the context embedding is pushed toward the center embedding. For all other words , the gradient pushes their context embeddings away from the center embedding in proportion to how much probability they were incorrectly assigned.
The gradient with respect to the center word embedding accumulates contributions from every vocabulary word's context embedding, weighted by the prediction error:
This again requires iterating over all vocabulary words for each training pair, which is the source of the computational bottleneck. Negative Sampling replaces this full sum with a small sample, making the gradient computation tractable.