8

TaggedDocument请帮助我理解工作方式和LabeledSentence工作方式之间的区别gensim。我的最终目标是使用Doc2Vec模型和任何分类器进行文本分类。我正在关注这个博客

class MyLabeledSentences(object):
    def __init__(self, dirname, dataDct={}, sentList=[]):
        self.dirname = dirname
        self.dataDct = {}
        self.sentList = []
    def ToArray(self):       
        for fname in os.listdir(self.dirname):            
            with open(os.path.join(self.dirname, fname)) as fin:
                for item_no, sentence in enumerate(fin):
                    self.sentList.append(LabeledSentence([w for w in sentence.lower().split() if w in stopwords.words('english')], [fname.split('.')[0].strip() + '_%s' % item_no]))
        return sentList


class MyTaggedDocument(object):
    def __init__(self, dirname, dataDct={}, sentList=[]):
        self.dirname = dirname
        self.dataDct = {}
        self.sentList = []
    def ToArray(self):       
        for fname in os.listdir(self.dirname):            
            with open(os.path.join(self.dirname, fname)) as fin:
                for item_no, sentence in enumerate(fin):
                    self.sentList.append(TaggedDocument([w for w in sentence.lower().split() if w in stopwords.words('english')], [fname.split('.')[0].strip() + '_%s' % item_no]))
        return sentList

sentences = MyLabeledSentences(some_dir_name)
model_l = Doc2Vec(min_count=1, window=10, size=300, sample=1e-4, negative=5,     workers=7)
sentences_l = sentences.ToArray()
model_l.build_vocab(sentences_l )
for epoch in range(15): # 
    random.shuffle(sentences_l )
    model.train(sentences_l )
    model.alpha -= 0.002  # decrease the learning rate
    model.min_alpha = model_l.alpha 

sentences = MyTaggedDocument(some_dir_name)
model_t = Doc2Vec(min_count=1, window=10, size=300, sample=1e-4, negative=5, workers=7)
sentences_t = sentences.ToArray()
model_l.build_vocab(sentences_t)
for epoch in range(15): # 
    random.shuffle(sentences_t)
    model.train(sentences_t)
    model.alpha -= 0.002  # decrease the learning rate
    model.min_alpha = model_l.alpha

我的问题是model_l.docvecs['some_word']一样的model_t.docvecs['some_word']?您能否为我提供良好资源的网络链接,以了解如何TaggedDocument或如何LabeledSentence工作。

4

1 回答 1

7

LabeledSentence是用于封装文本示例的相同简单对象类型的较旧的、已弃用的名称,现在称为TaggedDocument. 任何具有wordstags属性的对象,每个对象都是一个列表,都可以。(words始终是字符串列表;tags可以是整数和字符串的混合,但在最常见和最有效的情况下,它只是一个具有单个 id 整数的列表,从 0 开始。)

model_l并且model_t将服务于相同的目的,使用相同的参数对相同的数据进行训练,只是为对象使用不同的名称。但是它们为单个词标记 model['some_word']model.docvecs['somefilename_NN']

于 2017-01-19T02:59:31.443 回答