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

    https://github.com/apache/spark/pull/4254#discussion_r23807925
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/clustering/PowerIterationClustering.scala
 ---
    @@ -0,0 +1,220 @@
    +/*
    + * 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.clustering
    +
    +import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV}
    +import org.apache.log4j.Logger
    +import org.apache.spark.SparkContext
    +import org.apache.spark.graphx._
    +import org.apache.spark.mllib.linalg.{Vector, Vectors}
    +import org.apache.spark.rdd.RDD
    +
    +import scala.language.existentials
    +
    +/**
    + * Implements the scalable graph clustering algorithm Power Iteration 
Clustering (see
    + * www.icml2010.org/papers/387.pdf).  From the abstract:
    + *
    + * The input data is first transformed to a normalized Affinity Matrix via 
Gaussian pairwise
    + * distance calculations. Power iteration is then used to find a 
dimensionality-reduced
    + * representation.  The resulting pseudo-eigenvector provides effective 
clustering - as
    + * performed by Parallel KMeans.
    + */
    +object PowerIterationClustering {
    +
    +  private val logger = Logger.getLogger(getClass.getName())
    +
    +  type LabeledPoint = (VertexId, BDV[Double])
    +  type Points = Seq[LabeledPoint]
    +  type DGraph = Graph[Double, Double]
    +  type IndexedVector[Double] = (Long, BDV[Double])
    +
    +  // Terminate iteration when norm changes by less than this value
    +  val defaultMinNormChange: Double = 1e-11
    +
    +  // Default number of iterations for PIC loop
    +  val defaultIterations: Int = 20
    +
    +  // Do not allow divide by zero: change to this value instead
    +  val defaultDivideByZeroVal: Double = 1e-15
    +
    +  // Default number of runs by the KMeans.run() method
    +  val defaultKMeansRuns = 10
    +
    +  /**
    +   *
    +   * Run a Power Iteration Clustering
    +   *
    +   * @param sc  Spark Context
    +   * @param G   Affinity Matrix in a Sparse Graph structure
    +   * @param nClusters  Number of clusters to create
    +   * @param nIterations Number of iterations of the PIC algorithm
    +   *                    that calculates primary PseudoEigenvector and 
Eigenvalue
    +   * @param nRuns  Number of runs for the KMeans clustering
    +   * @return Tuple of (Seq[(Cluster Id,Cluster Center)],
    +   *         Seq[(VertexId, ClusterID Membership)]
    +   */
    +  def run(sc: SparkContext,
    +          G: Graph[Double, Double],
    +          nClusters: Int,
    +          nIterations: Int = defaultIterations,
    +          nRuns: Int = defaultKMeansRuns)
    +  : (Seq[(Int, Vector)], Seq[((VertexId, Vector), Int)]) = {
    +    val (gUpdated, lambda, vt) = getPrincipalEigen(sc, G, nIterations)
    +    // TODO: avoid local collect and then sc.parallelize.
    +    val localVt = vt.collect.sortBy(_._1)
    +    val vectRdd = sc.parallelize(localVt.map(v => (v._1, 
Vectors.dense(v._2))))
    +    vectRdd.cache()
    +    val model = KMeans.train(vectRdd.map {
    +      _._2
    +    }, nClusters, nRuns)
    +    vectRdd.unpersist()
    +    if (logger.isDebugEnabled) {
    +      logger.debug(s"Eigenvalue = $lambda EigenVector: 
${localVt.mkString(",")}")
    +    }
    +    val estimates = vectRdd.zip(model.predict(vectRdd.map(_._2)))
    +    if (logger.isDebugEnabled) {
    +      logger.debug(s"lambda=$lambda  eigen=${localVt.mkString(",")}")
    +    }
    +    val ccs = (0 until 
model.clusterCenters.length).zip(model.clusterCenters)
    +    if (logger.isDebugEnabled) {
    +      logger.debug(s"Kmeans model cluster centers: ${ccs.mkString(",")}")
    +    }
    +    val estCollected = estimates.collect.sortBy(_._1._1)
    +    if (logger.isDebugEnabled) {
    +      val clusters = estCollected.map(_._2)
    +      val counts = estCollected.groupBy(_._2).mapValues {
    +        _.length
    +      }
    +      logger.debug(s"Cluster counts: Counts: ${counts.mkString(",")}"
    +        + s"\nCluster Estimates: ${estCollected.mkString(",")}")
    +    }
    +    (ccs, estCollected)
    +  }
    +
    +
    +  /**
    +   * Create a Graph given an initial Vt0 and a set of Edges that
    +   * represent the Normalized Affinity Matrix (W)
    +   */
    +  def createGraphFromEdges(sc: SparkContext,
    +                           edgesRdd: RDD[Edge[Double]],
    +                           nPoints: Int,
    +                           optInitialVt: Option[Seq[(VertexId, Double)]] = 
None) = {
    +
    +    assert(nPoints > 0, "Must provide number of points from the original 
dataset")
    +    val G = if (optInitialVt.isDefined) {
    +      val initialVt = optInitialVt.get
    +      val vertsRdd = sc.parallelize(initialVt)
    +      Graph(vertsRdd, edgesRdd)
    +    } else {
    +      Graph.fromEdges(edgesRdd, -1.0)
    +    }
    +    G
    +
    +  }
    +
    +  /**
    +   * Calculate the dominant Eigenvalue and Eigenvector for a given sparse 
graph
    +   * using the PIC method
    +   * @param sc
    +   * @param G  Input Graph representing the Normalized Affinity Matrix (W)
    +   * @param nIterations Number of iterations of the PIC algorithm
    +   * @param optMinNormChange Minimum norm acceleration for detecting 
convergence
    +   *                         - indicated as "epsilon" in the PIC paper
    +   * @return
    +   */
    +  def getPrincipalEigen(sc: SparkContext,
    +                        G: DGraph,
    +                        nIterations: Int = defaultIterations,
    +                        optMinNormChange: Option[Double] = None
    +                         ): (DGraph, Double, VertexRDD[Double]) = {
    +
    +    var priorNorm = Double.MaxValue
    +    var norm = Double.MaxValue
    +    var priorNormVelocity = Double.MaxValue
    +    var normVelocity = Double.MaxValue
    +    var normAccel = Double.MaxValue
    +    val DummyVertexId = -99L
    +    var vnorm: Double = -1.0
    +    var outG: DGraph = null
    +    var prevG: DGraph = G
    +    val epsilon = optMinNormChange
    +      .getOrElse(1e-5 / G.vertices.count())
    --- End diff --
    
    Added the following - feel free to comment further
    
        // The epsilon calculation is provided by the original paper 
www.icml2010.org/papers/387.pdf as epsilon = 1e-5/(#points)
        // However that seems quite small for large#points
        // Instead we use  epsilonPrime = Max(epsilon, 1e-10) 
        
        val epsilon = optMinNormChange
          .getOrElse(math.max(1e-5 / G.vertices.count(), 1e-10))



---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to