I have two directories from which I want to read their text files and label them, but I don't know how to do this via TaggedDocument
. I thought it would work as TaggedDocument([Strings],[Labels]) but this doesn't work apparently.
This is my code:
from gensim import models
from gensim.models.doc2vec import TaggedDocument
import utilities as util
import os
from sklearn import svm
from nltk.tokenize import sent_tokenize
CogPath = "./FixedCog/"
NotCogPath = "./FixedNotCog/"
SamplePath ="./Sample/"
docs = []
tags = []
CogList = [p for p in os.listdir(CogPath) if p.endswith('.txt')]
NotCogList = [p for p in os.listdir(NotCogPath) if p.endswith('.txt')]
SampleList = [p for p in os.listdir(SamplePath) if p.endswith('.txt')]
for doc in CogList:
str = open(CogPath+doc,'r').read().decode("utf-8")
docs.append(str)
print docs
tags.append(doc)
print "###########"
print tags
print "!!!!!!!!!!!"
for doc in NotCogList:
str = open(NotCogPath+doc,'r').read().decode("utf-8")
docs.append(str)
tags.append(doc)
for doc in SampleList:
str = open(SamplePath + doc, 'r').read().decode("utf-8")
docs.append(str)
tags.append(doc)
T = TaggedDocument(docs,tags)
model = models.Doc2Vec(T,alpha=.025, min_alpha=.025, min_count=1,size=50)
and this is the error I get:
Traceback (most recent call last):
File "/home/farhood/PycharmProjects/word2vec_prj/doc2vec.py", line 34, in <module>
model = models.Doc2Vec(T,alpha=.025, min_alpha=.025, min_count=1,size=50)
File "/home/farhood/anaconda2/lib/python2.7/site-packages/gensim/models/doc2vec.py", line 635, in __init__
self.build_vocab(documents, trim_rule=trim_rule)
File "/home/farhood/anaconda2/lib/python2.7/site-packages/gensim/models/word2vec.py", line 544, in build_vocab
self.scan_vocab(sentences, progress_per=progress_per, trim_rule=trim_rule) # initial survey
File "/home/farhood/anaconda2/lib/python2.7/site-packages/gensim/models/doc2vec.py", line 674, in scan_vocab
if isinstance(document.words, string_types):
AttributeError: 'list' object has no attribute 'words'
min_alpha
be the same value as the startingalpha
means your training isn't doing proper stochastic gradient descent. Also, it's rare formin_count=1
to be helpful in Word2Vec/Doc2Vec training – keeping such rare words just tends to make training take longer and interfere with the quality of the remaining word-vecs/doc-vecs. – Outofdoorsmin_alpha
, I've copied it from a sample code followed by this code:for epoch in range(10): model.train(docs) model.alpha -= 0.002 # decrease the learning rate model.min_alpha = model.alpha # fix the learning rate, no decay
and about themin_count
: my data set is very limited and some words are not that much frequent but weigh a lot in the meaning, also I have filtered most stop words and frequent daily words. – Cloningeralpha
tomin_alpha
, and you shouldn't calltrain()
yourself. (And if you do, like you've shown without any other specifics, the latest gensim versions will throw an error because it's such a common mistake.) It is a rare, expert thing to calltrain()
yourself or much with the defaultalpha
/min_alpha
. – Outofdoors