简体   繁体   中英

Determining log_perplexity using ldamulticore for optimum number of topics

I am trying to determine the optimum number of topics for my LDA model using log perplexity in python. That is, I am graphing the log perplexity for a range of topics and determining the minimum perplexity. However, the graph I have obtained has negative values for log perplexity, when it should have positive values between 0 and 1.

#calculating the log perplexity per word as obtained by gensim code 
##https://radimrehurek.com/gensim/models/atmodel.html
#parameters: pass in trained corpus
#return: graph of perplexity per word for varying number of topics
parameter_list = range(1, 500, 100)
grid ={}

for parameter_value in parameter_list:
model = models.LdaMulticore(corpus=corpus, workers=None, id2word=None, 
                            num_topics=parameter_value, iterations=10)
grid[parameter_value]=[]

perplex=model.log_perplexity(corpus, total_docs=len(corpus))
grid[parameter_value].append(perplex)


df = pd.DataFrame(grid)
ax = plt.figure(figsize=(7, 4), dpi=300).add_subplot(111)
df.iloc[0].transpose().plot(ax=ax,  color="#254F09")
plt.xlim(parameter_list[0], parameter_list[-1])
plt.ylabel('Perplexity')
plt.xlabel('topics')
plt.show()     

The perplexity must be between 0 and 1. What you are computing is the log -perplexity. It is negative because the logarithm of a number in the (0,1) range is below zero.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM