-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_kernel.py
265 lines (234 loc) · 8.31 KB
/
part_kernel.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
import inspect
from numpy import ones, zeros, where, argmin, unique, array
from numpy import logical_and, logical_or, arange, sqrt
from numpy import maximum, pi, log
from numpy.random import choice
from numpy.linalg import norm, det
from scipy.stats import binom, uniform
from sklearn.metrics.pairwise import pairwise_distances
from scipy.spatial.distance import euclidean
from scipy.sparse.linalg import LinearOperator, cg
from random import randint
from numpy.random import randint as nrandint
from multiprocessing import Pool, Array
import ctypes
from numpy.ctypeslib import as_array
from sklearn.neighbors import KNeighborsClassifier as KClass
import pickle
import sys
_sharedX = None
_sharedX2 = None
def para_func(arg):
num, shape, metric, cnum = arg
X = _sharedX
centers = choice(X.shape[0], cnum, False)
mod = KClass(1, metric=metric)
mod.fit(X[centers, :], range(centers.size))
dist, m = mod.kneighbors(X, return_distance=True)
return m
def para_func2(arg):
num, shape, shape2, metric, cnum = arg
X = _sharedX
X2 = _sharedX2
centers = choice(X.shape[0], cnum, False)
mod = KClass(1, metric=metric)
mod.fit(X[centers, :], range(centers.size))
dista1, ma1 = mod.kneighbors(X, return_distance=True)
distb1, mb1 = mod.kneighbors(X2, return_distance=True)
mall = ma1
mall2 = mb1
return mall2, mall
def initShared(X):
global _sharedX
_sharedX = X
def initShared2(X, X2):
global _sharedX
global _sharedX2
_sharedX = X
_sharedX2 = X2
def load_model(model_folder):
model = FastKernel(None, None, None)
with open(model_folder + "/model.cfg", 'r') as f:
model.X = load()
return model
class FastKernel:
def __init__(self, X, y, m=200, h=8, distance='euclidean', sigma=0.01, eps=0.05, num_proc=8):
self.cnum = 3*X.shape[0]//4
self.d = distance
self.X = X
self.y = y
self.num_proc = num_proc
self.v = None
self.m = m
self.h = h
self.sigma = sigma
self.eps = eps
self.cs = None
self.selected = False
# the number of centers for each m
if len(X.shape) == 1:
yt = 1
else:
x, yt = X.shape
if yt is None:
yt = 1
def _select_centers(self, X):
if self.selected:
return
if len(X.shape) == 1:
X = X.reshape((X.shape[0], 1))
self.selected = True
def K(self, X):
# the cluster class assigned to each example use
self._select_centers(X)
if len(X.shape) == 1:
X = X.reshape((X.shape[0], 1))
c = zeros((X.shape[0], self.m))
share_base = Array(ctypes.c_double, X.shape[0]*X.shape[1], lock=False)
share = as_array(share_base)
share = share.reshape(X.shape)
share[:, :] = X
if self.cs is None:
pool = Pool(self.num_proc, maxtasksperchild=50, initializer=initShared, initargs=[share])
cs = pool.imap(para_func, ((i, X.shape, self.d, self.cnum) for i in xrange(self.m)), 10)
self.cs = list(cs)
pool.close()
pool.join()
for i, cv in enumerate(self.cs):
c[:, i] = cv.flatten()
return c
def K2y(self, X, X2, y):
res = zeros(X.shape[0])
if len(X.shape) == 1:
X = X.reshape((X.shape[0], 1))
if len(X2.shape) == 1:
X2 = X2.reshape((X2.shape[0], 1))
share_base = Array(ctypes.c_double, X.shape[0]*X.shape[1], lock=False)
share = as_array(share_base)
share = share.reshape(X.shape)
share[:, :] = X
share2_base = Array(ctypes.c_double, X2.shape[0]*X2.shape[1], lock=False)
share2 = as_array(share2_base)
share2 = share2.reshape(X2.shape)
share2[:, :] = X2
pool = Pool(self.num_proc, maxtasksperchild=50, initializer=initShared2, initargs=[share2, share])
cs = pool.imap(para_func2, ((i, X2.shape, X.shape, self.d, self.cnum) for i in xrange(self.m)), 10)
for c, c2 in cs:
for cls in unique(c):
if cls > -1:
res[c.flatten() == cls] += y[c2.flatten() == cls].sum()
res /= self.m
pool.close()
pool.join()
return res
def K2(self, X, X2):
#if X.ndim == 0:
# X = X.reshape((1, 1))
#if X2.ndim == 0:
# X2 = X2.reshape((1, 1))
if X.ndim == 1:
X = X.reshape((X.shape[0], 1))
if X2.ndim == 1:
X2 = X2.reshape((X2.shape[0], 1))
if X.ndim == 0:
Xsh = 1
Xsh2 = 1
else:
Xsh = X.shape[0]
Xsh2 = X.shape[1]
if X2.ndim == 0:
X2sh = 1
X2sh2 = 1
else:
X2sh = X2.shape[0]
X2sh2 = X2.shape[1]
res = zeros((Xsh, X2sh))
share_base = Array(ctypes.c_double, Xsh*Xsh2, lock=False)
share = as_array(share_base)
share = share.reshape((Xsh, Xsh2))
share[:, :] = X
share2_base = Array(ctypes.c_double, X2sh*X2sh2, lock=False)
share2 = as_array(share2_base)
share2 = share2.reshape(X2.shape)
share2[:, :] = X2
pool = Pool(self.num_proc, maxtasksperchild=50, initializer=initShared2, initargs=[share2, share])
cs = pool.imap(para_func2, ((i, X2.shape, X.shape, self.d, self.cnum) for i in xrange(self.m)), 10)
for c, c2 in cs:
for i, c_v in enumerate(c):
for j, c_v2 in enumerate(c2):
if c_v == c_v2 and c_v != -1:
res[i, j] += 1.
res /= self.m
pool.close()
pool.join()
if X.ndim == 0:
res = res.flatten()
return res
def Ky(self, X, y):
if len(X.shape) == 1:
X = X.reshape((X.shape[0], 1))
res = zeros(X.shape[0])
c = self.K(X)
a = 1.0
#a = 0.95
for i in range(self.m):
for j in unique(c[:, i]):
if j < 0:
continue
ind = where(c[:, i] == j)[0]
for k in ind:
res[k] += (1.-a)*y[k] + a*y[ind].sum()
if (c[:, i] == -1).any():
res[c[:, i] == -1] += y[c[:, i] == -1] # JOE remove if not doing semi
res /= float(self.m)
return res
def B(self, X, y):
if len(X.shape) == 1:
X = X.reshape((X.shape[0], 1))
res = zeros(X.shape[0])
c = self.K(X)
for i in range(self.m):
for j in unique(c[:, i]):
ind = c[:, i] == j
if j < 0:
res[ind] += (1./(1. + self.sigma))*y[ind]
continue
res[ind] += (1./(float(where(ind)[0].size) + self.sigma))*y[ind].sum()
res /= self.m
res = (1./self.sigma)*y - res
return res
def train(self, X, y):
if self.v is None:
A = LinearOperator((X.shape[0], X.shape[0]), lambda x: self.Ky(X, x) + self.sigma*x)
M = LinearOperator((X.shape[0], X.shape[0]), lambda x: self.B(X, x))
self.v, info = cg(A, y, M=M, maxiter=40, tol=self.eps, callback=resid_callback)
def predict_mean(self, X2, X, y):
self.train(X, y)
self.cs = None
res = self.K2y(X2, X, self.v)
return res
def predict_var(self, X2, X, y):
vs = zeros(X2.shape[0])
for i in range(X2.shape[0]):
self.cs = None
# v = self.K2(X2[i, :], X2[i, :])
v = 1. # by definition of partition kernel K(x, x) = 1
A = LinearOperator((X.shape[0], X.shape[0]), lambda x: self.Ky(X, x) + self.sigma*x)
M = LinearOperator((X.shape[0], X.shape[0]), lambda x: self.B(X, x))
self.cs = None
if X2.ndim == 1:
k_star = self.K2(X2[i], X)
else:
k_star = self.K2(X2[i, :], X)
tmp, info = cg(A, k_star.T, M=M, maxiter=40, tol=self.eps)
vs[i] = v - k_star.dot(tmp)
return vs
def likelihood(self, X, y):
self.train(X, y)
A = self.K2(X, X)
res = -.5*y.dot(self.v)-y.shape[0]*log(2.*pi)-.5*log(det(A))
return res
def resid_callback(xk):
res = inspect.currentframe().f_back.f_locals['resid']
with open('residuals.dat', 'a') as f:
f.write('%s\n' % res)