-
Notifications
You must be signed in to change notification settings - Fork 0
/
otpca.py
468 lines (395 loc) · 13.5 KB
/
otpca.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import geoopt
import numpy as np
import ot
import scipy
from sklearn.decomposition import PCA
import torch
import torch.nn.functional as F
import warnings
import time
class NanError(Exception):
def __init__(self, message):
self.message = message
def compute_loss(M, G, reg):
if type(M) == type(G) == torch.Tensor:
return torch.sum(M*G) + reg*torch.sum(G*(torch.log(G)-1))
if type(M) == type(G) == np.ndarray:
return np.sum(M*G) + reg*np.sum(G*(np.log(G)-1))
return TypeError('Error: should be np.ndarray or torch.Tensor...')
def subspace_relative_distance(P1, P2):
# slow when the ambient dimension is large
proj1 = P1@P1.T
proj2 = P2@P2.T
if type(P1) == type(P2) == torch.Tensor:
return torch.linalg.norm(proj1-proj2)/torch.linalg.norm(proj1)
if type(P1) == type(P2) == np.ndarray:
return np.linalg.norm(proj1-proj2)/np.linalg.norm(proj1)
return TypeError('Error: should be np.ndarray or torch.Tensor...')
def Grassmann_distance(P1, P2):
proj = P1.T@P2
if type(P1) == type(P2) == torch.Tensor:
_, s, _ = torch.linalg.svd(proj)
s[s > 1] = 1
s = torch.arccos(s)
return torch.linalg.norm(s)
if type(P1) == type(P2) == np.ndarray:
_, s, _ = np.linalg.svd(proj)
s[s > 1] = 1
s = np.arccos(s)
return np.linalg.norm(s)
return TypeError('Error: should be np.ndarray or torch.Tensor...')
class StiefelLinear(torch.nn.Module):
def __init__(self, k, d, device):
super(StiefelLinear, self).__init__()
self.d = d
self.k = k
self.device = device
self.weight = geoopt.ManifoldParameter(
data=torch.Tensor(d, k),
manifold=geoopt.Stiefel(canonical=False)
)
self._init_done = False
def reset_parameters(self, input):
with torch.no_grad():
U, _, _ = torch.linalg.svd(input.T, full_matrices=False)
self.weight.data = U[:, :self.k]
self.weight.data = self.weight.data.to(self.device) # TODO: necessary?
self._init_done = True
def forward(self, input):
if self._init_done is False:
self.reset_parameters(input)
return F.linear(input, self.weight)
def forward_T(self, input):
if self._init_done is False:
self.reset_parameters(input)
return F.linear(input, self.weight.T)
def fforward(self, input):
if self._init_done is False:
self.reset_parameters(input)
return F.linear(input, self.weight @ self.weight.T)
def ot_pca_bcd(
X, k, reg=1e-3, thresh=1e-4, max_iter=10000,
P0=None, max_iter_sink=100, max_iter_MM=10,
verbose=False, log=True, method_sink='sinkhorn',
method='MM', svd_fct_cpu='numpy', device='cpu',
warn=True
):
if verbose is False:
verbose = 0
elif verbose is True:
verbose = 1
if verbose > 0:
print()
assert device in ['cpu', 'cuda']
if device == 'cuda':
X = torch.tensor(X, dtype=torch.float64)
X = X.to(device)
n, d = X.shape
X = X - X.mean(0) # center data
assert k <= d
t0 = time.time()
if P0 is None:
if device == 'cpu':
pca_fitted = PCA(n_components=k).fit(X)
P = pca_fitted.components_.T
if method == 'MM':
lambda_scm = pca_fitted.explained_variance_[0]
elif device == 'cuda':
P, s, _ = torch.linalg.svd(X.T, full_matrices=False)
P = P[:, :k]
if method == 'MM':
lambda_scm = (1/n)*(s[0]**2)
else:
P = P0
if verbose > 1:
print('init time:', time.time() - t0)
# marginals
if device == 'cpu':
u0 = (1./n)*np.ones(n)
elif device == 'cuda':
u0 = (1./n)*torch.ones(n, dtype=torch.float64).to(device)
# log
if log:
log_ = {}
log_['P'] = []
log_['G'] = []
if device == 'cpu':
log_['P0'] = P
elif device == 'gpu':
log_['P0'] = P.detach().clone().cpu().numpy()
log_['loss'] = []
# print iterations
if verbose > 0:
if method == 'BCD':
print('OTPCA BCD')
elif method == 'MM':
print('OTPCA MM')
else:
raise ValueError('Unknown method...')
print('{:4s}|{:13s}|{:12s}|{:12s}|{:8s}'.format(
'It.', 'Loss', 'Crit.', 'Thres.', 'Time.'
) + '\n' + '-' * 48)
# loop
go = True
it = 0
warmstart_sinkhorn = None
while go:
st = time.time()
P_old = P
# Solve transport
t0 = time.time()
M = ot.dist(X, (X@P)@P.T)
G, log_sinkhorn = ot.sinkhorn(
u0, u0, M, reg,
numItermax=max_iter_sink,
method=method_sink, warmstart=warmstart_sinkhorn,
warn=warn, log=True
)
key_warmstart = 'warmstart'
if key_warmstart in log_sinkhorn:
alpha, beta = log_sinkhorn[key_warmstart]
if type(alpha) == type(beta) == torch.Tensor:
alpha, beta = alpha.detach(), beta.detach()
warmstart_sinkhorn = (alpha, beta)
if (G >= 1e-300).all():
loss = compute_loss(M, G, reg)
else:
if device == 'cpu':
loss = np.inf
elif device == 'cuda':
loss = torch.inf
if verbose > 1:
print('OT time:', time.time() - t0)
# log
if log:
if device == 'cpu':
log_['P'].append(P)
log_['G'].append(G)
log_['loss'].append(loss)
elif device == 'cuda':
log_['P'].append(P.detach().clone().cpu().numpy())
log_['G'].append(G.detach().clone().cpu().numpy())
log_['loss'].append(loss.item())
# Solve PCA
t0 = time.time()
G_sym = (G+G.T)/2
if method == 'BCD':
# block coordinate descent
t0_bcd = time.time()
if device == 'cpu':
eye = np.eye(n)
elif device == 'cuda':
eye = torch.eye(n).to(device)
S = X.T @ (2*G_sym-(1./n)*eye) @ X
if verbose > 1:
print('BCD S computation time:', time.time() - t0_bcd)
t1_bcd = time.time()
if device == 'cpu':
if svd_fct_cpu == 'scipy':
_, U = scipy.sparse.linalg.eigsh(S, k, which='LA')
P = U[:, ::-1]
elif svd_fct_cpu == 'numpy':
_, U = np.linalg.eigh(S)
P = U[:, ::-1][:, :k]
else:
raise ValueError('Wrong value for svd_fct_cpu...')
elif device == 'cuda':
_, U = torch.linalg.eigh(S)
idx = np.array(np.arange(d)[::-1][:k])
P = U[:, idx]
if verbose > 1:
print('BCD svd computation time:', time.time() - t1_bcd)
elif method == 'MM':
# majorization-minimization
t0_mm = time.time()
if device == 'cpu':
if svd_fct_cpu == 'scipy':
eig, _ = scipy.sparse.linalg.eigsh(G_sym, k=1, which='SA')
elif svd_fct_cpu == 'numpy':
eig, _ = np.linalg.eigh(G_sym)
else:
raise ValueError('Wrong value for svd_fct_cpu...')
lambda_G = eig[0]
elif device == 'cuda':
raise NotImplementedError
if verbose > 1:
print('MM lambda min computation time:', time.time() - t0_mm)
t_R, t_proj = 0, 0
for _ in range(max_iter_MM):
t1_mm = time.time()
X_proj = X@P
X_T_X_proj = X.T@X_proj
R = (1/n) * X_T_X_proj
alpha = 1 - 2*n*lambda_G
if alpha > 0:
R = alpha*(R - lambda_scm*P)
else:
R = alpha*R
R = R - (2*X.T@(G_sym@X_proj)) + (2*lambda_G*X_T_X_proj)
t_R += time.time() - t1_mm
t2_mm = time.time()
if device == 'cpu':
P, _ = np.linalg.qr(R)
# U, s, Vt = np.linalg.svd(R, full_matrices=False)
# P = U@Vt
elif device == 'cuda':
P, _ = torch.linalg.qr(R)
t_proj += time.time() - t2_mm
if verbose > 1:
print('MM R computation time:', t_R)
print('MM proj computation time:', t_proj)
else:
raise ValueError('Unknown method...')
if verbose > 1:
print('Update U time:', time.time() - t0)
# stop or not
crit = Grassmann_distance(P_old, P)
if crit <= thresh:
go = False
it += 1
if it >= max_iter:
go = False
if warn:
warnings.warn('ot_pca_bcd: max iter reached')
# print
ed = time.time()
if verbose > 0:
print('{:4d}|{:8e}|{:8e}|{:8e}|{:8e}'.format(
it, loss, crit, thresh, ed-st))
if log:
log_['it'] = it
if device == 'cuda':
G = G.detach().clone().cpu().numpy()
P = P.detach().clone().cpu().numpy()
if log:
return G, P, log_
else:
return G, P
def ot_pca_auto_diff(
X, k, reg=1e-3, lr=1e-2, thresh=1e-3, max_iter=100,
P0=None, max_iter_sink=100, verbose=False, log=True,
method_sink='sinkhorn', device=None, warn=True
):
if verbose is False:
verbose = 0
elif verbose is True:
verbose = 1
if verbose > 0:
print()
# optimization on the Stiefel manifold
manifold = geoopt.Stiefel()
# X
X = torch.from_numpy(X).type(torch.float64).to(device)
X = X - X.mean(0) # center data
n, d = X.shape
assert k <= d
# device
if device is None:
device = torch.device('cpu')
else:
device = device
# init subspace basis
if P0 is None:
P = StiefelLinear(d=d, k=k, device=device)
P.reset_parameters(X)
else:
P = P0
# marginals
u0 = (1./n)*torch.ones(n, dtype=torch.float64).to(device)
# optimizer
optimizer = geoopt.optim.RiemannianAdam(P.parameters(), lr=lr)
# log
if log:
log_ = {}
log_['P'] = []
log_['G'] = []
log_['P0'] = P.weight.detach().clone().numpy()
log_['loss'] = []
log_['gradnorm'] = []
log_['backward'] = []
# print iterations
if verbose:
print('OTPCA Riemannian')
print('{:4s}|{:13s}|{:12s}|{:12s}|{:8s}'.format(
'It.', 'Loss', 'Crit.', 'Thres.', 'Time.'
) + '\n' + '-' * 48)
# loop
go = True
it = 0
warmstart_sinkhorn = None
while go:
st = time.time()
optimizer.zero_grad()
P_old = P.weight.data.detach().clone()
# compute loss
t0 = time.time()
M = ot.dist(X, P.forward(P.forward_T(X)))
G, log_sinkhorn = ot.sinkhorn(
u0, u0, M, reg,
numItermax=max_iter_sink,
method=method_sink, warmstart=warmstart_sinkhorn,
warn=warn, log=True
)
key_warmstart = 'warmstart'
if key_warmstart in log_sinkhorn:
alpha, beta = log_sinkhorn[key_warmstart]
if type(alpha) == type(beta) == torch.Tensor:
alpha, beta = alpha.detach(), beta.detach()
warmstart_sinkhorn = (alpha, beta)
loss = compute_loss(M, G, reg)
if torch.isnan(loss).any():
print(G)
print(M)
raise NanError('Loss is Nan')
if verbose > 1:
print(f'Loss evaluation time: {time.time() - t0:,.4f}s')
# gradient step
t0 = time.time()
loss.backward()
if verbose > 1:
print(f'Gradient evaluation time: {time.time() - t0:,.4f}s')
t0 = time.time()
optimizer.step()
t_backward = time.time() - t0
if verbose > 1:
print(f'Optimizer step time: {time.time() - t0:,.4f}s')
# draft of a possibly fater gradient computation
# X_proj = P.forward_T(X)
# loss = - torch.sum(u0 * (torch.linalg.norm(X_proj, axis=1)**2))
# gram_matrix = X_proj @ X_proj.T
# loss = loss - 2 * torch.sum(G*gram_matrix)
# loss.backward()
# log
t0 = time.time()
if log:
log_['backward'].append(t_backward)
log_['P'].append(P.weight.data.detach().clone().numpy())
log_['G'].append(G.data.detach().clone().numpy())
log_['loss'].append(loss.item())
rgrad = manifold.egrad2rgrad(P.weight, P.weight.grad)
gradnorm = manifold.inner(P.weight, rgrad).item()
log_['gradnorm'].append(gradnorm)
if verbose > 1:
print(f'Log time: {time.time() - t0:,.4f}s')
# stop conditions
t0 = time.time()
crit = Grassmann_distance(P_old, P.weight.data)
it += 1
if crit <= thresh:
go = False
if it >= max_iter:
go = False
if verbose > 1:
print(f'Stopping criteria eval. time: {time.time() - t0:,.4f}s')
# print
ed = time.time()
if verbose:
print('{:4d}|{:8e}|{:8e}|{:8e}|{:8e}'.format(
it, loss, crit, thresh, ed-st))
P = P.weight.data.detach().clone().numpy()
G = G.data.detach().clone().numpy()
if log:
log_['it'] = it
return G, P, log_
else:
return G, P