Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/7705#discussion_r35818056
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAModel.scala ---
@@ -197,8 +229,92 @@ class LocalLDAModel private[clustering] (
// TODO:
// override def topicDistributions(documents: RDD[(Long, Vector)]):
RDD[(Long, Vector)] = ???
+ /**
+ * Calculate and return log variational bound on perplexity using the
provided `documents` of
+ * documents as a test corpus.
+ * Perplexity on a test corpus is calculated as:
+ * perplexity(documents) = exp( -log p(documents) / numWords )
+ * where p is the LDA model. It is upper bounded by the variational
distribution using:
+ * perplexity(documents) <= exp( -(E_q[log p(documents] - E_q[log
q(documents)]) / numWords }
+ */
+ def logPerplexity(documents: RDD[(Long, Vector)]): Double = {
+ val numDocs = documents.count()
+ val corpusWords = documents
+ .map { case (_, termCounts) => termCounts.toArray.sum }
+ .sum()
+ val subsampleRatio = numDocs.toDouble / documents.count()
+ val batchVariationalBound = bound(documents, subsampleRatio,
docConcentration,
+ topicConcentration, topicsMatrix.toBreeze.toDenseMatrix, gammaShape,
k, vocabSize)
+ val perWordBound = batchVariationalBound / (subsampleRatio *
corpusWords)
+
+ perWordBound
+ }
+
+
+ /**
+ * Estimate the variational bound of documents from `documents`:
+ * log p(documents) >= E_q[log p(documents)] - E_q[log q(documents)])
--- End diff --
It's OK not to include this here since it's a private method and already
documented in the public method.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]