2

Python で gensim word2vec ライブラリを使用し、事前トレーニング済みの GoogleNews-vectors-negative300.bin モデルを使用しています。しかし、

コーパスに単語ベクトルがない単語があり、そのために keyError を取得しています。この問題を解決するにはどうすればよいですか?

これが私がこれまでに試したことです、

GoogleNews-vectors-negative300.bin1:トレーニング済みモデルごとの読み込み:

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 モデルに新しい単語を追加する方法を教えてください。

4

1 に答える 1