-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriemannianpca.py
165 lines (145 loc) · 5.07 KB
/
riemannianpca.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
import numpy as np
import pymanopt
import torch
from pymanopt.manifolds import Stiefel
from pymanopt.optimizers import ConjugateGradient, SteepestDescent, TrustRegions
from pyriemann.utils.distance import distance_riemann
def nearest_neighbors(C):
"""
returns distances and neighbors for each sample
"""
n_samples = len(C)
tri = np.tri(n_samples, k=-1)
line_idx, col_idx = tri.nonzero()
sample_distances = np.zeros(shape=(n_samples, n_samples))
for i, j in zip(line_idx, col_idx):
sample_distances[i, j] = distance_riemann(C[i], C[j])
sample_distances[j, i] = sample_distances[i, j]
n_ = np.argsort(sample_distances, axis=1)
neighbors = n_[:, 1:]
distances = np.zeros(shape=(n_samples, n_samples - 1))
for i in range(n_samples):
distances[i, :] = sample_distances[i, [neighbors[i, :]]]
return distances, neighbors
def kneighbors(k, neighbors):
"""
return k adjacency matrix from complete neighborhood graph
"""
n_samples = neighbors.shape[0]
nn = np.zeros(shape=(n_samples, n_samples))
for i in range(n_samples):
for j in range(k):
nn[i, neighbors[i, j]] = 1
return nn
def get_cost_pytorch(manifold, C, y, k=3):
"""Create autograd cost function for a set of covariance matrices C
Parameters
----------
manifold; Manifold
Target manifold for optimization
C: array, shape (2 * n_samples, dim, dim)
Covariance matrices to decompose in subspaces
y: array, shape (2 * n_samples)
Class label (-1 or +1) associated with C
k: int
nearest neighbors consider for optimization
Returns
-------
cost: function
Cost function for pymanopt Problem
"""
n_samples, _, _ = C.shape
distances, neighbors = nearest_neighbors(C)
nn_ = torch.from_numpy(kneighbors(k, neighbors))
tri = torch.tril(torch.ones(n_samples, n_samples), diagonal=-1)
line_idx, col_idx = torch.nonzero(tri, as_tuple=True)
C_, y_ = torch.from_numpy(C), torch.from_numpy(y)
@pymanopt.function.pytorch(manifold)
def cost_pytorch(U):
cost = 0.0
for i, j in zip(line_idx, col_idx):
if nn_[i, j] != 0:
ei, ev = torch.linalg.eigh(
torch.matmul(torch.matmul(torch.transpose(U, 1, 0), C_[i]), U),
UPLO="U",
)
W = torch.matmul(
torch.matmul(ev, torch.diag(1.0 / torch.sqrt(ei))),
torch.transpose(ev, 1, 0),
)
C = torch.matmul(
torch.matmul(
torch.matmul(torch.matmul(W, torch.transpose(U, 1, 0)), C_[j]),
U,
),
W,
)
ei = torch.linalg.eigvalsh(C, UPLO="U")
sign = y_[i] * y_[j]
cost += sign * torch.sum(torch.log(ei) ** 2)
return cost
return cost_pytorch
def compute_supervised_rpca(
C,
subspace_dim,
y=None,
backend="pytorch",
solver="steepest",
solv_args=None,
return_log=False,
init=None,
k=3,
):
"""Estimate discrimanative Riemannian PCA from high dim covariance matrices
Parameters
----------
C: array, , shape (2 * n_samples, dim, dim)
Covariance matrices to decompose in subspaces
subspace_dim: int or list
Dimension of the subspaces
y: array, shape (2 * n_samples)
Class label (-1 or +1) associated with C, if None assume that data
are ordered and balanced between class
backend: str
Backend to use for Pymanopt, could be autograd, callable, pytorch,
or pytorch-gpu
solver: str
Solver to use: steepest, trust, conj
solv_args: dict
dict of solver arguments
return_log: bool
return also the log of optimisation problem
init: None, str, or list
Initialization for solver: None, 'Id' or 'SPD' or a list of subspaces
k: int
nearest neighbors consider for optimization
Returns
-------
U_opt: array
Optimal decomposition of subspaces, and their associated weight
logs: list
list of dict for each iteration
"""
n_samples, original_dim, _ = C.shape
manifold = Stiefel(original_dim, subspace_dim)
if y is None:
y = np.array([-1] * (n_samples // 2) + [1] * (n_samples // 2))
if backend == "pytorch":
cost = get_cost_pytorch(manifold, C, y, k)
problem = pymanopt.Problem(manifold=manifold, cost=cost)
else:
raise NotImplementedError("no such backend yet")
if solv_args is None:
solv_args = {"maxtime": float("inf")}
if solver == "steepest":
solver = SteepestDescent(**solv_args, log_verbosity=2)
elif solver == "trust":
solver = TrustRegions(**solv_args, log_verbosity=2)
elif solver == "conj":
solver = ConjugateGradient(**solv_args, log_verbosity=2)
res = solver.run(problem, initial_point=init)
Uopt, logs = res.point, res.log
if return_log:
return Uopt, logs
else:
return Uopt