-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrandomized_svd.java
54 lines (47 loc) · 1.47 KB
/
randomized_svd.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import org.jblas.Decompose;
import org.jblas.DoubleMatrix;
import org.jblas.Singular;
public class RandomizedSVD {
// Compute a (truncated) randomized SVD of a JBLAS DoubleMatrix
public int numComponents = 2;
public int niters = 5;
private int numOversamples = 10;
private boolean transpose = false;
public DoubleMatrix[] rsvd = new DoubleMatrix[3];
public RandomizedSVD(int numComponents, int niters) {
this.numComponents = numComponents;
this.niters = niters;
}
public void fit(DoubleMatrix A) {
transpose = A.rows > A.columns;
rsvd[0] = new DoubleMatrix(A.rows, numComponents);
rsvd[1] = new DoubleMatrix(numComponents);
rsvd[2] = new DoubleMatrix(A.columns, numComponents);
if (transpose) {
A = A.transpose();
}
DoubleMatrix C = A.mmul(A.transpose());
DoubleMatrix Q = DoubleMatrix.randn(A.rows, Math.min(A.rows, numComponents + numOversamples));
for (int i = 0; i < niters; i++) {
C.mmuli(Q, Q);
Q = Decompose.lu(Q).l;
}
C.mmuli(Q, Q);
Q = Decompose.qr(Q).q;
DoubleMatrix[] svd = Singular.fullSVD(Q.transpose().mmul(A));
DoubleMatrix W = Q.mmul(svd[0]);
if (transpose) {
for (int i = 0; i < numComponents; i++) {
rsvd[0].putColumn(i, svd[2].getColumn(i));
rsvd[1].put(i, svd[1].get(i));
rsvd[2].putColumn(i, W.getColumn(i));
}
} else {
for (int i = 0; i < numComponents; i++) {
rsvd[0].putColumn(i, W.getColumn(i));
rsvd[1].put(i, svd[1].get(i));
rsvd[2].putColumn(i, svd[2].getColumn(i));
}
}
}
}