-
Notifications
You must be signed in to change notification settings - Fork 2
/
svdbenchmark.py
143 lines (122 loc) · 4.76 KB
/
svdbenchmark.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import time
import numpy
import sppy
import sppy.linalg
import matplotlib.pyplot as plt
import scipy.sparse
import os
from scipy.sparse.linalg import svds
from pypropack import svdp
from sparsesvd import sparsesvd
from sklearn.decomposition import TruncatedSVD
from sppy.linalg import GeneralLinearOperator
def time_reps(func, params, reps):
start = time.time()
for s in range(reps):
func(*params)
print("Executed " + str(func))
return (time.time() - start) / reps
"""
Compare and contrast different methods of computing the SVD
"""
# Fix issue with CPU affinity
os.system("taskset -p 0xff %d" % os.getpid())
k = 50
p = 0
reps = 2
density = 10**-3
n_iter = 5
truncated_svd = TruncatedSVD(k, n_iter=5)
var_range = numpy.arange(1, 11)
def time_densities():
n = 10**4
densities = var_range * 2 * 10**-3
times = numpy.zeros((5, densities.shape[0]))
for i, density in enumerate(densities):
# Generate random sparse matrix
inds = numpy.random.randint(n, size=(2, n * n * density))
data = numpy.random.rand(n * n * density)
A = scipy.sparse.csc_matrix((data, inds), (n, n))
A_sppy = sppy.csarray(A, storagetype="row")
L = GeneralLinearOperator.asLinearOperator(A_sppy, parallel=True)
print(A.shape, A.nnz)
times[0, i] = time_reps(svds, (A, k), reps)
times[1, i] = time_reps(svdp, (A, k), reps)
# Remove SparseSVD since it is significantly slower than the other methods
# times[2, i] = time_reps(sparsesvd, (A, k), reps)
times[3, i] = time_reps(truncated_svd.fit, (A,), reps)
times[4, i] = time_reps(sppy.linalg.rsvd, (L, k, p, n_iter), reps)
print(n, density, times[:, i])
plt.figure(0)
plt.plot(densities, times[0, :], 'k-', label="ARPACK")
plt.plot(densities, times[1, :], 'r-', label="PROPACK")
# plt.plot(densities, times[2, :], 'b-', label="SparseSVD")
plt.plot(densities, times[3, :], 'k--', label="sklearn RSVD")
plt.plot(densities, times[4, :], 'r--', label="sppy RSVD")
plt.legend(loc="upper left")
plt.xlabel("density")
plt.ylabel("time (s)")
plt.savefig("time_densities.png", format="png")
# Next, vary the matrix size
def time_ns():
density = 10**-3
ns = var_range * 10**4
times = numpy.zeros((5, ns.shape[0]))
for i, n in enumerate(ns):
# Generate random sparse matrix
inds = numpy.random.randint(n, size=(2, n * n * density))
data = numpy.random.rand(n * n * density)
A = scipy.sparse.csc_matrix((data, inds), (n, n))
A_sppy = sppy.csarray(A, storagetype="row")
L = GeneralLinearOperator.asLinearOperator(A_sppy, parallel=True)
print(A.shape, A.nnz)
times[0, i] = time_reps(svds, (A, k), reps)
times[1, i] = time_reps(svdp, (A, k), reps)
# times[2, i] = time_reps(sparsesvd, (A, k), reps)
times[3, i] = time_reps(truncated_svd.fit, (A,), reps)
times[4, i] = time_reps(sppy.linalg.rsvd, (L, k, p, n_iter), reps)
print(n, density, times[:, i])
plt.figure(1)
plt.plot(ns, times[0, :], 'k-', label="ARPACK")
plt.plot(ns, times[1, :], 'r-', label="PROPACK")
# plt.plot(ns, times[2, :], 'b-', label="SparseSVD")
plt.plot(ns, times[3, :], 'k--', label="sklearn RSVD")
plt.plot(ns, times[4, :], 'r--', label="sppy RSVD")
plt.legend(loc="upper left")
plt.xlabel("n")
plt.ylabel("time (s)")
plt.savefig("time_ns.png", format="png")
def time_ks():
n = 10**4
density = 10**-3
ks = var_range * 20
times = numpy.zeros((5, ks.shape[0]))
for i, k in enumerate(ks):
# Generate random sparse matrix
inds = numpy.random.randint(n, size=(2, n * n * density))
data = numpy.random.rand(n * n * density)
A = scipy.sparse.csc_matrix((data, inds), (n, n))
A_sppy = sppy.csarray(A, storagetype="row")
L = GeneralLinearOperator.asLinearOperator(A_sppy, parallel=True)
print(A.shape, A.nnz)
times[0, i] = time_reps(svds, (A, k), reps)
times[1, i] = time_reps(svdp, (A, k), reps)
# times[2, i] = time_reps(sparsesvd, (A, k), reps)
truncated_svd = TruncatedSVD(k, n_iter=5)
times[3, i] = time_reps(truncated_svd.fit, (A,), reps)
times[4, i] = time_reps(sppy.linalg.rsvd, (L, k, p, n_iter), reps)
print(n, density, times[:, i])
plt.figure(2)
plt.plot(ks, times[0, :], 'k-', label="ARPACK")
plt.plot(ks, times[1, :], 'r-', label="PROPACK")
# plt.plot(ks, times[2, :], 'b-', label="SparseSVD")
plt.plot(ks, times[3, :], 'k--', label="sklearn RSVD")
plt.plot(ks, times[4, :], 'r--', label="sppy RSVD")
plt.legend(loc="upper left")
plt.xlabel("k")
plt.ylabel("time (s)")
plt.savefig("time_ks.png", format="png")
time_densities()
time_ks()
time_ns()
plt.show()