【问题标题】:Improving spaCy memory usage and run time when running on 400K+ documents?在 400K+ 文档上运行时提高 spaCy 内存使用和运行时间?
【发布时间】:2019-11-18 15:32:40
【问题描述】:

我目前有大约 400K+ 文档,每个文档都有一个关联的组和 ID 号。它们平均大约有 24K 个字符和 350 行。总共有大约 25 GB 的数据。目前,它们按组进行拆分,一次需要处理的文档数量减少到 15K 左右。在具有 128GB 内存的机器上运行时,我遇到了内存使用和分段错误的问题(我相信后者是前者的结果)。我已经改变了我处理文档的方式,使用批处理来一次处理它们。

批号

def batchGetDoc(raw_documents):
        out = []
        reports = []
        infos = []
        # Each item in raw_documents is a tuple of 2 items, where the first item is all 
        # information (report number, tags) that correlate with said document. The second 
        # item is the raw text of the document itself 
        for info, report in raw_documents:
            reports.append(report)
            infos.append(info)

        # Using en_core_web_sm as the model
        docs = list(SPACY_PARSER.pipe(reports))
        for i in range(len(infos)):
            out.append([infos[i],docs[i]])
        return out

我使用 500 的批量大小,即使这样,仍然需要一段时间。由于在完整文档而不是句子上使用.pipe(),这些速度和内存问题是否存在?单独通过并运行SPACY_PARSER(report)会更好吗?

我正在使用 spaCy 从每个文档中获取命名实体、它们的链接实体、依赖图和知识库。这样做会不会有丢失对 spaCy 以后获取所述数据很重要的信息的风险?

编辑:我应该提一下,我确实需要文档信息以供以后根据文档文本预测准确性

【问题讨论】:

  • 您可以尝试丢失初学者的 for 循环
  • @pissall 会的。我应该如何将文档信息与 spaCy 返回的文档匹配?

标签: python nlp spacy named-entity-recognition


【解决方案1】:

解析器和 NER 中存在内存泄漏,该问题在 v2.1.9 和 v2.2.2 中已修复,因此如有必要请更新。如果您有很长的文档,您可能希望将它们分成段落或部分进行处理。 (超过 1,000,000 个字符的文本会出现错误。)

一定要使用nlp.pipe() 来加快处理速度。您可以使用带有nlp.pipe()as_tuples 选项来传入(text, context) 元组并返回(doc, context) 元组,因此您不需要多个循环。你必须从上面的代码中处理你的元组反转,但是一旦你有了(text, context) 元组,你只需要这样的东西:

out = nlp.pipe(raw_documents, as_tuples=True)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多