# How to use pre trained word2vec model?

Where can I find a reliable word2vec model trained on some English articles?

I need a word2vec black box, where I, for example, can pass a sentence as array: ["London", "is", "the", "capital", "of", "Great", "Britain"]

and receive: [some_vector_of_floats1, some_vector_of_floats2, some_vector_of_floats3, some_vector_of_floats4, some_vector_of_floats5, some_vector_of_floats6, some_vector_of_floats7]

## 2 Answers

In Python, you can use Gensim

import gensim
model = gensim.models.Word2Vec.load_word2vec_format('path-to-vectors.txt', binary=False)
# if you vector file is in binary format, change to binary=True
sentence = ["London", "is", "the", "capital", "of", "Great", "Britain"]
vectors = [model[w] for w in sentence]


These vectors should give you better performance than the pre-trained ones you'd get with word2vec.

• thank you very much. it probably works, but I receive the error KeyError: 'of' – Dmitry Nalyvaiko Mar 13 '17 at 14:05
• That means of is not in your model's vocabulary. Which vectors are you using? – Alexandre Mar 13 '17 at 14:06
• GoogleNews-vectors-negative300.bin – Dmitry Nalyvaiko Mar 13 '17 at 14:06
• I use that model in node-word2vec and it works there with sentence about London – Dmitry Nalyvaiko Mar 13 '17 at 14:09
• Did you change binary=True to binary=False as noted? The pre-trained Google vectors are in binary format. – Alexandre Mar 13 '17 at 14:16

I slightly modified the code - for my purposes

vocab = model.vocab.keys()
sentence = ["London", "is", "the", "capital", "of", "Great", "Britain"]
vectors=[]
for w in sentence:
if w in vocab:
vectors.append(model[w])
else:
print("Word {} not in vocab".format(w))
vectors.append([0])


You could also use a try/catch - your call.