简体   繁体   中英

Issues in doc2vec tags in Gensim

I am using gensim doc2vec as below.

from gensim.models import doc2vec
from collections import namedtuple
import re

my_d = {'recipe__001__1': 'recipe 1 details should come here',
 'recipe__001__2': 'Ingredients of recipe 2 need to be added'}
docs = []
analyzedDocument = namedtuple('AnalyzedDocument', 'words tags')
for key, value in my_d.items():
    value = re.sub("[^a-zA-Z]"," ", value)
    words = value.lower().split()
    tags = key
    docs.append(analyzedDocument(words, tags))
model = doc2vec.Doc2Vec(docs, size = 300, window = 10, dm=1, negative=5, hs=0, min_count = 1, workers = 4, iter = 20)

However, when I check model.docvecs.offset2doctag I get ['r', 'e', 'c', 'i', 'p', '_', '0', '1', '2'] as the output. The real output should be `'recipe__001__1' and 'recipe__001__2'.

When I use len(model.docvecs.doctag_syn0) I get 9 as the output. But the real value should be 2 because I only have 2 recipes in my test dictionary.

Please let me know, why this happens?

Try to change this line:

tags = key

to

tags = [key]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM