gpt4 book ai didi

python - 为什么在达到 200 万个单词时向 gensim 词典添加文档会变慢?

转载 作者:太空狗 更新时间:2023-10-30 02:53:40 24 4
gpt4 key购买 nike

我注意到,当向 gensim 词典添加文档时,执行时间在达到 200 万个单词时从 0.2 秒跃升至 6 秒以上。

下面的代码是一个简单的例子。我循环遍历 int 并在每次迭代时将数字添加到字典中。

from gensim import corpora
import time



dict_transcript = corpora.Dictionary()


for i in range(1,10000000):

start_time = time.time()

doc = [str(i)]

dict_transcript.add_documents([doc])

print("Iter "+str(i)+" done in " + str(time.time() - start_time) + ' w/ '+str(len(doc)) + ' words and dico size ' +
str(len(dict_transcript)))

当达到 200 万个单词时,我确实得到了以下输出:

Iter 1999999 done in 0.0 w/ 1 words and dico size 1999999
Iter 2000000 done in 0.0 w/ 1 words and dico size 2000000
Iter 2000001 done in 0.0 w/ 1 words and dico size 2000001
Iter 2000002 done in 7.940511226654053 w/ 1 words and dico size 2000001

有什么原因吗?有谁知道如何绕过这个问题?我在一个大型语料库上使用这本词典,我将其标记为双字母组,因此我预计该词典有几百万行。

非常感谢

最佳答案

看看 gensim documentation :

class gensim.corpora.dictionary.Dictionary(documents=None, prune_at=2000000)

prune_at (int, optional) – Total number of unique words. Dictionary will keep not more than prune_at words.

设置 prune_at=None 或适合您的用例的整数。

关于python - 为什么在达到 200 万个单词时向 gensim 词典添加文档会变慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705138/

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