Github user tgaloppo commented on a diff in the pull request:
https://github.com/apache/spark/pull/4654#discussion_r24846194
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala ---
@@ -135,25 +135,39 @@ class GaussianMixture private (
val breezeData = data.map(_.toBreeze).cache()
// Get length of the input vectors
- val d = breezeData.first().length
-
+ val tmp = breezeData.first()
+ val (d, nFeatures) = (tmp.length, tmp.size)
+
+ val distributeGaussian = if (k >= 10 && nFeatures >= 10) true else
false
// Determine initial weights and corresponding Gaussians.
// If the user supplied an initial GMM, we use those values, otherwise
// we start with uniform weights, a random mean from the data, and
// diagonal covariance matrices using component variances
- // derived from the samples
+ // derived from the samples.
val (weights, gaussians) = initialModel match {
case Some(gmm) => (gmm.weights, gmm.gaussians)
case None => {
val samples = breezeData.takeSample(withReplacement = true, k *
nSamples, seed)
- (Array.fill(k)(1.0 / k), Array.tabulate(k) { i =>
- val slice = samples.view(i * nSamples, (i + 1) * nSamples)
- new MultivariateGaussian(vectorMean(slice),
initCovariance(slice))
- })
+ val weights = Array.fill(k)(1.0 / k)
+ val gaussians = {
+ if (distributeGaussian) {
+ Array.tabulate(k) { i =>
+ val slice = samples.view(i * nSamples, (i + 1) * nSamples)
+ new MultivariateGaussian(vectorMean(slice),
initCovariance(slice))
+ }
+ }
+ else {
+ sc.parallelize(0 until k).map { i =>
+ val slice = samples.view(i * nSamples, (i + 1) * nSamples)
+ new MultivariateGaussian(vectorMean(slice),
initCovariance(slice))
+ }
+ }.collect
+ }
+ (weights, gaussians)
}
}
-
+
--- End diff --
This only parallelizes the very first set of gaussian initializations...
even if the parallelization is effective (which, as discussed on Jira, I have
my doubts), this will be a very small gain... better to focus on the
initializations at line 191 (in this revision), which occur with each iteration.
---
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.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]