@@ -531,8 +531,8 @@ def fit(self, X, y=None):
531531 doc_topics_distr , _ = self ._e_step (X , cal_sstats = False ,
532532 random_init = False ,
533533 parallel = parallel )
534- bound = self .perplexity (X , doc_topics_distr ,
535- sub_sampling = False )
534+ bound = self ._perplexity_precomp_distr (X , doc_topics_distr ,
535+ sub_sampling = False )
536536 if self .verbose :
537537 print ('iteration: %d, perplexity: %.4f'
538538 % (i + 1 , bound ))
@@ -690,8 +690,10 @@ def score(self, X, y=None):
690690 score = self ._approx_bound (X , doc_topic_distr , sub_sampling = False )
691691 return score
692692
693- def perplexity (self , X , doc_topic_distr = None , sub_sampling = False ):
694- """Calculate approximate perplexity for data X.
693+ def _perplexity_precomp_distr (self , X , doc_topic_distr = None ,
694+ sub_sampling = False ):
695+ """Calculate approximate perplexity for data X with ability to accept
696+ precomputed doc_topic_distr
695697
696698 Perplexity is defined as exp(-1. * log-likelihood per word)
697699
@@ -737,3 +739,21 @@ def perplexity(self, X, doc_topic_distr=None, sub_sampling=False):
737739 perword_bound = bound / word_cnt
738740
739741 return np .exp (- 1.0 * perword_bound )
742+
743+ def perplexity (self , X , sub_sampling = False ):
744+ """Calculate approximate perplexity for data X.
745+
746+ Perplexity is defined as exp(-1. * log-likelihood per word)
747+
748+ Parameters
749+ ----------
750+ X : array-like or sparse matrix, [n_samples, n_features]
751+ Document word matrix.
752+
753+ Returns
754+ -------
755+ score : float
756+ Perplexity score.
757+ """
758+
759+ return self ._perplexity_precomp_distr (X , sub_sampling = sub_sampling )
0 commit comments