@@ -531,8 +531,8 @@ def fit(self, X, y=None):
531
531
doc_topics_distr , _ = self ._e_step (X , cal_sstats = False ,
532
532
random_init = False ,
533
533
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 )
536
536
if self .verbose :
537
537
print ('iteration: %d, perplexity: %.4f'
538
538
% (i + 1 , bound ))
@@ -690,8 +690,10 @@ def score(self, X, y=None):
690
690
score = self ._approx_bo
8000
und (X , doc_topic_distr , sub_sampling = False )
691
691
return score
692
692
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
695
697
696
698
Perplexity is defined as exp(-1. * log-likelihood per word)
697
699
@@ -737,3 +739,21 @@ def perplexity(self, X, doc_topic_distr=None, sub_sampling=False):
737
739
perword_bound = bound / word_cnt
738
740
739
741
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