2017-08-24 18 views
1

word2vectorsを使って2つの文の類似度を計算したいのですが、文のベクトルを取得しようとしていますコサイン類似度。私はこのコードを試しましたが、動作しません。出力は1の文ベクトルを与える。私はsentence_1_avg_vectorの文の実際のベクトルを& sentence_2_avg_vectorにしたい。Find Pythonで文のword2vecを使って2文の類似度を調べる

コード:

#DataSet# 
    sent1=[['What', 'step', 'step', 'guide', 'invest', 'share', 'market', 'india'],['What', 'story', 'Kohinoor', 'KohiNoor', 'Diamond']] 
    sent2=[['What', 'step', 'step', 'guide', 'invest', 'share', 'market'],['What', 'would', 'happen', 'Indian', 'government', 'stole', 'Kohinoor', 'KohiNoor', 'diamond', 'back']] 
    sentences=sent1+sent2 

    #''''Applying Word2vec''''# 
    word2vec_model=gensim.models.Word2Vec(sentences, size=100, min_count=5) 
    bin_file="vecmodel.csv" 
    word2vec_model.wv.save_word2vec_format(bin_file,binary=False) 

    #''''Making Sentence Vectors''''# 
    def avg_feature_vector(words, model, num_features, index2word_set): 
     #function to average all words vectors in a given paragraph 
     featureVec = np.ones((num_features,), dtype="float32") 
     #print(featureVec) 
     nwords = 0 
     #list containing names of words in the vocabulary 
     index2word_set = set(model.wv.index2word)# this is moved as input param for performance reasons 
     for word in words: 
      if word in index2word_set: 
       nwords = nwords+1 
       featureVec = np.add(featureVec, model[word]) 
       print(featureVec) 
     if(nwords>0): 
      featureVec = np.divide(featureVec, nwords) 
     return featureVec 

    i=0 
    while i<len(sent1): 
     sentence_1_avg_vector = avg_feature_vector(mylist1, model=word2vec_model, num_features=300, index2word_set=set(word2vec_model.wv.index2word)) 
     print(sentence_1_avg_vector) 

     sentence_2_avg_vector = avg_feature_vector(mylist2, model=word2vec_model, num_features=300, index2word_set=set(word2vec_model.wv.index2word)) 
     print(sentence_2_avg_vector) 

     sen1_sen2_similarity = 1 - spatial.distance.cosine(sentence_1_avg_vector,sentence_2_avg_vector) 
     print(sen1_sen2_similarity) 

     i+=1 

出力このコードが得られます。

[ 1. 1. .... 1. 1.] 
[ 1. 1. .... 1. 1.] 
0.999999898245 
[ 1. 1. .... 1. 1.] 
[ 1. 1. .... 1. 1.] 
0.999999898245 
+0

あらかじめ計算されたword2vecベクトルを検索して平均化したり、最初から計算したりして、文章のベクトル表現を計算しますか?あなたのコードは、あなたが後者を試しているように見えます...しかし、私はあなたがただ2つの文から有用な埋め込みを学ぶことはできないと思います。人々は通常、そのために数百万語を使用します。 – Tobias

+0

おそらくこれが役に立ちます。 – alvas

+0

これは実際には2つのセンテンスではありません。私のデータセットには8つのlacs +行の文が含まれています。便宜上私のコンセプトを伝えるためにサンプルデータをここに挙げました... –

答えて

0

を、私はあなたが達成しようとしているものだと思う以下の通りです:

  1. ためword2vecからベクトル表現を取得しますあなたの文章のすべての単語。
  2. 文の表現を得るために、文のすべての単語ベクトルを平均します。
  3. 2つのセンテンスのベクトル間のコサイン類似度を計算します。

2と3のコードは(しかしそれをテストしていない)、一般的に私には罰金見えますが、問題は、あなたが

word2vec_model=gensim.models.Word2Vec(sentences, size=100, min_count=5)

であなたのコードでやっているステップ1である可能性があり

は、新しいword2vecモデルを初期化することです。 word2vec_model.train()に電話すると、gensimは文章の新しいモデルを訓練します。その結果、後で単語ごとに結果のベクトルを使うことができます。しかし、類似性のようなものを捕らえる有用な単語ベクトルを得るためには、通常、多くのデータにword2vecモデルを訓練する必要があります。model provided by Googleは1,000億語の訓練を受けました。

代わりにやりたいことは、事前に訓練されたword2vecモデルを使用して、コードでgensimと一緒に使用することです。 documentation of gensimによれば、これは方法KeyedVectors.load_word2vec_formatで行うことができます。

+0

問題はありますか?ボキャブラリを使って。ベクトルモデルをbinファイルに保存すると、文字だけのベクトルが得られます。 –

関連する問題