-
Notifications
You must be signed in to change notification settings - Fork 2
/
selfrepresentation.py
executable file
·258 lines (214 loc) · 11.2 KB
/
selfrepresentation.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
# Taken from:
# https://github.com/ChongYou/subspace-clustering
# MIT License
import warnings
import numpy as np
import spams
import time
from scipy import sparse
from sklearn import cluster
from sklearn.base import BaseEstimator, ClusterMixin
from sklearn.decomposition import sparse_encode
from sklearn.linear_model import orthogonal_mp
from sklearn.neighbors import kneighbors_graph
from sklearn.preprocessing import normalize
from sklearn.utils import check_random_state, check_array, check_symmetric
class SelfRepresentation(BaseEstimator, ClusterMixin):
"""Base class for self-representation based subspace clustering.
Attributes
----------
representation_matrix_ : array-like, shape (n_samples, n_samples)
Self-representation matrix. Available only if after calling
``fit`` or ``fit_self_representation``.
labels_ :
Labels of each point. Available only if after calling ``fit``.
"""
def __init__(self, n_clusters=8, affinity='symmetrize', random_state=None, n_init=20, n_jobs=1):
self.n_clusters = n_clusters
self.affinity = affinity
self.random_state = random_state
self.n_init = n_init
self.n_jobs = n_jobs
def fit(self, X, y=None):
"""Compute representation matrix, then apply spectral clustering
Parameters
----------
X : array-like or sparse matrix, shape (n_samples, n_features)
"""
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'], dtype=np.float64)
time_base = time.time()
self._self_representation(X)
self.timer_self_representation_ = time.time() - time_base
self._representation_to_affinity()
self._spectral_clustering()
self.timer_time_ = time.time() - time_base
return self
def fit_self_representation(self, X, y=None):
"""Compute representation matrix without apply spectral clustering.
Parameters
----------
X : array-like or sparse matrix, shape (n_samples, n_features)
"""
X = check_array(X, accept_sparse=['csr', 'csc', 'coo'], dtype=np.float64)
time_base = time.time()
self._self_representation(X)
self.timer_self_representation_ = time.time() - time_base
return self
def _representation_to_affinity(self):
"""Compute affinity matrix from representation matrix.
"""
normalized_representation_matrix_ = normalize(self.representation_matrix_, 'l2')
if self.affinity == 'symmetrize':
self.affinity_matrix_ = 0.5 * (np.absolute(normalized_representation_matrix_) + np.absolute(normalized_representation_matrix_.T))
elif self.affinity == 'nearest_neighbors':
neighbors_graph = kneighbors_graph(normalized_representation_matrix_, 3,
mode='connectivity', include_self=False)
self.affinity_matrix_ = 0.5 * (neighbors_graph + neighbors_graph.T)
def _spectral_clustering(self):
affinity_matrix_ = check_symmetric(self.affinity_matrix_)
random_state = check_random_state(self.random_state)
laplacian = sparse.csgraph.laplacian(affinity_matrix_, normed=True)
_, vec = sparse.linalg.eigsh(sparse.identity(laplacian.shape[0]) - laplacian,
k=self.n_clusters, sigma=None, which='LA')
embedding = normalize(vec)
_, self.labels_, _ = cluster.k_means(embedding, self.n_clusters,
random_state=random_state, n_init=self.n_init)
def active_support_elastic_net(X, y, alpha, tau=1.0, algorithm='spams', support_init='knn',
support_size=100, maxiter=40):
"""An active support based algorithm for solving the elastic net optimization problem
min_{c} tau ||c||_1 + (1-tau)/2 ||c||_2^2 + alpha / 2 ||y - c X ||_2^2.
Parameters
-----------
X : array-like, shape (n_samples, n_features)
y : array-like, shape (1, n_features)
alpha : float
tau : float, default 1.0
algorithm : string, default ``spams``
Algorithm for computing solving the subproblems. Either lasso_lars or lasso_cd or spams
(installation of spams package is required).
Note: ``lasso_lars`` and ``lasso_cd`` only support tau = 1.
support_init: string, default ``knn``
This determines how the active support is initialized.
It can be either ``knn`` or ``L2``.
support_size: int, default 100
This determines the size of the working set.
A small support_size decreases the runtime per iteration while increase the number of iterations.
maxiter: int default 40
Termination condition for active support update.
Returns
-------
c : shape n_samples
The optimal solution to the optimization problem.
"""
n_samples = X.shape[0]
if n_samples <= support_size: # skip active support search for small scale data
supp = np.arange(n_samples, dtype=int) # this results in the following iteration to converge in 1 iteration
else:
if support_init == 'L2':
L2sol = np.linalg.solve(np.identity(y.shape[1]) * alpha + np.dot(X.T, X), y.T)
c0 = np.dot(X, L2sol)[:, 0]
supp = np.argpartition(-np.abs(c0), support_size)[0:support_size]
elif support_init == 'knn':
supp = np.argpartition(-np.abs(np.dot(y, X.T)[0]), support_size)[0:support_size]
curr_obj = float("inf")
for _ in range(maxiter):
Xs = X[supp, :]
if algorithm == 'spams':
cs = spams.lasso(np.asfortranarray(y.T), D=np.asfortranarray(Xs.T),
lambda1=tau*alpha, lambda2=(1.0-tau)*alpha)
cs = np.asarray(cs.todense()).T
else:
cs = sparse_encode(y, Xs, algorithm=algorithm, alpha=alpha)
delta = (y - np.dot(cs, Xs)) / alpha
obj = tau * np.sum(np.abs(cs[0])) + (1.0 - tau)/2.0 * np.sum(np.power(cs[0], 2.0)) + alpha/2.0 * np.sum(np.power(delta, 2.0))
if curr_obj - obj < 1.0e-10 * curr_obj:
break
curr_obj = obj
coherence = np.abs(np.dot(delta, X.T))[0]
coherence[supp] = 0
addedsupp = np.nonzero(coherence > tau + 1.0e-10)[0]
if addedsupp.size == 0: # converged
break
# Find the set of nonzero entries of cs.
activesupp = supp[np.abs(cs[0]) > 1.0e-10]
if activesupp.size > 0.8 * support_size: # this suggests that support_size is too small and needs to be increased
support_size = min([round(max([activesupp.size, support_size]) * 1.1), n_samples])
if addedsupp.size + activesupp.size > support_size:
ord = np.argpartition(-coherence[addedsupp], support_size - activesupp.size)[0:support_size - activesupp.size]
addedsupp = addedsupp[ord]
supp = np.concatenate([activesupp, addedsupp])
c = np.zeros(n_samples)
c[supp] = cs
return c
def elastic_net_subspace_clustering(X, gamma=50.0, gamma_nz=True, tau=1.0, algorithm='lasso_lars',
active_support=True, active_support_params=None, n_nonzero=50):
"""Elastic net subspace clustering (EnSC) [1].
References
-----------
[1] C. You, C.-G. Li, D. Robinson, R. Vidal, Oracle Based Active Set Algorithm for Scalable Elastic Net Subspace Clustering, CVPR 2016
[2] E. Elhaifar, R. Vidal, Sparse Subspace Clustering: Algorithm, Theory, and Applications, TPAMI 2013
[3] C. Lu, et al. Robust and efficient subspace segmentation via least squares regression, ECCV 2012
"""
if algorithm in ('lasso_lars', 'lasso_cd') and tau < 1.0 - 1.0e-10:
warnings.warn(f'algorithm {algorithm} cannot handle tau smaller than 1. Using tau = 1')
tau = 1.0
if active_support == True and active_support_params == None:
active_support_params = {}
n_samples = X.shape[0]
rows = np.zeros(n_samples * n_nonzero)
cols = np.zeros(n_samples * n_nonzero)
vals = np.zeros(n_samples * n_nonzero)
curr_pos = 0
for i in range(n_samples):
y = X[i, :].copy().reshape(1, -1)
X[i, :] = 0
if algorithm in ('lasso_lars', 'lasso_cd', 'spams'):
if gamma_nz == True:
coh = np.delete(np.absolute(np.dot(X, y.T)), i)
alpha0 = np.amax(coh) / tau # value for which the solution is zero
alpha = alpha0 / gamma
else:
alpha = 1.0 / gamma
if active_support == True:
c = active_support_elastic_net(X, y, alpha, tau, algorithm, **active_support_params)
else:
if algorithm == 'spams':
c = spams.lasso(np.asfortranarray(y.T), D=np.asfortranarray(X.T),
lambda1=tau * alpha, lambda2=(1.0-tau) * alpha)
c = np.asarray(c.todense()).T[0]
else:
c = sparse_encode(y, X, algorithm=algorithm, alpha=alpha)[0]
else:
warnings.warn("algorithm {} not found".format(algorithm))
index = np.flatnonzero(c)
if index.size > n_nonzero:
index = index[np.argsort(-np.absolute(c[index]))[0:n_nonzero]]
rows[curr_pos:curr_pos + len(index)] = i
cols[curr_pos:curr_pos + len(index)] = index
vals[curr_pos:curr_pos + len(index)] = c[index]
curr_pos += len(index)
X[i, :] = y
return sparse.csr_matrix((vals, (rows, cols)), shape=(n_samples, n_samples))
class ElasticNetSubspaceClustering(SelfRepresentation):
"""Elastic net subspace clustering (EnSC) [1].
References
-----------
[1] C. You, C.-G. Li, D. Robinson, R. Vidal, Oracle Based Active Set Algorithm for Scalable Elastic Net Subspace Clustering, CVPR 2016
[2] E. Elhaifar, R. Vidal, Sparse Subspace Clustering: Algorithm, Theory, and Applications, TPAMI 2013
[3] C. Lu, et al. Robust and efficient subspace segmentation via least squares regression, ECCV 2012
"""
def __init__(self, n_clusters=8, affinity='symmetrize', random_state=None, n_init=20, n_jobs=1, gamma=50.0, gamma_nz=True, tau=1.0,
algorithm='lasso_lars', active_support=True, active_support_params=None, n_nonzero=50):
self.gamma = gamma
self.gamma_nz = gamma_nz
self.tau = tau
self.algorithm = algorithm
self.active_support = active_support
self.active_support_params = active_support_params
self.n_nonzero = n_nonzero
SelfRepresentation.__init__(self, n_clusters, affinity, random_state, n_init, n_jobs)
def _self_representation(self, X):
self.representation_matrix_ = elastic_net_subspace_clustering(X, self.gamma, self.gamma_nz,
self.tau, self.algorithm,
self.active_support, self.active_support_params,
self.n_nonzero)