Search code examples
nlpdata-mininggensimword2vecdoc2vec

Why Gensim most similar in doc2vec gives the same vector as the output?


I am using the following code to get the ordered list of user posts.

model = doc2vec.Doc2Vec.load(doc2vec_model_name)
doc_vectors = model.docvecs.doctag_syn0
doc_tags = model.docvecs.offset2doctag

for w, sim in model.docvecs.most_similar(positive=[model.infer_vector('phone_comments')], topn=4000):
        print(w, sim)
        fw.write(w)
        fw.write(" (")
        fw.write(str(sim))
        fw.write(")")
        fw.write("\n")

fw.close()

However, I am also getting the vector "phone comments" (that I use to find nearest neighbours) in like 6th place of the list. Is there any mistake I do in the code? or is it a issue in Gensim (becuase the vector cannot be a neighbour of itself)?

EDIT

Doc2vec model training code

######Preprocessing
docs = []
analyzedDocument = namedtuple('AnalyzedDocument', 'words tags')
for key, value in my_d.items():
    value = re.sub("[^1-9a-zA-Z]"," ", value)
    words = value.lower().split()
    tags = key.replace(' ', '_')
    docs.append(analyzedDocument(words, tags.split(' ')))

sentences = []  # Initialize an empty list of sentences
######Get n-grams
#Get list of lists of tokenised words. 1 sentence = 1 list
for item in docs:
    sentences.append(item.words)

#identify bigrams and trigrams (trigram_sentences_project) 
trigram_sentences_project = []
bigram = Phrases(sentences, min_count=5, delimiter=b' ')
trigram = Phrases(bigram[sentences], min_count=5, delimiter=b' ')

for sent in sentences:
    bigrams_ = bigram[sent]
    trigrams_ = trigram[bigram[sent]]
    trigram_sentences_project.append(trigrams_)

paper_count = 0
for item in trigram_sentences_project:
    docs[paper_count] = docs[paper_count]._replace(words=item)
    paper_count = paper_count+1

# Train model
model = doc2vec.Doc2Vec(docs, size = 100, window = 300, min_count = 5, workers = 4, iter = 20)

#Save the trained model for later use to take the similarity values
model_name = user_defined_doc2vec_model_name
model.save(model_name)

Solution

  • The infer_vector() method expects a list-of-tokens, just like the words property of the text examples (TaggedDocument objects, usually) that were used to train the model.

    You're supplying a simple string, 'phone_comments', which will look to infer_vector() like the list ['p', 'h', 'o', 'n', 'e', '_', 'c', 'o', 'm', 'm', 'e', 'n', 't', 's']. Thus your origin vector for the most_similar() is probably garbage.

    Further, you're not getting back the input 'phone_comments', you're getting back the different string 'phone comments'. If that's a tag-name in the model, then that must have been a supplied tag during model training. Its superficial similarity to phone_comments may be meaningless - they're different strings.

    (But it may also hints that your training had problems, too, and trained the text that should have been words=['phone', 'comments'] as words=['p', 'h', 'o', 'n', 'e', ' ', 'c', 'o', 'm', 'm', 'e', 'n', 't', 's'] instead.)