2016-12-16 37 views
5

Proszę mi pomóc w zrozumieniu różnicy między tym, jak działa TaggedDocument i LabeledSentence z gensim. Moim ostatecznym celem jest Klasyfikacja tekstu przy użyciu modelu Doc2Vec i dowolnego klasyfikatora. Śledzę to blog!Jaka jest różnica między gensim LabeledSentence a TaggedDocument

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 

Moje pytanie brzmi model_l.docvecs['some_word'] jest taka sama jak model_t.docvecs['some_word']? Czy możesz podać link do strony internetowej z dobrym źródłem informacji, aby dowiedzieć się, jak działa TaggedDocument lub LabeledSentence.

Odpowiedz

5

LabeledSentence to starsza, nieaktualna nazwa tego samego prostego typu obiektu do enkapsulacji tekstu-przykładu, który jest teraz nazywany TaggedDocument. Wszystkie obiekty, które mają właściwości words i tags, każda lista zrobi. (words zawsze jest listą ciągów; tags może być mieszanką i ciągi liczb całkowitych, ale we wspólnej i najbardziej efektywny przypadku, to tylko lista z jednym id integer, zaczynając od 0.)

model_l i model_t będzie służyć tym samym celom, po przeszkoleniu na tych samych danych o tych samych parametrach, przy użyciu tylko różnych nazw obiektów. Ale wektory, które zwrócą dla pojedynczych słówek (model['some_word']) lub znaczników dokumentu (model.docvecs['somefilename_NN']) będą prawdopodobnie inne - istnieje przypadkowość w inicjowaniu Word2Vec/Doc2Vec i próbkowaniu treningowym oraz wprowadzane przez zamawianie jittera podczas treningu wielowątkowego.