Skip to content
Prev Previous commit
Next Next commit
fixed unit tests
  • Loading branch information
jkbradley committed Nov 9, 2015
commit 631d40759e88c84dfffe4805a7834c353caa341b
22 changes: 11 additions & 11 deletions mllib/src/main/scala/org/apache/spark/ml/clustering/LDA.scala
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ private[clustering] trait LDAParams extends Params with HasFeaturesCol with HasM
SchemaUtils.checkColumnType(schema, $(featuresCol), new VectorUDT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we prominently document that features have to be Vectors with integer-quantized Double values? A novice user could easily assume that LDA takes text as input. Also, I'm not terribly keen on continuing to use Vectors and implicitly requiring integer tokens for words: Array[Int] using catalyst's ArrayType would be nicer IMO. We should fix this decision before merging since it will affect the public API.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it should be documented, but we can later support Array of Int or even Strings without breaking the API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should provide the Vector API at all if the vector can only contain integer arguments since we really just mean Array[Int]. Making the change now would allow us to drop this legacy API altogether. If we need to merge this quickly, can we make the public API support Array[Int] and do a conversion to vector internally?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bigger issue might be that the relevant feature transformers (CountVectorizer) produce Vectors, so we need to support Vector. It also might be reasonable to have weighted docs (someday), which would change the Int values to Doubles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't think I completely understand how weighted docs makes these Doubles, but good point about CountVectorizer (although I have feelings about that one as well, but it's too late to change that >.<). Let's leave this as Vector then

SchemaUtils.appendColumn(schema, $(topicDistributionCol), new VectorUDT)
}

override def validateParams(): Unit = {
if (getDocConcentration.length != 1) {
require(getDocConcentration.length == getK, s"LDA docConcentration was of length" +
s" ${getDocConcentration.length}, but k = $getK. docConcentration must be either" +
s" length 1 (scalar) or an array of length k.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: either a length 1 vector or a vector of length k

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I forgot to make this update. I'll fix it now.

}
}
}


Expand All @@ -195,14 +203,6 @@ class LDAModel private[ml] (
@Since("1.6.0") @transient protected val sqlContext: SQLContext)
extends Model[LDAModel] with LDAParams with Logging {

override def validateParams(): Unit = {
if (getDocConcentration.length != 1) {
require(getDocConcentration.length == getK, s"LDA docConcentration was of length" +
s" ${getDocConcentration.length}, but k = $getK. docConcentration must be either" +
s" length 1 (scalar) or an array of length k.")
}
}

/** Returns underlying spark.mllib model */
@Since("1.6.0")
protected def getModel: OldLDAModel = oldLocalModel match {
Expand Down Expand Up @@ -328,7 +328,7 @@ class LDAModel private[ml] (
def describeTopics(maxTermsPerTopic: Int): DataFrame = {
val topics = getModel.describeTopics(maxTermsPerTopic).zipWithIndex.map {
case ((termIndices, termWeights), topic) =>
(topic, termIndices, termWeights)
(topic, termIndices.toSeq, termWeights.toSeq)
}
sqlContext.createDataFrame(topics).toDF("topic", "termIndices", "termWeights")
}
Expand Down Expand Up @@ -463,8 +463,8 @@ class LDA @Since("1.6.0") (
@Since("1.6.0")
def this() = this(Identifiable.randomUID("lda"))

setDefault(k -> 10, docConcentration -> Array(-1.0), topicConcentration -> -1.0,
optimizer -> new OnlineLDAOptimizer)
setDefault(maxIter -> 20, k -> 10, docConcentration -> Array(-1.0), topicConcentration -> -1.0,
optimizer -> new OnlineLDAOptimizer, checkpointInterval -> 10)

/**
* The features for LDA should be a [[Vector]] representing the word counts in a document.
Expand Down
39 changes: 18 additions & 21 deletions mllib/src/test/scala/org/apache/spark/ml/clustering/LDASuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ object LDASuite {
def generateLDAData(
sql: SQLContext,
rows: Int,
dim: Int,
k: Int,
vocabSize: Int): DataFrame = {
val avgWC = 1 // average instances of each word in a doc
val sc = sql.sparkContext
val rng = new java.util.Random()
rng.setSeed(1)
val rdd = sc.parallelize(1 to rows).map { i =>
Vectors.dense(Array.fill(dim)(rng.nextInt(vocabSize).toDouble))
Vectors.dense(Array.fill(vocabSize)(rng.nextInt(2 * avgWC).toDouble))
}.map(v => new TestRow(v))
sql.createDataFrame(rdd)
}
Expand All @@ -44,16 +44,13 @@ object LDASuite {

class LDASuite extends SparkFunSuite with MLlibTestSparkContext {

val k = 5
val k: Int = 5
val vocabSize: Int = 30
@transient var dataset: DataFrame = _
@transient var vocabSize: Int = _

override def beforeAll(): Unit = {
super.beforeAll()

dataset = LDASuite.generateLDAData(sqlContext, 50, 3, k, 30)
vocabSize = dataset.flatMap(_.getAs[Vector](0).toArray.map(_.toInt).toSet)
.distinct().count().toInt
dataset = LDASuite.generateLDAData(sqlContext, 50, k, vocabSize)
}

test("default parameters") {
Expand All @@ -62,7 +59,7 @@ class LDASuite extends SparkFunSuite with MLlibTestSparkContext {
assert(lda.getFeaturesCol === "features")
assert(lda.getMaxIter === 20)
assert(lda.isDefined(lda.seed))
assert(!lda.isDefined(lda.checkpointInterval))
assert(lda.getCheckpointInterval === 10)
assert(lda.getK === 10)
assert(lda.getDocConcentration === Array(-1.0))
assert(lda.getTopicConcentration === -1.0)
Expand Down Expand Up @@ -129,9 +126,9 @@ class LDASuite extends SparkFunSuite with MLlibTestSparkContext {

// validateParams()
lda.setDocConcentration(-1)
assert(lda.getDocConcentration === -1)
assert(lda.getDocConcentration === Array(-1.0))
lda.validateParams()
lda.setDocConcentration(0.1)
lda.setDocConcentration(1.1)
lda.validateParams()
lda.setDocConcentration(Range(0, lda.getK).map(_ + 2.0).toArray)
lda.validateParams()
Expand Down Expand Up @@ -178,7 +175,7 @@ class LDASuite extends SparkFunSuite with MLlibTestSparkContext {
}
transformed.select(lda.getTopicDistributionCol).collect().foreach { r =>
val topicDistribution = r.getAs[Vector](0)
assert(topicDistribution.size === vocabSize)
assert(topicDistribution.size === k)
assert(topicDistribution.toArray.forall(w => w >= 0.0 && w <= 1.0))
}

Expand All @@ -192,14 +189,14 @@ class LDASuite extends SparkFunSuite with MLlibTestSparkContext {
val topics = model.describeTopics(3)
assert(topics.count() === k)
assert(topics.select("topic").map(_.getInt(0)).collect().toSet === Range(0, k).toSet)
assert(topics.select("termIndices").collect().forall { case r: Row =>
val termIndices = r.getAs[Array[Int]](0)
termIndices.length === 3 && termIndices.toSet.size === 3
})
assert(topics.select("termWeights").collect().forall { case r: Row =>
val termWeights = r.getAs[Array[Double]](0)
termWeights.length === 3 && termWeights.forall(w => w >= 0.0 && w <= 1.0)
})
topics.select("termIndices").collect().foreach { case r: Row =>
val termIndices = r.getAs[Seq[Int]](0)
assert(termIndices.length === 3 && termIndices.toSet.size === 3)
}
topics.select("termWeights").collect().foreach { case r: Row =>
val termWeights = r.getAs[Seq[Double]](0)
assert(termWeights.length === 3 && termWeights.forall(w => w >= 0.0 && w <= 1.0))
}
}

test("fit & transform with EM LDA") {
Expand All @@ -223,6 +220,6 @@ class LDASuite extends SparkFunSuite with MLlibTestSparkContext {
val ll = model.trainingLogLikelihood
assert(ll <= 0.0 && ll != Double.NegativeInfinity)
val lp = model.logPrior
assert(lp >= 0.0 && lp != Double.PositiveInfinity)
assert(lp <= 0.0 && lp != Double.NegativeInfinity)
}
}