Generate deterministic Skip-gram training pairs from a token sequence. For each center position, pair its token with every token inside a fixed context window, excluding the center itself and positions outside the sequence.
{(wi,wj):max(0,i−c)≤j≤min(n−1,i+c), j=i}Here, wi is the token at center position i, wj is a context token, c is window, and n is the sequence length. Emit pairs by increasing center position and then increasing context position. Return an int64 PyTorch tensor with shape (P,2), including shape (0,2) when no pair exists.
Examples
Input: token_ids = [0,1,2,3], window = 1
Output: [[0,1],[1,0],[1,2],[2,1],[2,3],[3,2]]
Explanation: Each token is paired with its immediate neighbors while the sequence boundaries are respected.
Input: token_ids = [5,6,7], window = 2
Output: [[5,6],[5,7],[6,5],[6,7],[7,5],[7,6]]
Input: token_ids = [9], window = 1
Output: []
Hint 1
Clamp the context loop to the sequence boundaries for each center index.
Hint 2
Reshape an empty result to (0, 2) before returning it.
Requirements
- Use PyTorch.
- Generate every valid center-context pair inside the fixed window.
- Exclude self-pairs and do not wrap around sequence boundaries.
- Preserve the documented deterministic order.
- Return the documented int64 tensor.
Constraints
- token_ids is a one-dimensional int64 tensor.
- token_ids contains at least one token.
- window is a positive integer no greater than six.
- P may be zero.
All steps in Word2Vec
- Subsampling
- Skip-gram Pairs
- Negative Sampling
- SGNS Loss
- CBOW Forward
- SGNS Step
Accepts: array
Accepts: number
Skip-gram Pairs
Generate deterministic Skip-gram training pairs from a token sequence. For each center position, pair its token with every token inside a fixed context window, excluding the center itself and positions outside the sequence.
{(wi,wj):max(0,i−c)≤j≤min(n−1,i+c), j=i}Here, wi is the token at center position i, wj is a context token, c is window, and n is the sequence length. Emit pairs by increasing center position and then increasing context position. Return an int64 PyTorch tensor with shape (P,2), including shape (0,2) when no pair exists.
Examples
Input: token_ids = [0,1,2,3], window = 1
Output: [[0,1],[1,0],[1,2],[2,1],[2,3],[3,2]]
Explanation: Each token is paired with its immediate neighbors while the sequence boundaries are respected.
Input: token_ids = [5,6,7], window = 2
Output: [[5,6],[5,7],[6,5],[6,7],[7,5],[7,6]]
Input: token_ids = [9], window = 1
Output: []
Hint 1
Clamp the context loop to the sequence boundaries for each center index.
Hint 2
Reshape an empty result to (0, 2) before returning it.
Requirements
- Use PyTorch.
- Generate every valid center-context pair inside the fixed window.
- Exclude self-pairs and do not wrap around sequence boundaries.
- Preserve the documented deterministic order.
- Return the documented int64 tensor.
Constraints
- token_ids is a one-dimensional int64 tensor.
- token_ids contains at least one token.
- window is a positive integer no greater than six.
- P may be zero.
All steps in Word2Vec
- Subsampling
- Skip-gram Pairs
- Negative Sampling
- SGNS Loss
- CBOW Forward
- SGNS Step
Accepts: array
Accepts: number