使用 gensim.utils.simple_preprocess Python 中的内存错误
Memory Error in Python using gensim.utils.simple_preprocess
我是 gensim word2vec 的初学者,在准备训练模型的文本时遇到内存错误。我正在使用 Python 3.8.8。我在 12 个不同的文件夹中有大约 900,000 个文本文件。我在想我应该通过 gensim.utils.simple_preprocess 发送所有文本文档,然后我就会得到模型的列表列表。在浏览了大约 150,000 个文档后,我收到了一个内存错误:
Traceback (most recent call last):
File "word2vec_part1.py", line 58, in <module>
documents = list(read_input(paths))
File "word2vec_part1.py", line 39, in read_input
myfile = infile.read()
MemoryError
有没有办法解决这个内存问题?我在下面包含了我正在使用的代码。我是 Python、word2vec 和 Whosebug 的新手,所以如果我的问题措辞不当或者这是一个愚蠢的问题,我深表歉意!感谢您的宝贵时间!
# imports and logging
import gensim
import logging
import os
import os.path
import glob
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
# define function
def read_input(inputs):
# logging info
logging.info("reading files")
# set working directories and load files into a list
for path in inputs:
os.chdir(path)
read_files=glob.glob("*.txt")
# preprocess and counting
for i, file in enumerate(read_files):
if(i%10000==0):
logging.info("read {0} reviews".format(i))
# preprocessing and return a list of words
with open(file, "rb") as infile:
myfile = infile.read()
yield gensim.utils.simple_preprocess(myfile)
# create a list of all file paths
paths = [#here is a list of file paths]
# call function
documents = list(read_input(paths))
logging.info("done reading files!!")
print(len(documents))
print(documents[1])
# training word2vec model
model = gensim.models.Word2Vec (documents, size=150, window=10, min_count=2)
model.train(documents,total_examples=len(documents),epochs=10)
model.save("word2vec.model")
# look up top 6 words similar to 'law'
w1 = ["law"]
model.wv.most_similar (positive=w1,topn=6)
logging.info("done!!!")
似乎试图将所有文档保存在 list
内存中需要比您的系统更多的 RAM。 (也许还有:其中一个文件很大。最大的单个文件是多少?)
没有必要将所有文档都保存在内存中。 Gensim 的 Word2Vec
(以及其他算法)几乎总是可以接受任何 Python iterable 对象 - 一个可以逐一迭代其内容的对象,重复,甚至如果他们来自其他后端。这通常使用更少的 RAM。
Gensim 项目的负责人有一个有用的 post 关于可迭代的方法,可以帮助您将 read_input()
函数调整为一个包装器 class,可以重复地重新迭代文件:
https://rare-technologies.com/data-streaming-in-python-generators-iterators-iterables/
另外两个注意事项:
(1) simple_preprocess()
并不是特别复杂,您可能希望自己进行标记化;但是:
(2) 如果您在每次迭代时都重新标记化,特别是如果您的标记化做了任何复杂的事情或使用了正则表达式,那么您正在对相同的文本进行大量冗余的重新标记化,这很可能成为你训练的瓶颈。所以实际上你可能只想使用你的 read_input()
不要将所有 rokenized 文档填充到列表中,而是将它们写入一个新文件,post-tokenization,一个文档到一行和所有标记由单个空格分隔。然后,像 Gensim 的 LineSentence
这样的实用程序 class 可以提供可迭代包装器,用于将几乎完全准备好的文件提供给 Word2Vec
.
我是 gensim word2vec 的初学者,在准备训练模型的文本时遇到内存错误。我正在使用 Python 3.8.8。我在 12 个不同的文件夹中有大约 900,000 个文本文件。我在想我应该通过 gensim.utils.simple_preprocess 发送所有文本文档,然后我就会得到模型的列表列表。在浏览了大约 150,000 个文档后,我收到了一个内存错误:
Traceback (most recent call last):
File "word2vec_part1.py", line 58, in <module>
documents = list(read_input(paths))
File "word2vec_part1.py", line 39, in read_input
myfile = infile.read()
MemoryError
有没有办法解决这个内存问题?我在下面包含了我正在使用的代码。我是 Python、word2vec 和 Whosebug 的新手,所以如果我的问题措辞不当或者这是一个愚蠢的问题,我深表歉意!感谢您的宝贵时间!
# imports and logging
import gensim
import logging
import os
import os.path
import glob
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
# define function
def read_input(inputs):
# logging info
logging.info("reading files")
# set working directories and load files into a list
for path in inputs:
os.chdir(path)
read_files=glob.glob("*.txt")
# preprocess and counting
for i, file in enumerate(read_files):
if(i%10000==0):
logging.info("read {0} reviews".format(i))
# preprocessing and return a list of words
with open(file, "rb") as infile:
myfile = infile.read()
yield gensim.utils.simple_preprocess(myfile)
# create a list of all file paths
paths = [#here is a list of file paths]
# call function
documents = list(read_input(paths))
logging.info("done reading files!!")
print(len(documents))
print(documents[1])
# training word2vec model
model = gensim.models.Word2Vec (documents, size=150, window=10, min_count=2)
model.train(documents,total_examples=len(documents),epochs=10)
model.save("word2vec.model")
# look up top 6 words similar to 'law'
w1 = ["law"]
model.wv.most_similar (positive=w1,topn=6)
logging.info("done!!!")
似乎试图将所有文档保存在 list
内存中需要比您的系统更多的 RAM。 (也许还有:其中一个文件很大。最大的单个文件是多少?)
没有必要将所有文档都保存在内存中。 Gensim 的 Word2Vec
(以及其他算法)几乎总是可以接受任何 Python iterable 对象 - 一个可以逐一迭代其内容的对象,重复,甚至如果他们来自其他后端。这通常使用更少的 RAM。
Gensim 项目的负责人有一个有用的 post 关于可迭代的方法,可以帮助您将 read_input()
函数调整为一个包装器 class,可以重复地重新迭代文件:
https://rare-technologies.com/data-streaming-in-python-generators-iterators-iterables/
另外两个注意事项:
(1) simple_preprocess()
并不是特别复杂,您可能希望自己进行标记化;但是:
(2) 如果您在每次迭代时都重新标记化,特别是如果您的标记化做了任何复杂的事情或使用了正则表达式,那么您正在对相同的文本进行大量冗余的重新标记化,这很可能成为你训练的瓶颈。所以实际上你可能只想使用你的 read_input()
不要将所有 rokenized 文档填充到列表中,而是将它们写入一个新文件,post-tokenization,一个文档到一行和所有标记由单个空格分隔。然后,像 Gensim 的 LineSentence
这样的实用程序 class 可以提供可迭代包装器,用于将几乎完全准备好的文件提供给 Word2Vec
.