def perplexity(self):
log_likelihood = 0.
for doc_ix in xrange(self.num_docs):
prob_topic_given_document = np.zeros(self.K)
for i in xrange(self.K):
prob_topic_given_document[i] = (self.ndt[doc_ix][i] + self.alpha) / (
self.nd[doc_ix] + self.K * self.alpha)
for word in self.corpus[doc_ix]:
prob_word_given_topic = np.zeros(self.K)
word_ix = self.word2ix[word]
for j in xrange(self.K):
prob_word_given_topic[j] = (self.ntw[j][word_ix] + self.beta) / (self.nt[j] + self.K * self.alpha)
prob_word = 0.
for j in xrange(self.K):
prob_word += prob_topic_given_document[j] * prob_word_given_topic[j]
log_likelihood += np.log(prob_word)
perplexity = np.exp2(-log_likelihood / self.num_docs)
return perplexity
评论列表
文章目录