-
Notifications
You must be signed in to change notification settings - Fork 13
/
algorithms.py
292 lines (246 loc) · 8.79 KB
/
algorithms.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import numpy as np
from sklearn.utils.extmath import randomized_svd
import time
import matplotlib.pyplot as plt
import scipy.linalg
from enum import Enum
n = 10000
m = 10
k = 2
eps = 1e-8
alpha = 1e-5
lamb = 50
Im = np.eye(m)
Ik = np.eye(k)
np.random.seed(0)
X = np.random.normal(size = (m, n))
X = X - np.mean(X, axis=1, keepdims=True)
W1 = np.random.normal(size = (k, m))
W2 = np.random.normal(size = (m, k))
class SyncMode(Enum):
UNTIED = 1
SYNC = 2
TIED = 3
class t_timer:
def __init__(self):
self.times = []
self.start = time.time()
def lap(self):
end = time.time()
self.times.append(end - self.start)
def reset(self):
self.start = time.time()
def total(self):
end = time.time()
return np.sum(self.times) + end - self.start
class error_metric_svd:
def __init__(self, u_svd, k):
self.description = 'Error in SVD factor U'
self.u_svd = u_svd
self.k = k
def compute_error(self, W2, W1 = None):
dist = 0
u, s, _ = np.linalg.svd(W2, full_matrices = False)
for col in range(k):
dist += np.min([np.linalg.norm(self.u_svd[:, col] - u[:, col]), np.linalg.norm(-self.u_svd[:, col] - u[:, col])])
return dist
class error_metric_objective:
def __init__(self, X, lamb):
self.X = X
self.lamb = lamb
self.description = 'Objective value (lambda = ' + str(lamb) + ')'
def compute_error(self, W2, W1 = None):
if W1 is None:
return np.linalg.norm(X - W2 @ W2.T @ X, 'fro')**2 + 2 * lamb * np.linalg.norm(W2)**2
else:
return np.linalg.norm(X - W2 @ W1 @ X, 'fro')**2 + lamb * np.linalg.norm(W2)**2 + lamb * np.linalg.norm(W1)**2
# SVD with dgesdd (divide and conquer)
def svd():
timer = t_timer()
u, s, _ = np.linalg.svd(X, full_matrices = False)
t = timer.total()
return (u, s, t)
def rsvd(k):
timer = t_timer()
u, s, _ = randomized_svd(X, k)
t = timer.total()
return (u, s, t)
# function values and derivatives
def f_W1(XXt, W1, W2):
return np.linalg.norm(X - W2 @ W1 @ X, 'fro')**2 + lamb * np.linalg.norm(W1)**2
def f_W2(XXt, W1, W2):
return np.linalg.norm(X - W2 @ W1 @ X, 'fro')**2 + lamb * np.linalg.norm(W2)**2
def Df_W1(XXt, W1, W2):
return (W2.T @ (W2 @ W1 - Im)) @ XXt + lamb * W1
def Df_W2(XXt, W1, W2):
return ((W2 @ W1 - Im) @ XXt) @ W1.T + lamb * W2
# LAE-PCA using gradient descent
def LAE_PCA_GD(W1, W2, syncMode, error_metric = None):
W1 = W1.copy()
W2 = W2.copy()
dist = []
timer = t_timer()
XXt = X @ X.T
diff = np.inf
i = 0
while diff > eps:
if syncMode == SyncMode.UNTIED:
W1 -= alpha * Df_W1(XXt, W1, W2)
W2 -= alpha * Df_W2(XXt, W1, W2)
diff = np.linalg.norm(W1 - W2.T)
elif syncMode == SyncMode.SYNC:
W1_update = alpha * Df_W1(XXt, W1, W2)
W2_update = alpha * Df_W2(XXt, W1, W2)
W1 -= W1_update
W2 -= W2_update
W1 = (W1 + W2.T) / 2
W2 = W1.T.copy()
diff = np.linalg.norm(W1_update) + np.linalg.norm(W2_update)
else:
update = alpha * Df_W2(XXt, W2.T, W2)
W2 -= update
W1 = W2.T
diff = 2 * np.linalg.norm(update)
if error_metric is not None:
timer.lap()
dist.append( error_metric(W2, W1) )
timer.reset()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
times = timer.times
t = timer.total()
return (u, s, t, i, dist, times)
# LAE-PCA using an optimization algorithm from Scipy
def LAE_PCA_SCIPY(method, W1, W2, syncMode, error_metric = None):
W1 = W1.copy()
W2 = W2.copy()
dist = []
timer = t_timer()
XXt = X @ X.T
diff = np.inf
i = 0
gradient_evalutions = 0
while diff > 100*eps:
if syncMode == SyncMode.UNTIED:
f = lambda W1 : f_W1(XXt, W1.reshape((k, m)), W2)
Df = lambda W1 : Df_W1(XXt, W1.reshape((k, m)), W2).reshape((k * m))
result = scipy.optimize.minimize(f, W1.reshape((k * m)), method = method, jac = Df)
W1 = result.x.reshape((k, m))
gradient_evalutions += result.nit
f = lambda W2 : f_W2(XXt, W1, W2.reshape((m, k)))
Df = lambda W2 : Df_W2(XXt, W1, W2.reshape((m, k))).reshape((m * k))
result = scipy.optimize.minimize(f, W2.reshape((m * k)), method = method, jac = Df)
W2 = result.x.reshape((m, k))
gradient_evalutions += result.nit
diff = np.linalg.norm(W1 - W2.T)
else:
raise Exception('Unsupported SyncMode for LAE_PCA_SCIPY.')
if error_metric is not None:
timer.lap()
dist.append( error_metric(W2, W1) )
timer.reset()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
times = timer.times
t = timer.total()
#print('Number of gradient evalutions:', gradient_evalutions)
return (u, s, t, i, dist, times)
# LAE-PCA using exact minimization
def LAE_PCA_exact(W1, W2, syncMode, error_metric = None):
W1 = W1.copy()
W2 = W2.copy()
dist = []
timer = t_timer()
XXt = X @ X.T
diff = np.inf
i = 0
while diff > eps:
if syncMode == SyncMode.UNTIED:
LHS = np.kron(W2.T @ W2, XXt) # order reversed since matrices are stored per row
np.fill_diagonal(LHS, LHS.diagonal() + lamb)
RHS = (W2.T @ XXt).reshape((m * k, 1))
W1 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos').reshape((k, m))
RHS = W1 @ XXt
LHS = RHS @ W1.T + lamb * Ik
W2 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos').T
diff = np.linalg.norm(W1 - W2.T)
elif syncMode == SyncMode.SYNC:
RHS = W2.T @ XXt
LHS = RHS @ W2 + lamb * Ik
W1 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos')
RHS = W1 @ XXt
LHS = RHS @ W1.T + lamb * Ik
W2 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos').T
W1 = (W1 + W2.T) / 2
W2 = W1.T
if i == 0:
diff = eps + 1
else:
diff = np.linalg.norm(W1 - prev_W1)
prev_W1 = W1
else:
RHS = W1 @ XXt
LHS = RHS @ W1.T + lamb * Ik
W2 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos').T
diff = np.linalg.norm(W1 - W2.T)
W1 = W2.T
if error_metric is not None:
timer.lap()
dist.append( error_metric(W2, W1) )
timer.reset()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
times = timer.times
t = timer.total()
return (u, s, t, i, dist, times)
def display(method, t, i, u, s):
if i == None:
print('{:s} ({:0.5f} secs)'.format(method, t))
else:
print('{:s} ({:0.5f} secs, {} iterations)'.format(method, t, i))
print(s[0:k])
print(u[:, 0:k])
algorithms = [('GD-untied', LAE_PCA_GD, [W1, W2, SyncMode.UNTIED]),
('GD-sync', LAE_PCA_GD, [W2.T, W2, SyncMode.SYNC]),
('GD-tied', LAE_PCA_GD, [W1, W2, SyncMode.TIED]),
#('L-BFGS untied', LAE_PCA_SCIPY, ['L-BFGS-B', W1, W2, SyncMode.UNTIED]),
('exact-untied', LAE_PCA_exact, [W1, W2, SyncMode.UNTIED]),
('exact-sync', LAE_PCA_exact, [W2.T, W2, SyncMode.SYNC]),
('exact-tied', LAE_PCA_exact, [W1, W2, SyncMode.TIED]),]
# perform timing runs
(u_svd, s, t) = svd()
display('SVD', t, None, u_svd, s)
(u, s, t) = rsvd(k)
display('Randomized SVD', t, None, u, s)
for algorithm in algorithms:
(u, s, t, i, _, _) = algorithm[1](*algorithm[2], None)
display(algorithm[0], t, i, u, s)
# perform diagnostic runs
error_metric = error_metric_svd(u_svd, k)
#error_metric = error_metric_objective(X, lamb)
distances = []
runtimes = []
for algorithm in algorithms:
(_ ,_ ,_ ,_ ,distance,runtime) = algorithm[1](*algorithm[2], error_metric.compute_error)
distances.append(distance)
runtimes.append(runtime)
# plot results
legend = []
for idx, algorithm in enumerate(algorithms):
legend.append(algorithm[0])
plt.plot(distances[idx])
plt.legend(legend)
plt.title('LAE-PCA - Rate of convergence')
plt.xlabel('Iteration')
plt.ylabel(error_metric.description)
plt.show()
for idx, algorithm in enumerate(algorithms):
plt.plot(np.cumsum(runtimes[idx]), distances[idx])
plt.legend(legend)
plt.title('Rate of convergence')
plt.xlabel('Time (sec)')
plt.ylabel(error_metric.description)
plt.show()