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

    https://github.com/apache/spark/pull/4459#discussion_r24302656
  
    --- Diff: 
mllib/src/main/scala/org/apache/spark/mllib/clustering/GaussianMixture.scala ---
    @@ -215,20 +217,29 @@ private object ExpectationSum {
       def add(
           weights: Array[Double], 
           dists: Array[MultivariateGaussian])
    -      (sums: ExpectationSum, x: BreezeVector[Double]): ExpectationSum = {
    +      (sums: ExpectationSum, x: BV[Double]): ExpectationSum = {
         val p = weights.zip(dists).map {
           case (weight, dist) => MLUtils.EPSILON + weight * dist.pdf(x)
         }
         val pSum = p.sum
         sums.logLikelihood += math.log(pSum)
    -    val xxt = x * new Transpose(x)
         var i = 0
    +    val isSparse = x match {
    +      case _: BSV[Double] => true
    +      case _: BDV[Double] => false
    +    }
         while (i < sums.k) {
           p(i) /= pSum
           sums.weights(i) += p(i)
           sums.means(i) += x * p(i)
    -      BLAS.syr(p(i), Vectors.fromBreeze(x).asInstanceOf[DenseVector],
    -        Matrices.fromBreeze(sums.sigmas(i)).asInstanceOf[DenseMatrix])
    +      if (isSparse){
    +        BLAS.syr(p(i), Vectors.fromBreeze(x).asInstanceOf[SparseVector],
    +          Matrices.fromBreeze(sums.sigmas(i)).asInstanceOf[DenseMatrix])
    +      }
    +      else {
    +        BLAS.syr(p(i), Vectors.fromBreeze(x).asInstanceOf[DenseVector],
    +          Matrices.fromBreeze(sums.sigmas(i)).asInstanceOf[DenseMatrix])
    +      }
    --- End diff --
    
    I would push the logic of checking for sparsity into the BLAS.syr method... 
(many other BLAS routines already do this... see BLAS.{axpy, copy, dot, gemm, 
scal, ...}) ... then this could simply be
    
    BLAS.syr(p(i), Vectors.fromBreeze(x), 
Matrices.fromBreeze(sums.sigmas(i).asInstanceOf[DenseMatrix])
    
    (obviating the need for the isSparse value)


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

Reply via email to