Github user etrain commented on a diff in the pull request:

    https://github.com/apache/spark/pull/476#discussion_r12126247
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/expectation/GibbsSampling.scala ---
    @@ -0,0 +1,219 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.mllib.expectation
    +
    +import java.util.Random
    +
    +import breeze.linalg.{DenseVector => BDV, sum}
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.mllib.clustering.{Document, LDAParams}
    +import org.apache.spark.mllib.linalg.{Vector, Vectors}
    +
    +/**
    + * Gibbs sampling from a given dataset and org.apache.spark.mllib.model.
    + * @param data Dataset, such as corpus.
    + * @param numOuterIterations Number of outer iteration.
    + * @param numInnerIterations Number of inner iteration, used in each 
partition.
    + * @param docTopicSmoothing Document-topic smoothing.
    + * @param topicTermSmoothing Topic-term smoothing.
    + */
    +class GibbsSampling(
    +    data: RDD[Document],
    +    numOuterIterations: Int,
    +    numInnerIterations: Int,
    +    docTopicSmoothing: Double,
    +    topicTermSmoothing: Double)
    +  extends Logging with Serializable {
    +
    +  import GibbsSampling._
    +
    +  /**
    +   * Main function of running a Gibbs sampling method. It contains two 
phases of total Gibbs
    +   * sampling: first is initialization, second is real sampling.
    +   */
    +  def runGibbsSampling(
    +      initParams: LDAParams,
    +      data: RDD[Document] = data,
    +      numOuterIterations: Int = numOuterIterations,
    +      numInnerIterations: Int = numInnerIterations,
    +      docTopicSmoothing: Double = docTopicSmoothing,
    +      topicTermSmoothing: Double = topicTermSmoothing): LDAParams = {
    +
    +    val numTerms = initParams.topicTermCounts.head.size
    +    val numDocs = initParams.docCounts.size
    +    val numTopics = initParams.topicCounts.size
    +
    +    // Construct topic assignment RDD
    +    logInfo("Start initialization")
    +
    +    val cpInterval = 
System.getProperty("spark.gibbsSampling.checkPointInterval", "10").toInt
    +    val sc = data.context
    +    val (initialParams, initialChosenTopics) = 
sampleTermAssignment(initParams, data)
    +
    +    // Gibbs sampling
    +    val (params, _, _) = Iterator.iterate((sc.accumulable(initialParams), 
initialChosenTopics, 0)) {
    +      case (lastParams, lastChosenTopics, i) =>
    +        logInfo("Start Gibbs sampling")
    +
    +        val rand = new Random(42 + i * i)
    +        val params = sc.accumulable(LDAParams(numDocs, numTopics, 
numTerms))
    +        val chosenTopics = data.zip(lastChosenTopics).map {
    +          case (Document(docId, content), topics) =>
    +            content.zip(topics).map { case (term, topic) =>
    +              lastParams += (docId, term, topic, -1)
    +
    +              val chosenTopic = lastParams.localValue.dropOneDistSampler(
    +                docTopicSmoothing, topicTermSmoothing, term, docId, rand)
    +
    +              lastParams += (docId, term, chosenTopic, 1)
    +              params += (docId, term, chosenTopic, 1)
    +
    +              chosenTopic
    +            }
    +        }.cache()
    +
    +        if (i + 1 % cpInterval == 0) {
    +          chosenTopics.checkpoint()
    +        }
    +
    +        // Trigger a job to collect accumulable LDA parameters.
    +        chosenTopics.count()
    +        lastChosenTopics.unpersist()
    +
    +        (params, chosenTopics, i + 1)
    +    }.drop(1 + numOuterIterations).next()
    +
    +    params.value
    +  }
    +
    +  /**
    +   * Model matrix Phi and Theta are inferred via LDAParams.
    +   */
    +  def solvePhiAndTheta(
    +      params: LDAParams,
    +      docTopicSmoothing: Double = docTopicSmoothing,
    +      topicTermSmoothing: Double = topicTermSmoothing): (Array[Vector], 
Array[Vector]) = {
    --- End diff --
    
    Again, Phi and Theta might be too big.


---
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.
---

Reply via email to