首页 > 解决方案 > 将迭代器传递给python中的父类init

问题描述

我想要做的是将Doc2Vec课堂包裹起来gensim并给它一个培训方法。通常,如果我想训练一个新模型,我会执行以下操作:

import glob
from gensim.models import doc2vec, Doc2Vec


class tagged_documents(object):
    def __init__(self, path):
        self.file_l = [name for name in glob.iglob(path, recursive=True)]
    
    def __iter__(self):
        for f_id, f_path in enumerate(self.file_l):
            with open(f_path, 'r') as f:
                docu = f.read()
                yield doc2vec.TaggedDocument(words=docu, tags=(f'DOC_{f_id}',))



Docs = tagged_documents('/path/to/docs/*')
    
Doc2Vec(Docs, min_count = 100, 
                 vector_size=300, 
                 epochs = 20, 
                 negative = 5, 
                 workers=20, 
                 sample = 1e-5,
                 alpha=0.01,
                 min_alpha=0.0001)

这很好用

现在我想做的是这个包装器

class doc2vec_model(Doc2Vec):

    def train(self,docs):
        super(Doc2Vec, self).__init__(docs, min_count = 100, 
                                     vector_size=300, 
                                     epochs = 20, 
                                     negative = 5, 
                                     workers=20, 
                                     sample = 1e-5,
                                     alpha=0.01,
                                     min_alpha=0.0001)

        
Docs = tagged_documents('/path/to/docs/*')
model = doc2vec_model()
model.train(Docs)

TypeError: 'NoneType' object is not iterable当我尝试调用该model.train()函数时,我得到了异常

标签: pythonpython-3.xoop

解决方案


推荐阅读