Before you start
Generate the (center, context) training pairs that word2vec's skip-gram model learns from.
Skip-gram learns a word's vector by asking it to predict the words around it.
For every position i in a token list, the center word tokens[i] is paired
with each word at most window positions away on either side, but not with
itself. Words that appear in similar contexts end up with similar vectors.
Emit the pairs in this order: centers from left to right, and for each center, context positions from left to right.
def skipgram_pairs(tokens, window):
Input
skipgram_pairs(["the", "cat", "sat", "down"], window=1)
Output
[("the", "cat"), ("cat", "the"), ("cat", "sat"),
("sat", "cat"), ("sat", "down"), ("down", "sat")]
Reasoning
With a window of 1, each word pairs with its direct neighbours. The first and last words have only one neighbour each, so they give one pair. The middle words give two.
Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell