spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mateiz <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-1390] Refactoring of matrices backed by...
Date Mon, 07 Apr 2014 20:36:07 GMT
Github user mateiz commented on a diff in the pull request:

    https://github.com/apache/spark/pull/296#discussion_r11364111
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/rdd/RowRDDMatrix.scala ---
    @@ -0,0 +1,327 @@
    +/*
    + * 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.linalg.rdd
    +
    +import java.util
    +
    +import scala.util.control.Breaks._
    +
    +import breeze.linalg.{DenseMatrix => BDM, DenseVector => BDV, svd => brzSvd}
    +import breeze.numerics.{sqrt => brzSqrt}
    +import com.github.fommil.netlib.BLAS.{getInstance => blas}
    +
    +import org.apache.spark.mllib.linalg._
    +import org.apache.spark.rdd.RDD
    +import org.apache.spark.Logging
    +
    +/**
    + * Represents a row-oriented RDDMatrix with no meaningful row indices.
    + *
    + * @param rows rows stored as an RDD[Vector]
    + * @param m number of rows
    + * @param n number of columns
    + */
    +class RowRDDMatrix(
    +    val rows: RDD[Vector],
    +    m: Long = -1L,
    +    n: Long = -1) extends RDDMatrix with Logging {
    +
    +  private var _m = m
    +  private var _n = n
    +
    +  /** Gets or computes the number of columns. */
    +  override def numCols(): Long = {
    +    if (_n < 0) {
    +      _n = rows.first().size
    +    }
    +    _n
    +  }
    +
    +  /** Gets or computes the number of rows. */
    +  override def numRows(): Long = {
    +    if (_m < 0) {
    +      _m = rows.count()
    +    }
    +    _m
    +  }
    +
    +  /**
    +   * Computes the Gramian matrix `A^T A`.
    +   */
    +  def computeGramianMatrix(): Matrix = {
    +    val n = numCols().toInt
    +    val nt: Int = n * (n + 1) / 2
    +
    +    // Compute the upper triangular part of the gram matrix.
    +    val GU = rows.aggregate(new BDV[Double](new Array[Double](nt)))(
    +      seqOp = (U, v) => {
    +        RowRDDMatrix.dspr(1.0, v, U.data)
    +        U
    +      },
    +      combOp = (U1, U2) => U1 += U2
    +    )
    +
    +    RowRDDMatrix.triuToFull(n, GU.data)
    +  }
    +
    +  /**
    +   * Computes the singular value decomposition of this matrix.
    +   * Denote this matrix by A (m x n), this will compute matrices U, S, V such that A
= U * S * V'.
    +   *
    +   * There is no restriction on m, but we require `n^2` doubles to fit in memory.
    +   * Further, n should be less than m.
    +
    +   * The decomposition is computed by first computing A'A = V S^2 V',
    +   * computing svd locally on that (since n x n is small), from which we recover S and
V.
    +   * Then we compute U via easy matrix multiplication as U =  A * (V * S^-1).
    +   * Note that this approach requires `O(n^3)` time on the master node.
    +   *
    +   * At most k largest non-zero singular values and associated vectors are returned.
    +   * If there are k such values, then the dimensions of the return will be:
    +   *
    +   * U is a RowRDDMatrix of size m x k that satisfies U'U = eye(k),
    +   * s is a Vector of size k, holding the singular values in descending order,
    +   * and V is a Matrix of size n x k that satisfies V'V = eye(k).
    +   *
    +   * @param k number of singular values to keep. We might return less than k if there
are
    +   *          numerically zero singular values. See rCond.
    +   * @param computeU whether to compute U
    +   * @param rCond the reciprocal condition number. All singular values smaller than rCond
* sigma(0)
    +   *              are treated as zero, where sigma(0) is the largest singular value.
    +   * @return SingularValueDecomposition(U, s, V)
    +   */
    +  def computeSVD(
    +      k: Int,
    +      computeU: Boolean = false,
    +      rCond: Double = 1e-9): SingularValueDecomposition[RowRDDMatrix, Matrix] = {
    +
    +    val n = numCols().toInt
    +
    +    require(k > 0 && k <= n, s"Request up to n singular values k=$k n=$n.")
    +
    +    val G = computeGramianMatrix()
    +
    +    // TODO: Use sparse SVD instead.
    +    val (u: BDM[Double], sigmaSquares: BDV[Double], v: BDM[Double]) =
    +      brzSvd(G.toBreeze.asInstanceOf[BDM[Double]])
    +    val sigmas: BDV[Double] = brzSqrt(sigmaSquares)
    +
    +    // Determine effective rank.
    +    val sigma0 = sigmas(0)
    +    val threshold = rCond * sigma0
    +    var i = 0
    +    breakable {
    +      while (i < k) {
    +        if (sigmas(i) < threshold) {
    +          break()
    +        }
    +        i += 1
    +      }
    +    }
    --- End diff --
    
    I know this code is copied, but `breakable` and `break` are kind of weird to use and expensive.
It would be better to just add a flag for shouldStop and do `while (i < k && !shouldStop)`.


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

Mime
View raw message