【问题标题】:Memory error after switching to python 64-bits切换到python 64位后出现内存错误
【发布时间】:2019-01-12 03:20:16
【问题描述】:

我正在使用 Python 64 位获得MemoryError。这是我的功能:

def entr_langue(path,nom_langue):
    mots_ts=[]
    table_tr=dict((ord(char),None) for char in string.punctuation)#table de translation/mapping
    with codecs.open(path,"r","utf-8") as filep:

        for i,line in enumerate(filep):
            #extraction par ligne
            line=" ".join(line.split()[1:])
            line=line.lower()
            line=re.sub(r"\d+"," ",line) #suppression des digits

            if len(line) !=0:
                line=line.translate(table_tr)#suppression des poncts
                mots_ts += line
                mots_ts.append(" ")#ajout des espaces

    ts_str=''.join(mots_ts)
    ts_str=re.sub(' +',' ',ts_str) #remp des series d'espaces par un seul espace
    seq_ts=[i for i in ts_str]


    #daba extraction des Bigram et les trier selon la frequ
    fn=BigramCollocationFinder.from_words(seq_ts)
    fn.apply_freq_filter(6) #"li 3ndhom frequ 9el m 6 ytfiltraw
    bigram_model=fn.ngram_fd.viewitems()
    bigram_model=sorted(fn.ngram_fd.viewitems(), key=lambda item: item[1],reverse=True)

    print (bigram_model)
    np.save(nom_langue+".npy",bigram_model)

错误:

File "C:/Users/msi/Documents/projIA/extraction_bigram.py", line 23, in entr_langue
    mots_ts += line
  MemoryError

【问题讨论】:

  • 您的输入文件有多大,可用的 RAM 有多少?
  • mots_ts += line 这行效率很低。将.append().extend() 用于列表。
  • 您可能还需要安装 64 位版本的 NLTK(或在安装 64 位版本的 Python 后重新安装)。
  • @KlausD.: lists 重载 += 使其在很大程度上等同于 extend。也就是说,OP 应该在这里使用append 进行一个不错的更改;因为linestr+=(和extend)都会单独添加line 中的每个字符,并且他们可能只是希望将整行作为单个值。
  • 旁注:各位,请停止使用codecs.openIt's buggy, slow, and unnecessary on Python 2.6 and higher, where io.open is available。在 Py3 上,openio.open 的别名,在 Py2 上,io.open 基本上是 codecs.open 的正确、高效版本。 with io.open(path,encoding="utf-8"): 是你想要的。

标签: python nltk


【解决方案1】:

如果你在 python 32bit 上没有这个错误,那应该是错误的移植代码。 因为在 python 64bit 上,您可以在列表中包含更多元素,标准 PC 实际上无法实现填充这些庞大的数据。但是,如果你甚至在 32 位操作系统上运行它,list 所包含的内容不会超过 4 GB(或类似的东西,我不确定。)

有关于内存限制的类似主题: Memory errors and list limits?

【讨论】:

  • 我只是认为你为你使用了所有可能的内存。在该链接中,您有使用sizeof() 函数检查其大小的代码示例:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-10
  • 1970-01-01
  • 2018-04-29
  • 2019-01-31
  • 2014-09-24
  • 2016-04-15
  • 1970-01-01
相关资源
最近更新 更多