Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python语句的word2vec查找两个句子之间的相似性_Python_Nlp - Fatal编程技术网

使用python语句的word2vec查找两个句子之间的相似性

使用python语句的word2vec查找两个句子之间的相似性,python,nlp,Python,Nlp,我想用word2vectors计算两个句子之间的相似度,我试图得到一个句子的向量,这样我就可以计算一个句子向量的平均值,找到余弦相似度。我尝试过这个代码,但它不起作用。它的输出给出了带有1的句子向量。我想要句子的实际向量,在句子1,平均向量和句子2,平均向量中 代码: 我认为你正在努力实现以下目标: 从word2vec中获取句子中每个单词的向量表示 求一个句子的所有词向量的平均值以获得一个句子表示 计算两个句子向量之间的余弦相似度 虽然2和3的代码在我看来总体上还不错(不过还没有测试),但问题可

我想用word2vectors计算两个句子之间的相似度,我试图得到一个句子的向量,这样我就可以计算一个句子向量的平均值,找到余弦相似度。我尝试过这个代码,但它不起作用。它的输出给出了带有1的句子向量。我想要句子的实际向量,在句子1,平均向量和句子2,平均向量中

代码:


我认为你正在努力实现以下目标:

  • 从word2vec中获取句子中每个单词的向量表示
  • 求一个句子的所有词向量的平均值以获得一个句子表示
  • 计算两个句子向量之间的余弦相似度
  • 虽然2和3的代码在我看来总体上还不错(不过还没有测试),但问题可能出在步骤1中。您在代码中使用的是什么

    word2vec\u model=gensim.models.word2vec(句子,大小=100,最小计数=5)

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

    您可能想做的是使用预训练的word2vec模型,并在代码中与gensim一起使用。根据,这可以使用
    键向量。load\u word2vec\u format
    方法完成。

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

    featureVec=np.one((num_features),dtype=“float32”)

    featureVec=np.zero((num_特征),dtype=“float32”)

    如果在字典(index2word_集)中找不到任何单词,则应将其全部置零。
    这解决了我的问题。你想通过查找和平均预计算的word2vec向量来计算句子的向量表示,还是从头开始计算?你的代码看起来像是在尝试后者。。。但我不认为你能从两句话中学到任何有用的嵌入。人们通常会用上百万个词来表达。也许这会有帮助。这实际上不是两句话。。我的数据集包含8行lacs+句子。。为了方便起见,我在这里提到了一些示例数据来传达我的概念……我也面临着同样的问题。你能告诉我mylist1和mylist2是什么吗?我在第一步中确实遇到了问题。。问题可能出在词汇上。。当我在bin文件中保存向量模型时,它只给我字母的向量。
        #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