Word2Vec Skip-gram
Implement skip-gram training pair generation for Word2Vec.
In the skip-gram model, given a center word, we predict the context words within a window. Generate all (center_word, context_word) training pairs.
Input format:
- Line 1: The text (space-separated words)
- Line 2: Window size W (integer)
For each word at position i, context words are at positions max(0, i-W) to min(n-1, i+W), excluding position i itself.
Output: A list of tuples (center_word, context_word) in order of center word position, and for each center word, context words from left to right.
Example:
I love NLP deeply 2
[('I', 'love'), ('I', 'NLP'), ('love', 'I'), ('love', 'NLP'), ('love', 'deeply'), ('NLP', 'I'), ('NLP', 'love'), ('NLP', 'deeply'), ('deeply', 'love'), ('deeply', 'NLP')]Words: ["I", "love", "NLP", "deeply"], window=2
Center "I" (pos 0): context positions max(0,0-2)=0 to min(3,0+2)=2, excluding 0 => 1,2 Pairs: (I, love), (I, NLP)
Center "love" (pos 1): context 0 to 3, excluding 1 => 0,2,3 Pairs: (love, I), (love, NLP), (love, deeply)
Center "NLP" (pos 2): context 0 to 3, excluding 2 => 0,1,3 Pairs: (NLP, I), (NLP, love), (NLP, deeply)
Center "deeply" (pos 3): context 1 to 3, excluding 3 => 1,2 Pairs: (deeply, love), (deeply, NLP)
Constraints:
- Window size W means W words to the left and W words to the right
- Handle boundary positions (beginning/end of text)
- Preserve original word casing
- Output pairs in positional order
Background Knowledge
The Word2Vec model is a type of word embedding technique used in Natural Language Processing (NLP). It represents words as vectors in a high-dimensional space, where semantically similar words are closer together. The skip-gram model is a variant of Word2Vec that predicts the context words given a center word. This is in contrast to the Continuous Bag of Words (CBOW) model, which predicts the center word given the context words.
In the skip-gram model, a window size W is used to define the context words. For a given center word at position i, the context words are those within the window of size W to the left and right of i, excluding i itself. The goal is to generate all possible (center word,context word) pairs for a given text. This is a crucial step in training the Word2Vec model, as it allows us to learn the vector representations of words based on their co-occurrence patterns.
The skip-gram model is based on the idea that words that appear in similar contexts are likely to have similar meanings. By predicting the context words given a center word, we can learn to represent words as vectors that capture their semantic relationships. This has numerous applications in NLP, including text classification, sentiment analysis, and language modeling.
Algorithm/Approach
The general approach to solving this problem involves iterating over each word in the input text and generating the context words within the specified window size. This can be achieved using a simple iterative algorithm that keeps track of the current word position and the corresponding context words.
Step-by-Step Strategy
To implement the solution, follow these steps:
- Read the input text and window size W.
- Split the input text into a list of words.
- Iterate over each word in the list, keeping track of its position i.
- For each word at position i, generate the context words by iterating over the positions from max(0,i−W) to min(n−1,i+W), excluding i itself.
- Create a tuple (center word,context word) for each context word and add it to the result list.
- Return the list of tuples.
Common Pitfalls
When implementing the solution, watch out for the following:
- Make sure to exclude the center word itself when generating the context words.
- Handle edge cases where the window size W is larger than the distance to the beginning or end of the text.
- Use efficient data structures to store the input text and result list.
Time & Space Complexity
The expected time complexity is O(n⋅W), where n is the length of the input text and W is the window size. This is because we iterate over each word in the text and generate the context words within the window size. The space complexity is O(n⋅W) as well, as we need to store the result list of tuples.