私は公式のgithubリポジトリからpredict_output_wordメソッドを持っています。これはskip-gramで訓練されたwod2vecモデルのみを取り、すべての入力単語のインデックスのベクトルを合計することによって中間単語を予測しようとし、 は入力単語インデックスのnp_sumの長さで除算します。そうすると、最も可能性の高い単語を得るためにこれらの確率を合計した後に、予測された単語の確率を得るために、出力を考慮してsoftmaxを取る。より良い言葉を得るためにこれを他者に近づけるより良い方法があります。これは、短い文に対して非常に悪い結果をもたらすからです。下の はgithubのコードです。中間の単語を予測するword2vec
def predict_output_word(model, context_words_list, topn=10):
from numpy import exp, dtype, float32 as REAL,\
ndarray, empty, sum as np_sum,
from gensim import utils, matutils
"""Report the probability distribution of the center word given the context words as input to the trained model."""
if not model.negative:
raise RuntimeError("We have currently only implemented predict_output_word "
"for the negative sampling scheme, so you need to have "
"run word2vec with negative > 0 for this to work.")
if not hasattr(model.wv, 'syn0') or not hasattr(model, 'syn1neg'):
raise RuntimeError("Parameters required for predicting the output words not found.")
word_vocabs = [model.wv.vocab[w] for w in context_words_list if w in model.wv.vocab]
if not word_vocabs:
warnings.warn("All the input context words are out-of-vocabulary for the current model.")
return None
word2_indices = [word.index for word in word_vocabs]
#sum all the indices
l1 = np_sum(model.wv.syn0[word2_indices], axis=0)
if word2_indices and model.cbow_mean:
#l1 = l1/len(word2_indices)
l1 /= len(word2_indices)
prob_values = exp(dot(l1, model.syn1neg.T)) # propagate hidden -> output and take softmax to get probabilities
prob_values /= sum(prob_values)
top_indices = matutils.argsort(prob_values, topn=topn, reverse=True)
return [(model.wv.index2word[index1], prob_values[index1]) for index1 in top_indices] #returning the most probable output words with their probabilities
ようこそStackOverflow。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 MCVEコードを投稿して問題を正確に記述するまでは、効果的にお手伝いすることはできません。 投稿したコードをテキストファイルに貼り付け、説明した問題を再現できるはずです。 特に、問題を引き起こす小さなデータセットを提供してください。誰もがなければ、アルゴリズム、トレーニングの強さ、またはソリッドデータの欠如に問題があるかどうかは不明です。 – Prune