https://paperswithcode.co/paper/1301.3781
This paper focuses on:
- learning word embeddings more efficiently from larger corpora of text
- embeddings should capture both grammatical and semantic relationships between words
- designing models which give embeddings where linear relationships are preserved:
Queen = King - Man + Woman
- I have used text8. download and place the txt file in
/data. - use
/scripts/split_dataset.pyto producetiny.txt,small.txtandmedium.txtfromraw.txt.
- Install the dataset and run
/scripts/split_dataset.py. - Train a model using
train_cbow_hs.pyortrain_skipgram_hs.py. pass command-line args forepochs,sizeandlr. - Explore the model's learnings using
test_similarity.pyandtest_analogy.py.
Training Loop:
We have W_in, W_out, ctx_word_ids, target_word_id
-
Forward pass: ctx vectors generated from
W_in[ctx_word_ids]. h is mean vector. scores obtained by matrix mult'ingW_out(V, D) andh(D, ) and then softmax'ing. -
Loss fn: scores of all words from vocab are generated in forward pass. ideally,
target_wordshould have highest score. hence, loss fn. is-log(prob[target_word]). so lower the probability, higher the loss. -
Backpropagation:
- so ideal score is one hot representation of target word, so
dscoresbasically computes diff between what has been predicated and what is "ideally correct". this isdscores(delta scores). dW_outcomputes which vectors cause this version ofdscores, because we previously didW_out @ h.dhcomputes how much context representation should change, ashis mean vector.dcontextjust speads out the delta among all words.- update
W_inandW_outaccordingly, taking into accountlearning_rate.
- so ideal score is one hot representation of target word, so
tests can be run with:
python3 -m tests.<test_name>most_similar(word) in similarity.py returns the k-most similar words to word.
analogy() from analogy.py finds x where a : b :: c : x and a, b and c are known words.
What is hierarchical softmax?
traditional softmax computes per word probabilities by performing dot products of millions of word vectors by multiplying
W_outwithh(mean vector of context words). hierarchical softmax arranges words in a huffman tree and then each internal node has a weight vector, which producesp, which is probability of the model choosing to go to the right node.
This converts a multi-class classification problem into a series of binary classification problems.
very similar intuitively to CBOW architecture. predicts multiple possible words from a single word compared to prediciting a single missing word from multiple context words.
R ~ Uniform(1, C)
This is supposed to perform better because words closer to the target are trained more to guess that specific word. Intuitively, words closer to the target contribute significantly more compared to the ones C-1 words away.
epoch,lrandcorpus_sizearguments can be passed from the terminal; implemented usingargparse.model_io.pycontains functions to save and load trained models.