word2vec

Using pretrained gensim Word2vec embedding in keras

筅森魡賤 提交于 2019-12-04 01:26:55
问题 I have trained word2vec in gensim. In Keras, I want to use it to make matrix of sentence using that word embedding. As storing the matrix of all the sentences is very space and memory inefficient. So, I want to make embedding layer in Keras to achieve this so that It can be used in further layers(LSTM). Can you tell me in detail how to do this? PS: It is different from other questions because I am using gensim for word2vec training instead of keras. 回答1: Let's say you have following data that

How to get vocabulary word count from gensim word2vec?

北城以北 提交于 2019-12-04 00:59:36
问题 I am using gensim word2vec package in python. I know how to get the vocabulary from the trained model. But how to get the word count for each word in vocabulary? 回答1: Each word in the vocabulary has an associated vocabulary object, which contains an index and a count. vocab_obj = w2v.vocab["word"] vocab_obj.count Output for google news w2v model: 2998437 So to get the count for each word, you would iterate over all words and vocab objects in the vocabulary. for word, vocab_obj in w2v.vocab

pytorch --- word2vec 实现 --《Efficient Estimation of Word Representations in Vector Space》

廉价感情. 提交于 2019-12-04 00:36:18
论文来自Mikolov等人的《Efficient Estimation of Word Representations in Vector Space》 论文地址: 66666 论文介绍了2个方法,原理不解释... skim code and comment : # -*- coding: utf-8 -*- # @time : 2019/11/9 12:53 import numpy as np import torch import torch.nn as nn import torch.optim as optim from torch.autograd import Variable import matplotlib.pyplot as plt dtype = torch.FloatTensor # 3 Words Sentence sentences = [ "i like dog", "i like cat", "i like animal", "dog cat animal", "apple cat dog like", "dog fish milk like", "dog cat eyes like", "i like apple", "apple i hate", "apple i movie book music like", "cat dog hate",

Generator is not an iterator?

こ雲淡風輕ζ 提交于 2019-12-04 00:00:18
I have an generator (a function that yields stuff), but when trying to pass it to gensim.Word2Vec I get the following error: TypeError: You can't pass a generator as the sentences argument. Try an iterator. Isn't a generator a kind of iterator? If not, how do I make an iterator from it? Looking at the library code, it seems to simply iterate over sentences like for x in enumerate(sentences) , which works just fine with my generator. What is causing the error then? Generator is exhausted after one loop over it. Word2vec simply needs to traverse sentences multiple times (and probably get item

Gensim word2vec in python3 missing vocab

寵の児 提交于 2019-12-03 23:26:13
I'm using gensim implementation of Word2Vec. I have the following code snippet: print('training model') model = Word2Vec(Sentences(start, end)) print('trained model:', model) print('vocab:', model.vocab.keys()) When I run this in python2, it runs as expected. The final print is all the words in the vocabulary. However, if I run it in python3, I get an error: trained model: Word2Vec(vocab=102, size=100, alpha=0.025) Traceback (most recent call last): File "learn.py", line 58, in <module> train(to_datetime('-4h'), to_datetime('now'), 'model.out') File "learn.py", line 23, in train print('vocab:'

Loading pre-trained word2vec to initialise embedding_lookup in the Estimator model_fn

家住魔仙堡 提交于 2019-12-03 15:53:43
I am solving a text classification problem. I defined my classifier using the Estimator class with my own model_fn . I would like to use Google's pre-trained word2vec embedding as initial values and then further optimise it for the task at hand. I saw this post: Using a pre-trained word embedding (word2vec or Glove) in TensorFlow which explains how to go about it in 'raw' TensorFlow code. However, I would really like to use the Estimator class. As an extension, I would like to then train this code on Cloud ML Engine, is there a good way of passing in the fairly large file with initial values?

How to Train GloVe algorithm on my own corpus

Deadly 提交于 2019-12-03 12:27:49
问题 I tried to follow this. But some how I wasted a lot of time ending up with nothing useful. I just want to train a GloVe model on my own corpus (~900Mb corpus.txt file). I downloaded the files provided in the link above and compiled it using cygwin (after editing the demo.sh file and changed it to VOCAB_FILE=corpus.txt . should I leave CORPUS=text8 unchanged?) the output was: cooccurrence.bin cooccurrence.shuf.bin text8 corpus.txt vectors.txt How can I used those files to load it as a GloVe

Gensim: KeyError: “word not in vocabulary”

佐手、 提交于 2019-12-03 11:43:11
问题 I have a trained Word2vec model using Python's Gensim Library. I have a tokenized list as below. The vocab size is 34 but I am just giving few out of 34: b = ['let', 'know', 'buy', 'someth', 'featur', 'mashabl', 'might', 'earn', 'affili', 'commiss', 'fifti', 'year', 'ago', 'graduat', '21yearold', 'dustin', 'hoffman', 'pull', 'asid', 'given', 'one', 'piec', 'unsolicit', 'advic', 'percent', 'buy'] Model model = gensim.models.Word2Vec(b,min_count=1,size=32) print(model) ### prints: Word2Vec

How to find the closest word to a vector using word2vec

被刻印的时光 ゝ 提交于 2019-12-03 11:00:39
问题 I have just started using Word2vec and I was wondering how can we find the closest word to a vector suppose. I have this vector which is the average vector for a set of vectors: array([-0.00449447, -0.00310097, 0.02421786, ...], dtype=float32) Is there a straight forward way to find the most similar word in my training data to this vector? Or the only solution is to calculate the cosine similarity between this vector and the vectors of each word in my training data, then select the closest

How does gensim calculate doc2vec paragraph vectors

♀尐吖头ヾ 提交于 2019-12-03 10:40:39
i am going thorugh this paper http://cs.stanford.edu/~quocle/paragraph_vector.pdf and it states that " Theparagraph vector and word vectors are averaged or concatenated to predict the next word in a context. In the experiments, we use concatenation as the method to combine the vectors." How does concatenation or averaging work? example (if paragraph 1 contain word1 and word2): word1 vector =[0.1,0.2,0.3] word2 vector =[0.4,0.5,0.6] concat method does paragraph vector = [0.1+0.4,0.2+0.5,0.3+0.6] ? Average method does paragraph vector = [(0.1+0.4)/2,(0.2+0.5)/2,(0.3+0.6)/2] ? Also from this