gpt4 book ai didi

python - 空间内存错误

转载 作者:行者123 更新时间:2023-12-01 08:29:05 24 4
gpt4 key购买 nike

我设法安装了 spacy,但是当尝试使用 nlp 时,由于某种奇怪的原因,我收到了 MemoryError。

我写的代码如下:

import spacy
import re
from nltk.corpus import gutenberg

def clean_text(astring):
#replace newlines with space
newstring=re.sub("\n"," ",astring)
#remove title and chapter headings
newstring=re.sub("\[[^\]]*\]"," ",newstring)
newstring=re.sub("VOLUME \S+"," ",newstring)
newstring=re.sub("CHAPTER \S+"," ",newstring)
newstring=re.sub("\s\s+"," ",newstring)
return newstring.lstrip().rstrip()

nlp=spacy.load('en')
alice=clean_text(gutenberg.raw('carroll-alice.txt'))
nlp_alice=list(nlp(alice).sents)

我收到的错误如下

The error message

虽然当我的代码是这样的时候它就可以工作:

import spacy

nlp=spacy.load('en')
alice=nlp("hello Hello")

如果有人能指出我做错了什么,我将非常感激

最佳答案

我猜你确实内存不足了。我找不到确切的数字,但我确信卡罗尔的《爱丽丝梦游仙境》有数万个句子。这相当于 Spacy 中的数万个 Span 元素。在不进行修改的情况下,nlp() 会确定从 POS 到传递给它的字符串的依赖关系的所有内容。此外,sents 属性返回一个应该利用的迭代器,而不是立即在列表中扩展。

基本上,您正在尝试进行很可能会遇到内存限制的计算。您的机器支持多少内存?在评论中,乔建议观察机器的内存使用情况,我对此表示赞同。我的建议:检查您是否确实内存不足,或限制 nlp() 的功能,或考虑使用迭代器功能进行工作:

for sentence in nlp(alice).sents:
pass

关于python - 空间内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54014422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com