首页 > 解决方案 > 在 Gensim 中训练分布式 LdaModel 时出现 AssertionError

问题描述

我一直在尝试在大型数据集(约 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])

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

标签: pythonnlpgensimtopic-modelingpyro4

解决方案


推荐阅读