python - 将迭代器传递给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()
函数时,我得到了异常
解决方案
推荐阅读
- r - 如何使用 R markdown 和 pandoc 防止 LaTeX 文档中的 ~ (波浪号)字符转义?
- r - R:对按特定列分组的特定列求和
- algorithm - 如何选择仅包含已知单词的句子的数据结构
- git - 隐藏当前分支和主分支之间的差异以将其应用于旧提交
- python - 如何处理空的“DataFrame”:没有数字数据来绘制错误以在图表上获取字符串
- flutter - 流控制器未注册接收器事件颤动
- angular-forms - 如何以角度形式实现速记验证?
- python - Django Rest Framework - 如何对 MSSQL 存储过程中的数据进行建模和序列化
- python - Django 无法读取其他应用程序的模型
- python - 使用 Python 和 Selenium 将 APK 上传到解释为 URL 的网页