我在 python 中使用 gensim word2vec 库并使用预训练的 GoogleNews-vectors-negative300.bin 模型。但,
我的语料库中有词,但我没有词向量,因此我得到了 keyError 我该如何解决这个问题?
这是我到目前为止所尝试的,
1:加载GoogleNews-vectors-negative300.bin
每个训练的模型:
model = Word2Vec.load_word2vec_format('GoogleNews-vectors-negative300.bin', binary=True)
print "model loaded..."
2:使用推文中所有词向量的平均值构建训练集的词向量,然后进行缩放
def buildWordVector(text, size):
vec = np.zeros(size).reshape((1, size))
count = 0.
for word in text:
try:
vec += model[word].reshape((1, size))
count += 1.
#print "found! ", word
except KeyError:
print "not found! ", word #missing words
continue
if count != 0:
vec /= count
return vec
trained_vecs = np.concatenate([buildWordVector(z, n_dim) for z in x_train])
请告诉如何在预训练的 Word2vec 模型中添加新单词?