2017-07-14 17 views
0

私は公式の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 
+0

ようこそStackOverflow。ヘルプドキュメントの投稿ガイドラインを読み、それに従ってください。 [最小、完全で検証可能な例](http://stackoverflow.com/help/mcve)がここに適用されます。 MCVEコードを投稿して問題を正確に記述するまでは、効果的にお手伝いすることはできません。 投稿したコードをテキストファイルに貼り付け、説明した問題を再現できるはずです。 特に、問題を引き起こす小さなデータセットを提供してください。誰もがなければ、アルゴリズム、トレーニングの強さ、またはソリッドデータの欠如に問題があるかどうかは不明です。 – Prune

答えて

0

word2vecアルゴリズムは単語を予測しようとすることにより、単語ベクトルを訓練し、それらの単語ベクトルは、他の目的のために有用である可能性がある単語予測があなたの本当であれば、その後、理想的なアルゴリズムである可能性が高いものではないがゴール。

word2vecのほとんどの実装では、個々の単語予測のための特定のインターフェースさえ提供していません。 gensimでは、predict_output_word()が最近追加されました。一部のモードでのみ動作します。 windowをトレーニング中と同じように扱うことはありません。有効な重み付けはありません。そして、それはかなり高価です - 本質的にすべての単語のモデルの予測をチェックし、次にトップNを報告します。 (トレーニング中に発生する「予測」は「スパース」であり、より効率的です。単なる例ではモデルを十分に実行してより良いものにすることができます)

単語予測が本当の目的なら、頻繁に使用される言葉の大部分のルックアップテーブルを計算するだけで、他の方法や他のnグラムの近くに表示するなど、他の方法よりも良い結果を得ることができます。

関連する問題