-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-31007][ML] KMeans optimization based on triangle-inequality #27758
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
66f33de
init
zhengruifeng c6b69ee
update
zhengruifeng 488713e
compute stats distributedly
zhengruifeng c9a720f
package upper tri
zhengruifeng c48183f
need collect
zhengruifeng b050973
add testsuite for statistics
zhengruifeng bb4539b
nit
zhengruifeng d31d488
adress comments
zhengruifeng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,23 +17,125 @@ | |
|
|
||
| package org.apache.spark.mllib.clustering | ||
|
|
||
| import org.apache.spark.SparkContext | ||
| import org.apache.spark.annotation.Since | ||
| import org.apache.spark.broadcast.Broadcast | ||
| import org.apache.spark.ml.impl.Utils.indexUpperTriangular | ||
| import org.apache.spark.mllib.linalg.{Vector, Vectors} | ||
| import org.apache.spark.mllib.linalg.BLAS.{axpy, dot, scal} | ||
| import org.apache.spark.mllib.util.MLUtils | ||
|
|
||
| private[spark] abstract class DistanceMeasure extends Serializable { | ||
|
|
||
| /** | ||
| * Statistics used in triangle inequality to obtain useful bounds to find closest centers. | ||
| * @param distance distance between two centers | ||
| */ | ||
| def computeStatistics(distance: Double): Double | ||
|
|
||
| /** | ||
| * Statistics used in triangle inequality to obtain useful bounds to find closest centers. | ||
| * | ||
| * @return The packed upper triangular part of a symmetric matrix containing statistics, | ||
| * matrix(i,j) represents: | ||
| * 1, if i != j: a bound r = matrix(i,j) to help avoiding unnecessary distance | ||
| * computation. Given point x, let i be current closest center, and d be current best | ||
| * distance, if d < f(r), then we no longer need to compute the distance to center j; | ||
| * 2, if i == j: a bound r = matrix(i,i) = min_k{maxtrix(i,k)|k!=i}. If distance | ||
| * between point x and center i is less than f(r), then center i is the closest center | ||
| * to point x. | ||
| */ | ||
| def computeStatistics(centers: Array[VectorWithNorm]): Array[Double] = { | ||
| val k = centers.length | ||
| if (k == 1) return Array(Double.NaN) | ||
|
|
||
| val packedValues = Array.ofDim[Double](k * (k + 1) / 2) | ||
| val diagValues = Array.fill(k)(Double.PositiveInfinity) | ||
| var i = 0 | ||
| while (i < k) { | ||
| var j = i + 1 | ||
| while (j < k) { | ||
| val d = distance(centers(i), centers(j)) | ||
| val s = computeStatistics(d) | ||
| val index = indexUpperTriangular(k, i, j) | ||
| packedValues(index) = s | ||
| if (s < diagValues(i)) diagValues(i) = s | ||
| if (s < diagValues(j)) diagValues(j) = s | ||
| j += 1 | ||
| } | ||
| i += 1 | ||
| } | ||
|
|
||
| i = 0 | ||
| while (i < k) { | ||
| val index = indexUpperTriangular(k, i, i) | ||
| packedValues(index) = diagValues(i) | ||
| i += 1 | ||
| } | ||
| packedValues | ||
| } | ||
|
|
||
| /** | ||
| * Compute distance between centers in a distributed way. | ||
| */ | ||
| def computeStatisticsDistributedly( | ||
| sc: SparkContext, | ||
| bcCenters: Broadcast[Array[VectorWithNorm]]): Array[Double] = { | ||
| val k = bcCenters.value.length | ||
| if (k == 1) return Array(Double.NaN) | ||
|
|
||
| val packedValues = Array.ofDim[Double](k * (k + 1) / 2) | ||
| val diagValues = Array.fill(k)(Double.PositiveInfinity) | ||
|
|
||
| val numParts = math.min(k, 1024) | ||
| sc.range(0, numParts, 1, numParts) | ||
| .mapPartitionsWithIndex { case (pid, _) => | ||
| val centers = bcCenters.value | ||
| Iterator.range(0, k).flatMap { i => | ||
| Iterator.range(i + 1, k).flatMap { j => | ||
| val hash = (i, j).hashCode.abs | ||
| if (hash % numParts == pid) { | ||
| val d = distance(centers(i), centers(j)) | ||
| val s = computeStatistics(d) | ||
| Iterator.single((i, j, s)) | ||
| } else Iterator.empty | ||
| } | ||
| } | ||
| }.collect.foreach { case (i, j, s) => | ||
| val index = indexUpperTriangular(k, i, j) | ||
| packedValues(index) = s | ||
| if (s < diagValues(i)) diagValues(i) = s | ||
| if (s < diagValues(j)) diagValues(j) = s | ||
| } | ||
|
|
||
| var i = 0 | ||
| while (i < k) { | ||
| val index = indexUpperTriangular(k, i, i) | ||
| packedValues(index) = diagValues(i) | ||
| i += 1 | ||
| } | ||
| packedValues | ||
| } | ||
|
|
||
| /** | ||
| * @return the index of the closest center to the given point, as well as the cost. | ||
| */ | ||
| def findClosest( | ||
| centers: TraversableOnce[VectorWithNorm], | ||
| centers: Array[VectorWithNorm], | ||
| statistics: Array[Double], | ||
| point: VectorWithNorm): (Int, Double) | ||
|
|
||
| /** | ||
| * @return the index of the closest center to the given point, as well as the cost. | ||
| */ | ||
| def findClosest( | ||
| centers: Array[VectorWithNorm], | ||
| point: VectorWithNorm): (Int, Double) = { | ||
| var bestDistance = Double.PositiveInfinity | ||
| var bestIndex = 0 | ||
| var i = 0 | ||
| centers.foreach { center => | ||
| while (i < centers.length) { | ||
| val center = centers(i) | ||
| val currentDistance = distance(center, point) | ||
| if (currentDistance < bestDistance) { | ||
| bestDistance = currentDistance | ||
|
|
@@ -48,7 +150,7 @@ private[spark] abstract class DistanceMeasure extends Serializable { | |
| * @return the K-means cost of a given point against the given cluster centers. | ||
| */ | ||
| def pointCost( | ||
| centers: TraversableOnce[VectorWithNorm], | ||
| centers: Array[VectorWithNorm], | ||
| point: VectorWithNorm): Double = { | ||
| findClosest(centers, point)._2 | ||
| } | ||
|
|
@@ -154,22 +256,79 @@ object DistanceMeasure { | |
| } | ||
|
|
||
| private[spark] class EuclideanDistanceMeasure extends DistanceMeasure { | ||
|
|
||
| /** | ||
| * Statistics used in triangle inequality to obtain useful bounds to find closest centers. | ||
| * @see <a href="https://www.aaai.org/Papers/ICML/2003/ICML03-022.pdf">Charles Elkan, | ||
| * Using the Triangle Inequality to Accelerate k-Means</a> | ||
| * | ||
| * @return One element used in statistics matrix to make matrix(i,j) represents: | ||
| * 1, if i != j: a bound r = matrix(i,j) to help avoiding unnecessary distance | ||
| * computation. Given point x, let i be current closest center, and d be current best | ||
| * squared distance, if d < r, then we no longer need to compute the distance to center | ||
| * j. matrix(i,j) equals to squared of half of Euclidean distance between centers i | ||
| * and j; | ||
| * 2, if i == j: a bound r = matrix(i,i) = min_k{maxtrix(i,k)|k!=i}. If squared | ||
| * distance between point x and center i is less than r, then center i is the closest | ||
| * center to point x. | ||
| */ | ||
| override def computeStatistics(distance: Double): Double = { | ||
| 0.25 * distance * distance | ||
| } | ||
|
|
||
| /** | ||
| * @return the index of the closest center to the given point, as well as the cost. | ||
| */ | ||
| override def findClosest( | ||
| centers: Array[VectorWithNorm], | ||
| statistics: Array[Double], | ||
| point: VectorWithNorm): (Int, Double) = { | ||
| var bestDistance = EuclideanDistanceMeasure.fastSquaredDistance(centers(0), point) | ||
srowen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (bestDistance < statistics(0)) return (0, bestDistance) | ||
|
|
||
| val k = centers.length | ||
| var bestIndex = 0 | ||
| var i = 1 | ||
| while (i < k) { | ||
| val center = centers(i) | ||
| // Since `\|a - b\| \geq |\|a\| - \|b\||`, we can use this lower bound to avoid unnecessary | ||
| // distance computation. | ||
| val normDiff = center.norm - point.norm | ||
| val lowerBound = normDiff * normDiff | ||
| if (lowerBound < bestDistance) { | ||
| val index1 = indexUpperTriangular(k, i, bestIndex) | ||
| if (statistics(index1) < bestDistance) { | ||
| val d = EuclideanDistanceMeasure.fastSquaredDistance(center, point) | ||
| val index2 = indexUpperTriangular(k, i, i) | ||
| if (d < statistics(index2)) return (i, d) | ||
| if (d < bestDistance) { | ||
| bestDistance = d | ||
| bestIndex = i | ||
| } | ||
| } | ||
| } | ||
| i += 1 | ||
| } | ||
| (bestIndex, bestDistance) | ||
| } | ||
|
|
||
| /** | ||
| * @return the index of the closest center to the given point, as well as the squared distance. | ||
| */ | ||
| override def findClosest( | ||
| centers: TraversableOnce[VectorWithNorm], | ||
| centers: Array[VectorWithNorm], | ||
| point: VectorWithNorm): (Int, Double) = { | ||
| var bestDistance = Double.PositiveInfinity | ||
| var bestIndex = 0 | ||
| var i = 0 | ||
| centers.foreach { center => | ||
| while (i < centers.length) { | ||
| val center = centers(i) | ||
| // Since `\|a - b\| \geq |\|a\| - \|b\||`, we can use this lower bound to avoid unnecessary | ||
| // distance computation. | ||
| var lowerBoundOfSqDist = center.norm - point.norm | ||
| lowerBoundOfSqDist = lowerBoundOfSqDist * lowerBoundOfSqDist | ||
| if (lowerBoundOfSqDist < bestDistance) { | ||
| val distance: Double = EuclideanDistanceMeasure.fastSquaredDistance(center, point) | ||
| val distance = EuclideanDistanceMeasure.fastSquaredDistance(center, point) | ||
| if (distance < bestDistance) { | ||
| bestDistance = distance | ||
| bestIndex = i | ||
|
|
@@ -234,6 +393,58 @@ private[spark] object EuclideanDistanceMeasure { | |
| } | ||
|
|
||
| private[spark] class CosineDistanceMeasure extends DistanceMeasure { | ||
|
|
||
| /** | ||
| * Statistics used in triangle inequality to obtain useful bounds to find closest centers. | ||
| * | ||
| * @return One element used in statistics matrix to make matrix(i,j) represents: | ||
| * 1, if i != j: a bound r = matrix(i,j) to help avoiding unnecessary distance | ||
| * computation. Given point x, let i be current closest center, and d be current best | ||
| * squared distance, if d < r, then we no longer need to compute the distance to center | ||
| * j. For Cosine distance, it is similar to Euclidean distance. However, radian/angle | ||
| * is used instead of Cosine distance to compute matrix(i,j): for centers i and j, | ||
| * compute the radian/angle between them, halving it, and converting it back to Cosine | ||
| * distance at the end; | ||
| * 2, if i == j: a bound r = matrix(i,i) = min_k{maxtrix(i,k)|k!=i}. If Cosine | ||
| * distance between point x and center i is less than r, then center i is the closest | ||
| * center to point x. | ||
| */ | ||
| override def computeStatistics(distance: Double): Double = { | ||
| // d = 1 - cos(x) | ||
| // r = 1 - cos(x/2) = 1 - sqrt((cos(x) + 1) / 2) = 1 - sqrt(1 - d/2) | ||
| 1 - math.sqrt(1 - distance / 2) | ||
| } | ||
|
|
||
| /** | ||
| * @return the index of the closest center to the given point, as well as the cost. | ||
| */ | ||
| def findClosest( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any clean way to avoid duplicating most of this code? maybe not. It looks almost identical to the above though |
||
| centers: Array[VectorWithNorm], | ||
| statistics: Array[Double], | ||
| point: VectorWithNorm): (Int, Double) = { | ||
| var bestDistance = distance(centers(0), point) | ||
| if (bestDistance < statistics(0)) return (0, bestDistance) | ||
|
|
||
| val k = centers.length | ||
| var bestIndex = 0 | ||
| var i = 1 | ||
| while (i < k) { | ||
| val index1 = indexUpperTriangular(k, i, bestIndex) | ||
| if (statistics(index1) < bestDistance) { | ||
| val center = centers(i) | ||
| val d = distance(center, point) | ||
| val index2 = indexUpperTriangular(k, i, i) | ||
| if (d < statistics(index2)) return (i, d) | ||
| if (d < bestDistance) { | ||
| bestDistance = d | ||
| bestIndex = i | ||
| } | ||
| } | ||
| i += 1 | ||
| } | ||
| (bestIndex, bestDistance) | ||
| } | ||
|
|
||
| /** | ||
| * @param v1: first vector | ||
| * @param v2: second vector | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.