Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/88#discussion_r10773006
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/SVD.scala ---
@@ -73,19 +170,110 @@ object SVD {
* U is m x k and satisfies U'U = eye(k)
* V is n x k and satisfies V'V = eye(k)
*
- * All input and output is expected in sparse matrix format, 0-indexed
- * as tuples of the form ((i,j),value) all in RDDs using the
- * SparseMatrix class
+ * The return values are as lean as possible: an RDD of rows for U,
+ * a simple array for sigma, and a dense 2d matrix array for V
*
- * @param matrix sparse matrix to factorize
+ * @param matrix dense matrix to factorize
* @param k Recover k singular values and vectors
- * @return Three sparse matrices: U, S, V such that A = USV^T
+ * @return Three matrices: U, S, V such that A = USV^T
*/
- def sparseSVD(
- matrix: SparseMatrix,
- k: Int)
- : MatrixSVD =
- {
+ private def denseSVD(matrix: RDD[Array[Double]]) :
+ (RDD[Array[Double]], Array[Double], Array[Array[Double]])
= {
+ val n = matrix.first.size
+
+ if (k < 1 || k > n) {
+ throw new IllegalArgumentException(
+ "Request up to n singular values k=$k n=$n")
+ }
+
+ // Compute A^T A
+ val fullata = matrix.mapPartitions { iter =>
+ val miniata = Array.ofDim[Double](n, n)
+ while(iter.hasNext) {
+ val row = iter.next
+ var i = 0
+ while(i < n) {
+ var j = 0
+ while(j < n) {
+ miniata(i)(j) += row(i) * row(j)
+ j += 1
+ }
+ i += 1
+ }
+ }
+ List(miniata).iterator
+ }.fold(Array.ofDim[Double](n, n)) { (a, b) =>
+ var i = 0
+ while(i < n) {
+ var j = 0
+ while(j < n) {
+ a(i)(j) += b(i)(j)
+ j += 1
+ }
+ i += 1
+ }
+ a
+ }
+
+ // Construct jblas A^T A locally
+ val ata = new DoubleMatrix(fullata)
+
+ // Since A^T A is small, we can compute its SVD directly
+ val svd = Singular.sparseSVD(ata)
+ val V = svd(0)
+ val sigmas = MatrixFunctions.sqrt(svd(1)).toArray.filter(x => x /
svd(1).get(0) > rCond)
+
+ val sk = Math.min(k, sigmas.size)
+ val sigma = sigmas.take(sk)
+
+ // prepare V for returning
+ val retV = Array.tabulate(n, sk)((i, j) => V.get(i, j))
+
+ if (computeU) {
+ // Compute U as U = A V S^-1
+ // Compute VS^-1
+ val vsinv = new DoubleMatrix(Array.tabulate(n, sk)((i, j) =>
V.get(i, j) / sigma(j)))
+ val retU = matrix.map { x =>
+ val v = new DoubleMatrix(Array(x))
+ v.mmul(vsinv).data
+ }
+ (retU, sigma, retV)
+ } else {
+ (null, sigma, retV)
+ }
+ }
+
+ /**
+ * Singular Value Decomposition for Tall and Skinny sparse matrices.
+ * Given an m x n matrix A, 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.
--- End diff --
n^2 -> `O(n^2)`
---
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.
---