Search code examples
pythongensimword2vecdoc2vec

How to get the Document Vector from Doc2Vec in gensim 0.11.1?


Is there a way to get the document vectors of unseen and seen documents from Doc2Vec in the gensim 0.11.1 version?

  • For example, suppose I trained the model on 1000 thousand - Can I get the doc vector for those 1000 docs?

  • Is there a way to get document vectors of unseen documents composed
    from the same vocabulary?


Solution

  • For the first bullet point, you can do it in gensim 0.11.1

    from gensim.models import Doc2Vec
    from gensim.models.doc2vec import LabeledSentence
    
    documents = []
    documents.append( LabeledSentence(words=[u'some', u'words', u'here'], labels=[u'SENT_1']) )
    documents.append( LabeledSentence(words=[u'some', u'people', u'words', u'like'], labels=[u'SENT_2']) )
    documents.append( LabeledSentence(words=[u'people', u'like', u'words'], labels=[u'SENT_3']) )
    
    
    model = Doc2Vec(size=10, window=8, min_count=0, workers=4)
    model.build_vocab(documents)
    model.train(documents)
    
    print(model[u'SENT_3'])
    

    Here SENT_3 is a known sentence.

    For the second bullet point, you can NOT do it in gensim 0.11.1, you have to update it to 0.12.4. This latest version has infer_vector function which can generate a vector for an unseen document.

    documents = []
    documents.append( LabeledSentence([u'some', u'words', u'here'], [u'SENT_1']) )
    documents.append( LabeledSentence([u'some', u'people', u'words', u'like'], [u'SENT_2']) )
    documents.append( LabeledSentence([u'people', u'like', u'words'], [u'SENT_3']) )
    
    
    model = Doc2Vec(size=10, window=8, min_count=0, workers=4)
    model.build_vocab(documents)
    model.train(documents)
    
    print(model.docvecs[u'SENT_3']) # generate a vector for a known sentence
    print(model.infer_vector([u'people', u'like', u'words'])) # generate a vector for an unseen sentence