This article mainly introduces the use of Python in the Gensim library Word2vec, has a certain reference value, now share to everyone, the need for friends can refer to
After the PIP install Gensim is installed, it is ready for import:
1. Training Model Definition
From gensim.models import Word2vec model = Word2vec (sentences, sg=1, size=100, window=5, min_count=5, negative=3, sample=0 .001, Hs=1, workers=4)
Parameter explanation:
1.sg=1 is a skip-gram algorithm, sensitive to low-frequency words; The default sg=0 is the Cbow algorithm.
2.size is the dimension of the output word vector, the value is too small can cause the word map to affect the result because of conflict, the value is too large will consume memory and make algorithm calculation slow, the general value is 100 to 200.
3.window is the maximum distance between the current word and the target word in the sentence, 3 means to see the 3-b word before the target word, followed by the B-word (b is random between 0-3).
4.min_count is the word filter, the frequency of less than min-count words will be ignored, the default value is 5.
5.negative and sample can be fine-tuned based on the training results, and sample indicates that the higher-frequency words are randomly sampled to the thresholds set, with a default value of 1e-3.
6.hs=1 indicates that the hierarchy Softmax will be used, the default hs=0 and negative not 0, the negative sampling will be selected for use.
7.workers control training in parallel, this parameter is only valid after installing CPython, otherwise only single core can be used.
Detailed parameter description to view Word2vec source code.
2, after the training model saving and loading
Model.save (fname) model = Word2vec.load (fname)
3, the use of the model (Word similarity calculation, etc.)
Model.most_similar (positive=[' woman ', ' King '), negative=[' Man ']) #输出 [(' Queen ', 0.50882536), ...] Model.doesnt_match ("Breakfast cereal Dinner Lunch". Split ()) #输出 ' cereal ' model.similarity (' Woman ', ' Man ') # Output 0.73723527 model[' computer '] # raw numpy vector of a word #输出array ([ -0.00449447, -0.00310097, 0.02421786, ...], Dtyp E=FLOAT32)
Other content no longer repeat, please refer to Gensim's Word2vec official note, which is detailed.