forked from danielkunin/Regularized-Linear-Autoencoders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithms.py
253 lines (216 loc) · 7.18 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
import numpy as np
from sklearn.utils.extmath import randomized_svd
import time
import matplotlib.pyplot as plt
import scipy.linalg
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))
def compute_svd_error(W2, k, u_svd):
dist = 0;
u, s, _ = np.linalg.svd(W2, full_matrices = False)
for col in range(k):
dist += np.min([np.linalg.norm(u_svd[:, col] - u[:, col]), np.linalg.norm(-u_svd[:, col] - u[:, col])])
return dist
# SVD with dgesdd (divide and conquer)
def svd():
start = time.time()
u, s, _ = np.linalg.svd(X, full_matrices = False)
end = time.time()
t = end - start
return (u, s, t)
# Randomized SVD
def rsvd(k):
start = time.time()
u, s, _ = randomized_svd(X, k)
end = time.time()
t = end - start
return (u, s, t)
# LAE-PCA using untied weights and gradient descent
def LAE_PCA_untied(W1, W2, u_svd = None):
W1 = W1.copy()
W2 = W2.copy()
dist = []
times = []
start = time.time()
XXt = X @ X.T
i = 0
while np.linalg.norm(W1 - W2.T) > eps:
W1 -= alpha * ((W2.T @ (W2 @ W1 - Im)) @ XXt + lamb * W1)
W2 -= alpha * (((W2 @ W1 - Im) @ XXt) @ W1.T + lamb * W2)
if u_svd is not None:
end = time.time()
dist.append( compute_svd_error(W2, k, u_svd) )
times.append(end - start)
start = time.time()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
end = time.time()
t = end - start
return (u, s, t, i, dist, times)
# LAE-PCA using synchronized weights at initialization and gradient descent
def LAE_PCA_sync(W2, u_svd = None):
W1 = W2.copy().T
W2 = W2.copy()
dist = []
times = []
start = time.time()
XXt = X @ X.T
diff = np.inf
i = 0
while diff > eps:
W1_update = alpha * ((W2.T @ (W2 @ W1 - Im)) @ XXt + lamb * W1)
W2_update = alpha * (((W2 @ W1 - Im) @ XXt) @ W1.T + lamb * W2)
W1 -= W1_update
W2 -= W2_update
diff = np.linalg.norm(W1_update) + np.linalg.norm(W2_update)
if u_svd is not None:
end = time.time()
dist.append( compute_svd_error(W2, k, u_svd) )
times.append(end - start)
start = time.time()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
end = time.time()
t = end - start
return (u, s, t, i, dist, times)
# LAE-PCA using single weight matrix and gradient descent (Regularized Oja's Rule)
def LAE_PCA_oja(W2, u_svd = None):
W2 = W2.copy()
dist = []
times = []
start = time.time()
XXt = X @ X.T
diff = np.inf
i = 0
while diff > eps:
update = alpha * (((W2 @ W2.T - Im) @ XXt) @ W2 + lamb * W2)
W2 -= update
diff = np.linalg.norm(update)
if u_svd is not None:
end = time.time()
dist.append( compute_svd_error(W2, k, u_svd) )
times.append(end - start)
start = time.time()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
end = time.time()
t = end - start
return (u, s, t, i, dist, times)
# LAE-PCA using untied weight matrices and alternating exact minimization
def LAE_PCA_exact_untied(W1, W2, u_svd = None):
W1 = W1.copy()
W2 = W2.copy()
dist = []
times = []
start = time.time()
XXt = X @ X.T
i = 0
while np.linalg.norm(W1 - W2.T) > eps:
RHS = (W2.T @ XXt).reshape((m*k, 1))
LHS = np.kron(W2.T @ W2, XXt) # order reversed since matrices are stored per row
np.fill_diagonal(LHS, LHS.diagonal() + lamb)
W1 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos').reshape((k, m))
RHS = W1 @ XXt
LHS = RHS @ W1.T
np.fill_diagonal(LHS, LHS.diagonal() + lamb)
W2 = scipy.linalg.solve(LHS, RHS, assume_a = 'pos').T
if u_svd is not None:
end = time.time()
dist.append( compute_svd_error(W2, k, u_svd) )
times.append(end - start)
start = time.time()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
end = time.time()
t = end - start
return (u, s, t, i, dist, times)
# LAE-PCA using tied weight matrices and alternating exact minimization
def LAE_PCA_exact_tied(W1, W2, u_svd = None):
W1 = W1.copy()
W2 = W2.copy()
dist = []
times = []
start = time.time()
XXt = X @ X.T
i = 0
while np.linalg.norm(W1 - W2.T) > eps:
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
if u_svd is not None:
end = time.time()
dist.append( compute_svd_error(W2, k, u_svd) )
times.append(end - start)
start = time.time()
i += 1
u, s, _ = np.linalg.svd(W2, full_matrices = False)
s = np.sqrt(lamb / (1 - s**2))
end = time.time()
t = end - start
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])
# 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)
(u, s, t, i, _, _) = LAE_PCA_untied(W1, W2, None)
display('LAE-PCA (untied)', t, i, u, s)
(u, s, t, i, _, _) = LAE_PCA_sync(W2, None)
display('LAE-PCA (sync)', t, i, u, s)
(u, s, t, i, _, _) = LAE_PCA_oja(W2, None)
display('LAE-PCA (oja)', t, i, u, s)
(u, s, t, i, _, _) = LAE_PCA_exact_untied(W1, W2, None)
display('LAE-PCA (exact untied)', t, i, u, s)
(u, s, t, i, _, _) = LAE_PCA_exact_tied(W1, W2, None)
display('LAE-PCA (exact tied)', t, i, u, s)
# perform diagnostic runs
(_ ,_ ,_ ,_ ,dist2,times2) = LAE_PCA_untied(W1, W2, u_svd)
(_ ,_ ,_ ,_ ,dist3,times3) = LAE_PCA_sync(W2, u_svd)
(_ ,_ ,_ ,_ ,dist4,times4) = LAE_PCA_oja(W2, u_svd)
(_ ,_ ,_ ,_ ,dist5,times5) = LAE_PCA_exact_untied(W1, W2, u_svd)
(_ ,_ ,_ ,_ ,dist6,times6) = LAE_PCA_exact_tied(W1, W2, u_svd)
plt.plot(dist2)
plt.plot(dist3)
plt.plot(dist4)
plt.plot(dist5)
plt.plot(dist6)
plt.legend(['LAE-PCA (untied)', 'LAE-PCA (sync)', 'LAE-PCA (oja)', 'LAE-PCA (exact untied)', 'LAE-PCA (exact tied)'])
plt.title('Rate of convergence')
plt.xlabel('Iteration')
plt.ylabel('Error in SVD factor U')
plt.show()
plt.plot(np.cumsum(times2), dist2)
plt.plot(np.cumsum(times3), dist3)
plt.plot(np.cumsum(times4), dist4)
plt.plot(np.cumsum(times5), dist5)
plt.plot(np.cumsum(times6), dist6)
plt.legend(['LAE-PCA (untied)', 'LAE-PCA (sync)', 'LAE-PCA (oja)', 'LAE-PCA (exact untied)', 'LAE-PCA (exact tied)'])
plt.title('Rate of convergence')
plt.xlabel('Time (sec)')
plt.ylabel('Error in SVD factor U')
plt.show()