在 Gensim 中训练分布式 LdaModel 时出现断言错误

我一直在尝试在大型数据集(约 1700 万个条目)上使用 LdaModel() 拟合主题模型。我正在大学的 HPC 集群上运行我的代码,并且在尝试以分布式模式(分布式 = True)训练模型时遇到了问题。现在我只是尝试在一小部分数据上运行 4 个内核,但是当我运行整个数据集时可能需要增加内核数。这是错误:

https://i.stack.imgur.com/xiaTV.png

当我以串行模式(分布式 = False)运行时,它似乎工作正常。在分布式运行时,似乎 gensim 正在尝试使用所有 4 个内核进行训练。回调日志产生了以下语句:“2021-04-26 18:32:05,757:INFO:using Distributed version with 4 workers”。我使用的是最新版本的 Gensim v4.0 和 Pyro4 v4.8 以及 Python v3.7.7。我也尝试将 gensim 降级到 3.8.3,但这似乎没有帮助。

另外值得注意的是,我正在从 shell 脚本运行 python 脚本,其中包含了 gensim 文档建议的以下代码行:

export PYRO_SERIALIZERS_accEPTED=pickle
export PYRO_SERIALIZER=pickle  
python -m Pyro4.naming -n 0.0.0.0 &

for i in {1..4}
do
    python -m gensim.models.lda_worker &
    echo "$i"
done
python -m gensim.models.lda_dispatcher &

这是我尝试运行的大部分 python 脚本:

# Import preprocessed text
df = pd.read_feather('narratives_processed_1.feather')
# Sample down to speed up debugging
df = df.loc[1:4000]

# Create dictionary from text
dictionary = Dictionary(df.FOI_TEXT)
dictionary.filter_extremes(no_below=100,no_above=0.5)
dictionary.compactify()
# Create corpus
corpus = [dictionary.doc2bow(doc) for doc in df.FOI_TEXT]
print('Number of unique tokens: %d' % len(dictionary))
print('Number of documents: %d' % len(corpus))
temp = dictionary[0]
id2word = dictionary.id2token

#Callback logging for coherence using the u_mass metric
coherence_umass_logger = CoherenceMetric(corpus=corpus,logger='shell',coherence = 'u_mass')

filename = "model_callbacks_1.log"
import logging
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)
logging.basicConfig(filename = filename,format="%(asctime)s:%(levelname)s:%(message)s",level=logging.INFO)

#Iterate over model parameters
iterations = [10,50,100]
num_topics = [2,3]
passes = 2

all_metrics = pd.DataFrame()

print('Fitting models...')
for iteration in iterations:
    print('Iterations for this model: %d'%(iteration))
    for num_topic in num_topics:
        print('Topics for this model: %d'%(num_topic))
        
        # Create model
        model = LdaModel(corpus=corpus,num_topics=num_topic,id2word=id2word,eval_every=0,passes=passes,iterations=iteration,chunksize=1000,random_state=100,callbacks=[coherence_umass_logger],distributed = True)
            
        df_temp = pd.DataFrame.from_dict(model.metrics)
        df_temp['iterations'] = iteration
        df_temp['topics'] = num_topic
    
        all_metrics = pd.concat([all_metrics,df_temp])

非常感谢您对此错误的任何帮助!

lang19999 回答:在 Gensim 中训练分布式 LdaModel 时出现断言错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/516421.html

大家都在问