【问题标题】:How to solve Memory Error with Spacy PhraseMatcher?如何使用 Spacy PhraseMatcher 解决内存错误?
【发布时间】:2019-08-19 14:03:20
【问题描述】:

高级背景

我正在开展一个项目,第一步是在大型文本语料库中搜索关键字和短语。我想确定这些关键字出现的段落/句子。稍后我想通过我本地的 postgres db 使这些段落可以访问,以便用户查询信息。数据存储在 Azure Blob Storage 上,我正在使用 Minio Server 连接我的 Django 应用程序。

实际问题

首先,我的 shell 被杀死,经过一些尝试和错误的重构/调试内存错误,运行我的脚本时:

  1. 从 blob 存储中随机抽取 30 个文本文档(我想抽样 10000 个,但它的数量已经很少了),
  2. 为 nlp 任务预处理原始文本,
  3. 通过 spacy 的 nlp.pipe 流式传输文本以获取文档列表和
  4. 将文档列表流式传输到 PhraseMatcher(它将 on_match 规则 ID、句子的开始标记(匹配)、句子 hash_id 传递到 match_list)。

起初,shell 被杀死了。我查看了日志文件,发现这是一个内存错误,但老实说,我对这个话题还很陌生。

重新排列代码后,我直接在 shell 中得到了 MemoryError。在将文本流式传输到 spaCy 的 language.pipe() 步骤中。

代码摘录

功能

# Function that samples filing_documents
def random_Filings(amount):
 ...
 return random_list

# Function that connects to storage and saves cleaned text
def get_clean_text(random_list):
  try:
    text_contents = S3Client().get_buffer(remote_path)
  ...
return clean_list

# matcher function that performs action on match of PhraseMatcher
def on_match(matcher, doc, id, matches):
  matcher_id, start, end = matches[id]
  rule_id = nlp.vocab.strings[match_id]
  token = doc[start]
  sent_of_token = token.sent
  match_list.append([str(rule_id), sent_of_token.start, sent_of_token, 
  doc.user_data])

def match_text_stream(clean_texts):
   some_pattern = [nlp(text) for text in ('foo', 'bar')]
   some_other_pattern = [nlp(text) for text in ('foo bar', 'barara')]

   matcher = PhraseMAtcher(nlp.vocab)

   matcher.add('SOME', on_match, *some_pattern)
   matcher.add('OTHER', on_match, *some_other_pattern)

   doc_list = []

   for doc in nlp.pipe(list_of_text, barch_size=30):
     doc_list.append(doc)

   for doc in matcher.pipi(doc_list, batch_size=30):
     pass

问题步骤:

match_list = []

nlp = en_core_web_sm.load()
sample_list = random_Filings(30)
clean_texts = get_clean_text(sample_list)
match_text_stream(clean_text)

print(match_list)

错误信息

MemoryError
<string> in in match_text_stream(clean_text)

../spacy/language.py in pipe(self, texts, as_tubles, n thready, batch_size, disable cleanup, component_cfg)

709 origingal_strings_data = None
710 nr_seen = 0
711 for doc in docs:
712   yield doc
713   if cleanup:

...

MemoryError


../tick/neural/_classes/convolution.py in begin_update(self, X__bi, drop)

31
32 def(bedin_update(self,X__bi, drop=0.0):
33   X__bo = self.ops.seqcol(X__bi, self.nW)
34   finish_update = self._get_finsih_update()
35   return X__bo, finish_update

ops.pyx in thinc.neural.ops.NumpyOps.seq2col()
ops.pyx in thinc.neural.ops.NumpyOps.allocate()

MemoryError:

【问题讨论】:

    标签: python-3.x spacy match-phrase


    【解决方案1】:

    解决方案是在训练之前将您的文档切成小块。段落单元工作得很好,或者可能是部分。

    【讨论】:

    • 目前我不是在训练,而是使用 PhraseMatcher 来识别这些段落。
    • 感谢您的提示。我注意到确实有一些文档非常大,而我的 VM 的 RAM 太少,无法处理它......
    猜你喜欢
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    相关资源
    最近更新 更多