Skip-gram Model
When a computer processes text, how does it discern that 'king' relates to 'queen' in the same way 'man' relates to 'woman'? The Skip-gram model addresses this by learning word representations where words with similar contexts are positioned closely in a vector space.
Defining 'Context': The Sliding Window
To understand how words relate, the Skip-gram model first needs to define what constitutes a word's context. It achieves this using a sliding window mechanism. This window moves across a sentence, identifying a central target word and all the words immediately surrounding it within a predefined range.
Predicting Neighbors: The Skip-gram Idea
At its core, the Skip-gram model operates on a simple yet powerful idea: given a specific word, it tries to predict the words that are likely to appear in its surrounding context. By repeatedly performing this prediction task across a large corpus of text, the model implicitly learns meaningful word embeddings. These embeddings are dense vector representations where words with similar meanings or contexts are mapped to nearby points in a high-dimensional space.
Maximizing Likelihood: The Training Goal
During training, the Skip-gram model's objective is to maximize the probability of observing the actual context words given a target word. This means the model adjusts its internal weights, which form the word embeddings, so that words frequently appearing together in the training data have higher predicted probabilities. The model essentially learns by making its predictions align as closely as possible with the real-world co-occurrence patterns of words.
The Skip-gram model aims to maximize the log-likelihood of observing context words given the target word over the entire training corpus :
Here, is the size of the context window, and is typically computed using a softmax function over the vocabulary.
Scaling Up: The Role of Negative Sampling
A significant computational challenge in training Skip-gram arises from the softmax function in the output layer. Calculating the probability for every word in a large vocabulary for each training step is extremely inefficient. Negative sampling provides an elegant solution by transforming the multi-class classification problem into a set of binary classification tasks. Instead of predicting all context words, it trains the model to distinguish between a few actual context words (positive samples) and a small number of randomly chosen non-context words (negative samples).
Preparing Training Data for Skip-gram
generate_skipgram_pairs function to use a window_size of 1 instead of 2. Observe how the number and specific pairs change.The Skip-gram model learns word embeddings by training a neural network to predict surrounding context words given a target word.
A sliding window mechanism is used to generate (target, context) pairs from raw text, forming the training data.
The model's architecture is a simple neural network with an input layer (one-hot target), a hidden layer (the embedding), and an output layer (context word probabilities).
Training involves maximizing the log-likelihood of observing actual context words, effectively adjusting embeddings to reflect co-occurrence patterns.
Negative sampling addresses the computational cost of softmax over large vocabularies by converting the task into binary classification.
By capturing these predictive relationships, Skip-gram enables computers to understand and represent complex semantic relationships between words.