-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-9245] [MLLIB] LDA topic assignments #8329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| package org.apache.spark.mllib.clustering | ||
|
|
||
| import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV, argtopk, normalize, sum} | ||
| import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV, argmax, argtopk, normalize, sum} | ||
| import breeze.numerics.{exp, lgamma} | ||
| import org.apache.hadoop.fs.Path | ||
| import org.json4s.DefaultFormats | ||
|
|
@@ -412,7 +412,7 @@ object LocalLDAModel extends Loader[LocalLDAModel] { | |
| Loader.checkSchema[Data](dataFrame.schema) | ||
| val topics = dataFrame.collect() | ||
| val vocabSize = topics(0).getAs[Vector](0).size | ||
| val k = topics.size | ||
| val k = topics.length | ||
|
|
||
| val brzTopics = BDM.zeros[Double](vocabSize, k) | ||
| topics.foreach { case Row(vec: Vector, ind: Int) => | ||
|
|
@@ -575,6 +575,48 @@ class DistributedLDAModel private[clustering] ( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Return the top topic for each (doc, term) pair. I.e., for each document, what is the most | ||
| * likely topic generating each term? | ||
| * | ||
| * @return RDD of (doc ID, assignment of top topic index for each term), | ||
| * where the assignment is specified via a pair of zippable arrays | ||
| * (term indices, topic indices). Note that terms will be omitted if not present in | ||
| * the document. | ||
| */ | ||
| lazy val topicAssignments: RDD[(Long, Array[Int], Array[Int])] = { | ||
| // For reference, compare the below code with the core part of EMLDAOptimizer.next(). | ||
| val eta = topicConcentration | ||
| val W = vocabSize | ||
| val alpha = docConcentration(0) | ||
| val N_k = globalTopicTotals | ||
| val sendMsg: EdgeContext[TopicCounts, TokenCount, (Array[Int], Array[Int])] => Unit = | ||
| (edgeContext) => { | ||
| // E-STEP: Compute gamma_{wjk} (smoothed topic distributions). | ||
| val scaledTopicDistribution: TopicCounts = | ||
| computePTopic(edgeContext.srcAttr, edgeContext.dstAttr, N_k, W, eta, alpha) | ||
| // For this (doc j, term w), send top topic k to doc vertex. | ||
| val topTopic: Int = argmax(scaledTopicDistribution) | ||
| val term: Int = index2term(edgeContext.dstId) | ||
| edgeContext.sendToSrc((Array(term), Array(topTopic))) | ||
| } | ||
| val mergeMsg: ((Array[Int], Array[Int]), (Array[Int], Array[Int])) => (Array[Int], Array[Int]) = | ||
| (terms_topics0, terms_topics1) => { | ||
| (terms_topics0._1 ++ terms_topics1._1, terms_topics0._2 ++ terms_topics1._2) | ||
| } | ||
| // M-STEP: Aggregation computes new N_{kj}, N_{wk} counts. | ||
| graph.aggregateMessages[(Array[Int], Array[Int])](sendMsg, mergeMsg).filter(isDocumentVertex) | ||
| .map { case (docID: Long, (terms: Array[Int], topics: Array[Int])) => | ||
| val (sortedTerms, sortedTopics) = terms.zip(topics).sortBy(_._1).unzip | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leave a TODO because |
||
| (docID, sortedTerms.toArray, sortedTopics.toArray) | ||
| } | ||
| } | ||
|
|
||
| /** Java-friendly version of [[topicAssignments]] */ | ||
| lazy val javaTopicAssignments: JavaRDD[(java.lang.Long, Array[Int], Array[Int])] = { | ||
| topicAssignments.asInstanceOf[RDD[(java.lang.Long, Array[Int], Array[Int])]].toJavaRDD() | ||
| } | ||
|
|
||
| // TODO | ||
| // override def logLikelihood(documents: RDD[(Long, Vector)]): Double = ??? | ||
|
|
||
|
|
@@ -797,10 +839,9 @@ object DistributedLDAModel extends Loader[DistributedLDAModel] { | |
| val classNameV1_0 = SaveLoadV1_0.thisClassName | ||
|
|
||
| val model = (loadedClassName, loadedVersion) match { | ||
| case (className, "1.0") if className == classNameV1_0 => { | ||
| case (className, "1.0") if className == classNameV1_0 => | ||
| DistributedLDAModel.SaveLoadV1_0.load(sc, path, vocabSize, docConcentration, | ||
| topicConcentration, iterationTimes.toArray, gammaShape) | ||
| } | ||
| case _ => throw new Exception( | ||
| s"DistributedLDAModel.load did not recognize model with (className, format version):" + | ||
| s"($loadedClassName, $loadedVersion). Supported: ($classNameV1_0, 1.0)") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix indentation