Github user jkbradley commented on a diff in the pull request:
https://github.com/apache/spark/pull/6948#discussion_r34755361
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAModel.scala ---
@@ -184,6 +199,82 @@ class LocalLDAModel private[clustering] (
}
+@Experimental
+object LocalLDAModel extends Loader[LocalLDAModel] {
+
+ private object SaveLoadV1_0 {
+
+ val thisFormatVersion = "1.0"
+
+ val thisClassName = "org.apache.spark.mllib.clustering.LocalLDAModel"
+
+ // Store the distribution of terms of each topic as a Row in data.
+ case class Data(topic: Vector)
+
+ def save(sc: SparkContext, path: String, topicsMatrix: Matrix): Unit =
{
+
+ val sqlContext = new SQLContext(sc)
+ import sqlContext.implicits._
+
+ val k = topicsMatrix.numCols
+ val metadata = compact(render
+ (("class" -> thisClassName) ~ ("version" -> thisFormatVersion) ~
+ ("k" -> k) ~ ("vocabSize" -> topicsMatrix.numRows)))
+ sc.parallelize(Seq(metadata),
1).saveAsTextFile(Loader.metadataPath(path))
+
+ val topicsDenseMatrix = topicsMatrix.toBreeze.toDenseMatrix
+ val topics = Range(0, k).map { topicInd =>
+ Data(Vectors.dense((topicsDenseMatrix(::, topicInd).toArray)))
+ }.toSeq
+ sc.parallelize(topics, 1).toDF().write.parquet(Loader.dataPath(path))
+ }
+
+ def load(sc: SparkContext, path: String): LocalLDAModel = {
+
+ val dataPath = Loader.dataPath(path)
+ val sqlContext = SQLContext.getOrCreate(sc)
+ val dataFrame = sqlContext.read.parquet(dataPath)
+
+ Loader.checkSchema[Data](dataFrame.schema)
+ val topics = dataFrame.collect()
--- End diff --
Uh oh, I just noticed this also assumes collect() returns items in a
particular order. I know that generally works, but I'm not sure if Spark
provides any guarantees. If so, they certainly are not documented anywhere
I've seen. You could either store a single Matrix, or add indices to the Data
structure.
---
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]