Skip to content

Commit 125205c

Browse files
Feynman Liangjkbradley
authored andcommitted
[SPARK-9888] [MLLIB] User guide for new LDA features
* Adds two new sections to LDA's user guide; one for each optimizer/model * Documents new features added to LDA (e.g. topXXXperXXX, asymmetric priors, hyperpam optimization) * Cleans up a TODO and sets a default parameter in LDA code jkbradley hhbyyh Author: Feynman Liang <[email protected]> Closes #8254 from feynmanliang/SPARK-9888.
1 parent 7467b52 commit 125205c

File tree

3 files changed

+117
-20
lines changed

3 files changed

+117
-20
lines changed

docs/mllib-clustering.md

Lines changed: 116 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -438,28 +438,125 @@ sameModel = PowerIterationClusteringModel.load(sc, "myModelPath")
438438
is a topic model which infers topics from a collection of text documents.
439439
LDA can be thought of as a clustering algorithm as follows:
440440

441-
* Topics correspond to cluster centers, and documents correspond to examples (rows) in a dataset.
442-
* Topics and documents both exist in a feature space, where feature vectors are vectors of word counts.
443-
* Rather than estimating a clustering using a traditional distance, LDA uses a function based
444-
on a statistical model of how text documents are generated.
445-
446-
LDA takes in a collection of documents as vectors of word counts.
447-
It supports different inference algorithms via `setOptimizer` function. EMLDAOptimizer learns clustering using [expectation-maximization](http://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm)
448-
on the likelihood function and yields comprehensive results, while OnlineLDAOptimizer uses iterative mini-batch sampling for [online variational inference](https://www.cs.princeton.edu/~blei/papers/HoffmanBleiBach2010b.pdf) and is generally memory friendly. After fitting on the documents, LDA provides:
449-
450-
* Topics: Inferred topics, each of which is a probability distribution over terms (words).
451-
* Topic distributions for documents: For each non empty document in the training set, LDA gives a probability distribution over topics. (EM only). Note that for empty documents, we don't create the topic distributions. (EM only)
441+
* Topics correspond to cluster centers, and documents correspond to
442+
examples (rows) in a dataset.
443+
* Topics and documents both exist in a feature space, where feature
444+
vectors are vectors of word counts (bag of words).
445+
* Rather than estimating a clustering using a traditional distance, LDA
446+
uses a function based on a statistical model of how text documents are
447+
generated.
448+
449+
LDA supports different inference algorithms via `setOptimizer` function.
450+
`EMLDAOptimizer` learns clustering using
451+
[expectation-maximization](http://en.wikipedia.org/wiki/Expectation%E2%80%93maximization_algorithm)
452+
on the likelihood function and yields comprehensive results, while
453+
`OnlineLDAOptimizer` uses iterative mini-batch sampling for [online
454+
variational
455+
inference](https://www.cs.princeton.edu/~blei/papers/HoffmanBleiBach2010b.pdf)
456+
and is generally memory friendly.
452457

453-
LDA takes the following parameters:
458+
LDA takes in a collection of documents as vectors of word counts and the
459+
following parameters (set using the builder pattern):
454460

455461
* `k`: Number of topics (i.e., cluster centers)
456-
* `maxIterations`: Limit on the number of iterations of EM used for learning
457-
* `docConcentration`: Hyperparameter for prior over documents' distributions over topics. Currently must be > 1, where larger values encourage smoother inferred distributions.
458-
* `topicConcentration`: Hyperparameter for prior over topics' distributions over terms (words). Currently must be > 1, where larger values encourage smoother inferred distributions.
459-
* `checkpointInterval`: If using checkpointing (set in the Spark configuration), this parameter specifies the frequency with which checkpoints will be created. If `maxIterations` is large, using checkpointing can help reduce shuffle file sizes on disk and help with failure recovery.
460-
461-
*Note*: LDA is a new feature with some missing functionality. In particular, it does not yet
462-
support prediction on new documents, and it does not have a Python API. These will be added in the future.
462+
* `optimizer`: Optimizer to use for learning the LDA model, either
463+
`EMLDAOptimizer` or `OnlineLDAOptimizer`
464+
* `docConcentration`: Dirichlet parameter for prior over documents'
465+
distributions over topics. Larger values encourage smoother inferred
466+
distributions.
467+
* `topicConcentration`: Dirichlet parameter for prior over topics'
468+
distributions over terms (words). Larger values encourage smoother
469+
inferred distributions.
470+
* `maxIterations`: Limit on the number of iterations.
471+
* `checkpointInterval`: If using checkpointing (set in the Spark
472+
configuration), this parameter specifies the frequency with which
473+
checkpoints will be created. If `maxIterations` is large, using
474+
checkpointing can help reduce shuffle file sizes on disk and help with
475+
failure recovery.
476+
477+
478+
All of MLlib's LDA models support:
479+
480+
* `describeTopics`: Returns topics as arrays of most important terms and
481+
term weights
482+
* `topicsMatrix`: Returns a `vocabSize` by `k` matrix where each column
483+
is a topic
484+
485+
*Note*: LDA is still an experimental feature under active development.
486+
As a result, certain features are only available in one of the two
487+
optimizers / models generated by the optimizer. Currently, a distributed
488+
model can be converted into a local model, but not vice-versa.
489+
490+
The following discussion will describe each optimizer/model pair
491+
separately.
492+
493+
**Expectation Maximization**
494+
495+
Implemented in
496+
[`EMLDAOptimizer`](api/scala/index.html#org.apache.spark.mllib.clustering.EMLDAOptimizer)
497+
and
498+
[`DistributedLDAModel`](api/scala/index.html#org.apache.spark.mllib.clustering.DistributedLDAModel).
499+
500+
For the parameters provided to `LDA`:
501+
502+
* `docConcentration`: Only symmetric priors are supported, so all values
503+
in the provided `k`-dimensional vector must be identical. All values
504+
must also be $> 1.0$. Providing `Vector(-1)` results in default behavior
505+
(uniform `k` dimensional vector with value $(50 / k) + 1$
506+
* `topicConcentration`: Only symmetric priors supported. Values must be
507+
$> 1.0$. Providing `-1` results in defaulting to a value of $0.1 + 1$.
508+
* `maxIterations`: The maximum number of EM iterations.
509+
510+
`EMLDAOptimizer` produces a `DistributedLDAModel`, which stores not only
511+
the inferred topics but also the full training corpus and topic
512+
distributions for each document in the training corpus. A
513+
`DistributedLDAModel` supports:
514+
515+
* `topTopicsPerDocument`: The top topics and their weights for
516+
each document in the training corpus
517+
* `topDocumentsPerTopic`: The top documents for each topic and
518+
the corresponding weight of the topic in the documents.
519+
* `logPrior`: log probability of the estimated topics and
520+
document-topic distributions given the hyperparameters
521+
`docConcentration` and `topicConcentration`
522+
* `logLikelihood`: log likelihood of the training corpus, given the
523+
inferred topics and document-topic distributions
524+
525+
**Online Variational Bayes**
526+
527+
Implemented in
528+
[`OnlineLDAOptimizer`](api/scala/org/apache/spark/mllib/clustering/OnlineLDAOptimizer.html)
529+
and
530+
[`LocalLDAModel`](api/scala/org/apache/spark/mllib/clustering/LocalLDAModel.html).
531+
532+
For the parameters provided to `LDA`:
533+
534+
* `docConcentration`: Asymmetric priors can be used by passing in a
535+
vector with values equal to the Dirichlet parameter in each of the `k`
536+
dimensions. Values should be $>= 0$. Providing `Vector(-1)` results in
537+
default behavior (uniform `k` dimensional vector with value $(1.0 / k)$)
538+
* `topicConcentration`: Only symmetric priors supported. Values must be
539+
$>= 0$. Providing `-1` results in defaulting to a value of $(1.0 / k)$.
540+
* `maxIterations`: Maximum number of minibatches to submit.
541+
542+
In addition, `OnlineLDAOptimizer` accepts the following parameters:
543+
544+
* `miniBatchFraction`: Fraction of corpus sampled and used at each
545+
iteration
546+
* `optimizeDocConcentration`: If set to true, performs maximum-likelihood
547+
estimation of the hyperparameter `docConcentration` (aka `alpha`)
548+
after each minibatch and sets the optimized `docConcentration` in the
549+
returned `LocalLDAModel`
550+
* `tau0` and `kappa`: Used for learning-rate decay, which is computed by
551+
$(\tau_0 + iter)^{-\kappa}$ where $iter$ is the current number of iterations.
552+
553+
`OnlineLDAOptimizer` produces a `LocalLDAModel`, which only stores the
554+
inferred topics. A `LocalLDAModel` supports:
555+
556+
* `logLikelihood(documents)`: Calculates a lower bound on the provided
557+
`documents` given the inferred topics.
558+
* `logPerplexity(documents)`: Calculates an upper bound on the
559+
perplexity of the provided `documents` given the inferred topics.
463560

464561
**Examples**
465562

mllib/src/main/scala/org/apache/spark/mllib/clustering/LDAModel.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,6 @@ object LocalLDAModel extends Loader[LocalLDAModel] {
435435
}
436436
val topicsMat = Matrices.fromBreeze(brzTopics)
437437

438-
// TODO: initialize with docConcentration, topicConcentration, and gammaShape after SPARK-9940
439438
new LocalLDAModel(topicsMat, docConcentration, topicConcentration, gammaShape)
440439
}
441440
}

mllib/src/test/scala/org/apache/spark/mllib/clustering/LDASuite.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class LDASuite extends SparkFunSuite with MLlibTestSparkContext {
6868
// Train a model
6969
val lda = new LDA()
7070
lda.setK(k)
71+
.setOptimizer(new EMLDAOptimizer)
7172
.setDocConcentration(topicSmoothing)
7273
.setTopicConcentration(termSmoothing)
7374
.setMaxIterations(5)

0 commit comments

Comments
 (0)