【问题标题】:Finding Similarity between 2 sentences using word2vec of sentence with python使用python的句子的word2vec查找2个句子之间的相似性
【发布时间】:2018-02-02 19:46:41
【问题描述】:

我想使用 word2vectors 计算两个句子之间的相似度,我正在尝试获取一个句子的向量,以便我可以计算一个句子向量的平均值来找到余弦相似度。我已经尝试过这段代码,但它不起作用。它的输出给出了带有一个的句子向量。我想要 sentence_1_avg_vector 和 sentence_2_avg_vector 中句子的实际向量。

代码:

    #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

【问题讨论】:

  • 您想通过查找和平均预先计算的 word2vec 向量来计算句子的向量表示,还是想从头开始计算它们?您的代码看起来像是在尝试后者……但我认为您不能仅从两句话中学习任何有用的嵌入。人们通常为此使用数百万个单词。
  • 也许这会有所帮助。
  • 这些实际上不是两个句子.. 我的数据集包含 8 lacs+ 行句子.. 为方便起见,我在这里提到了一些示例数据来传达我的概念...
  • 我也面临同样的问题。你能告诉我什么是 mylist1 和 mylist2 吗?

标签: python nlp


【解决方案1】:

您的第二部分(将文本转换为特征向量)是错误的。 你必须更换:

featureVec = np.ones((num_features,), dtype="float32")

featureVec = np.zeros((num_features,), dtype="float32").

如果在字典 (index2word_set) 中没有找到任何单词,那么它应该将它们全部归零。 这解决了我的问题。 ??

【讨论】:

    【解决方案2】:

    我认为您要实现的目标如下:

    1. 从 word2vec 获取句子中每个单词的向量表示。
    2. 平均一个句子的所有词向量以获得一个句子表示。
    3. 计算两个句子向量之间的余弦相似度。

    虽然 2 和 3 的代码对我来说总体上看起来不错(虽然还没有测试过),但问题可能出在第 1 步。您在代码中所做的事情

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

    是初始化一个新的word2vec模型。如果你随后调用word2vec_model.train(),gensim 会在你的句子上训练一个新模型,这样你就可以在之后为每个单词使用结果向量。但是,为了获得有用的词向量来捕捉相似度之类的东西,你通常需要在大量数据上训练 word2vec 模型——model provided by Google 是在 1000 亿个词上训练的。

    您可能想要做的是使用预训练的 word2vec 模型,并在您的代码中将其与 gensim 一起使用。根据documentation of gensim,这可以通过KeyedVectors.load_word2vec_format方法完成。

    【讨论】:

    • 我在第一步中确实有问题..问题可能出在词汇表上..当我将矢量模型保存在 bin 文件中时,它只给了我字母的矢量。
    猜你喜欢
    • 1970-01-01
    • 2016-08-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2020-01-22
    • 1970-01-01
    • 2017-05-28
    • 1970-01-01
    相关资源
    最近更新 更多