-
Notifications
You must be signed in to change notification settings - Fork 1
/
kliep.py
318 lines (257 loc) · 11.4 KB
/
kliep.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
from __future__ import division
import numpy as np
import math as m
class Kliep(object):
def __init__(self, kliepParEta, kliepParLambda, kliepParB, kliepParThreshold, kliepDefSigma=0.01):
self.kliepDefSigma = kliepDefSigma
self.kliepParEta = kliepParEta
self.kliepParLambda = kliepParLambda
self.kliepParB = kliepParB
self.kliepParThreshold = kliepParThreshold
def pdf_gaussian(self, x, mu):
x_size = np.shape(x)
d = x_size[0]
nx = x_size[len(x_size) - 1]
tmp = (x - np.tile(mu, (1, nx)))/(np.sqrt(2) * np.tile(self.kliepDefSigma, (1, nx)))
denom = m.pow((2 * m.pi), (-1 / 2))
px = np.exp(-np.power(tmp, 2, dtype='float64'))*(denom / self.kliepDefSigma)
return px
def kernel_Gaussian(self, x, c):
x_size = np.shape(x)
dx = x_size[0]
nx = x_size[len(x_size) - 1]
c_size = np.shape(c)
dc = c_size[0]
nc = c_size[len(c_size) - 1]
x2 = np.power(x, 2, dtype=np.float)
c2 = np.power(c, 2, dtype=np.float)
# if the array is 1D, need to add an axis first before doing transpose.
distance2 = np.tile(c2, (nx, 1)) + np.tile(x2.T, (1, nc)) - (2 * x.T * c)
X = np.exp(-distance2 / (2 * m.pow(self.kliepDefSigma, 2)), dtype='float64')
return X
"""
- find kernel gaussians for multi dim x
- in x and c, each column represents one data point
"""
def kernel_Gaussian_mdim(self, x, c):
x_size = np.shape(x)
dx = x_size[0]
nx = x_size[len(x_size) - 1]
c_size = np.shape(c)
dc = c_size[0]
nc = c_size[len(c_size) - 1]
distance2 = None
for i in range(0, nx):
# though we extract a column, it become a row matrix in python.
x_col_i = x[:, i]
dist_x_col_i_c = self.distance(x_col_i[np.newaxis].T, dx, 1, c, dc, nc)
if distance2 is None:
# since X will have more rows, so while copying need to add a new axis also.
distance2 = dist_x_col_i_c[np.newaxis]
else:
distance2 = np.append(distance2, dist_x_col_i_c[np.newaxis], axis=0)
X = np.exp(-distance2 / (2 * m.pow(self.kliepDefSigma, 2)), dtype='float64')
return X
def kernel_Gaussian_mdim_choose_sigma(self, x, c, sigma):
x_size = np.shape(x)
dx = x_size[0]
nx = x_size[len(x_size) - 1]
c_size = np.shape(c)
dc = c_size[0]
nc = c_size[len(c_size) - 1]
distance2 = None
for i in range(0, nx):
# though we extract a column, it become a row matrix in python.
x_col_i = x[:, i]
dist_x_col_i_c = self.distance(x_col_i[np.newaxis].T, dx, 1, c, dc, nc)
if distance2 is None:
# since X will have more rows, so while copying need to add a new axis also.
distance2 = dist_x_col_i_c[np.newaxis]
else:
distance2 = np.append(distance2, dist_x_col_i_c[np.newaxis], axis=0)
X = np.exp(-distance2/(2*m.pow(sigma, 2)), dtype='float64')
return X
"""
x_col_i represents ith instance in row matrix format.
c represents the selected test points. ith column represents ith selected test point.
distance returns a row matrix, dimension 1*c_ncol, where (1,i) element is the distance between the instance
represented by x_col_i and ith instance in c, i.e., ith column in c
"""
def distance(self, x_col_i, x_col_i_nrow, x_col_i_ncol, c, c_nrow, c_ncol):
dist_tmp = np.power(np.tile(x_col_i, (1, c_ncol)) - c, 2, dtype='float64')
# need to do column-wise sum
dist_2 = np.sum(dist_tmp, axis=0, dtype='float64')
return dist_2
def KLIEP_projection(self, alpha, Xte, meanDistSrcData, c):
# b_alpha = np.sum(b*alpha)
b_alpha = np.dot(meanDistSrcData.T, alpha)
alpha = alpha + meanDistSrcData * (1 - b_alpha) * np.linalg.pinv(c, rcond=1e-20)
# alpha = np.max(0,alpha[np.newaxis])
alpha[alpha < 0] = 0
b_alpha_new = np.dot(meanDistSrcData.T, alpha)
alpha = alpha * np.linalg.pinv(b_alpha_new, rcond=1e-20)
Xte_alpha = np.dot(Xte, alpha)
Xte_alpha[(Xte_alpha-0)<0.00001] = 0.00001
#Xte_alpha_no_zeros = np.array([(1/100) if (h - 0) < 0.00001 else h for h in Xte_alpha])
log_xte_alpha = np.log(Xte_alpha, dtype='float64')
score = np.mean(log_xte_alpha, dtype='float64')
return alpha, Xte_alpha, score
def KLIEP_projection_wo_score(self, alpha, meanDistSrcData, c):
b_alpha = np.dot(meanDistSrcData.T, alpha)
alpha = alpha + meanDistSrcData * (1 - b_alpha) * np.linalg.pinv(c, rcond=1e-20)
# alpha = np.max(0,alpha[np.newaxis])
alpha[alpha < 0] = 0
b_alpha_new = np.dot(meanDistSrcData.T, alpha)
alpha = alpha * np.linalg.pinv(b_alpha_new, rcond=1e-20)
return alpha
def KLIEP_learning(self, mean_X_de, X_nu):
X_nu_size = np.shape(X_nu)
n_nu = X_nu_size[0]
nc = X_nu_size[len(X_nu_size) - 1]
max_iteration = 100
epsilon_list = np.power(10, range(3, -4, -1), dtype='float64')
# c = sum(np.power(mean_X_de, 2, dtype=np.float))
c = np.dot(mean_X_de.T, mean_X_de)
alpha = np.ones((nc, 1))
[alpha, X_nu_alpha, score] = self.KLIEP_projection(alpha, X_nu, mean_X_de, c)
for epsilon in epsilon_list:
for iteration in range(1, max_iteration):
alpha_tmp = alpha + (epsilon * np.dot(X_nu.T, (1 / X_nu_alpha)))
[alpha_new, X_nu_alpha_new, score_new] = self.KLIEP_projection(alpha_tmp, X_nu, mean_X_de, c)
if (score_new - score) <= 0:
break
score = score_new
alpha = alpha_new
X_nu_alpha = X_nu_alpha_new
return alpha
def KLIEP(self, srcData, trgData):
srcDataSize = np.shape(srcData)
nRowSrcData = srcDataSize[0]
nColSrcData = srcDataSize[len(srcDataSize) - 1]
trgDataSize = np.shape(trgData)
nRowTrgData = trgDataSize[0]
nColTrgData = trgDataSize[len(trgDataSize) - 1]
b = min(self.kliepParB, nColTrgData)
#rand_index = np.random.permutation(nColTrgData)
# rand_index = genfromtxt('rand_index.csv', delimiter=',')-1
#refPoints = trgData[:, rand_index[0:b].tolist()]
refPoints = trgData[:, -b:]
######### Computing the final solution wh_x_de
kernelMatSrcData = self.kernel_Gaussian_mdim(srcData, refPoints)
kernelMatTrgData = self.kernel_Gaussian_mdim(trgData, refPoints)
meanDistSrcData = np.transpose(np.mean(kernelMatSrcData, 0)[np.newaxis])
alphah = self.KLIEP_learning(meanDistSrcData, kernelMatTrgData)
# wh_x_nu = np.transpose(np.dot(X_nu, alphah))
#weightTrgData = np.dot(kernelMatTrgData, alphah)
return alphah, kernelMatSrcData, kernelMatTrgData, refPoints
def chooseSigma(self, srcData, trgData, fold=5):
srcDataSize = np.shape(srcData)
nRowSrcData = srcDataSize[0]
nColSrcData = srcDataSize[len(srcDataSize) - 1]
trgDataSize = np.shape(trgData)
nRowTrgData = trgDataSize[0]
nColTrgData = trgDataSize[len(trgDataSize) - 1]
print "Choose Sigma"
####### Choosing Gaussian kernel center `x_ce'
# rand_index = np.random.permutation(n_nu)
b = min(self.kliepParB, nColTrgData)
# undo after finishing debug
# x_ce = np.array(x_nu)
# np.random.shuffle(x_ce)
rand_index = np.random.permutation(nColTrgData)
# rand_index = genfromtxt('rand_index.csv', delimiter=',')-1
refPoints = trgData[:, rand_index[0:b].tolist()]
####### Searching Gaussian kernel width `sigma_chosen'
sigma = 10
score = -float("inf")
epsilon_list = range(int(m.log10(sigma)) - 1, -2, -1)
for epsilon in epsilon_list:
for iteration in range(1, 10, 1):
sigma_new = sigma - m.pow(10, epsilon)
print "sigma = ", sigma, " epsilon=", epsilon, "sigma_new=", sigma_new
# undo after finishing debug
cv_index = np.random.permutation(nColTrgData)
# cv_index = genfromtxt('cv_index' + str(epsilon) + '_' + str(iteration) + '.csv', delimiter=',')-1
cv_split = np.floor(np.divide(np.multiply(range(0, nColTrgData), fold), nColTrgData)) + 1
score_new = 0
kernelMatSrcData = self.kernel_Gaussian_mdim_choose_sigma(srcData, refPoints, sigma_new)
kernelMatTrgData = self.kernel_Gaussian_mdim_choose_sigma(trgData, refPoints, sigma_new)
# axis = 0 means column-wise mean
meanDistSrcData = np.transpose(np.mean(kernelMatSrcData, axis=0)[np.newaxis])
for i in range(1, fold + 1, 1):
alpha_cv = self.KLIEP_learning(meanDistSrcData, kernelMatTrgData[cv_index[cv_split != i].tolist(), :])
wh_cv = np.dot(kernelMatTrgData[cv_index[cv_split == i].tolist(), :], alpha_cv)
score_new = score_new + (np.mean(np.log(wh_cv), dtype=np.float)/fold)
if (score_new - score) <= 0:
break
score = score_new
sigma = sigma_new
print "score=", score, " sigma=", sigma, "epsilon=", epsilon, "iteration=", iteration
print "Sigma = ", str(sigma)
return sigma
def changeDetection(self, trgData, refPointsOld, alphahOld, refPointsNew, alphahNew, kernelMatTrgDataNew=None):
if len(np.shape(trgData)) == 1:
trgData = trgData[np.newaxis]
if kernelMatTrgDataNew is None:
kernelMatTrgDataNew = self.kernel_Gaussian_mdim(trgData, refPointsNew)
kernelMatTrgDataOld = self.kernel_Gaussian_mdim(trgData, refPointsOld)
weightTrgDataNew = self.calcInstanceWeights(kernelMatTrgDataNew, alphahNew)
weightTrgDataNew[(weightTrgDataNew - 0) < 0.00001] = 0.00001
#weightTrgDataNew_no_zeros = np.array([float(0.0001) if (h-0)<0.00001 else h for h in weightTrgDataNew[0]])
weightTrgDataOld = self.calcInstanceWeights(kernelMatTrgDataOld, alphahOld)
weightTrgDataOld[(weightTrgDataOld - 0) < 0.00001] = 0.00001
#weightTrgDataOld_no_zeros = np.array([float(0.0001) if (h - 0) < 0.00001 else h for h in weightTrgDataOld[0]])
l_ratios = weightTrgDataNew/weightTrgDataOld
lnWeightTrgData = np.log(l_ratios, dtype='float64')
changeScore = np.sum(lnWeightTrgData, dtype='float64')
#print "ChangeScore=", changeScore
return changeScore > self.kliepParThreshold, changeScore, kernelMatTrgDataNew
"""
updateAlpha parameters:
srcData - contains instances from src stream
trgData - contains instances from trg stream, including the new point
newTrgPoint - is the new point, last column of trgData should match with newTrgPoint
alphah - most recent set of alpha
"""
def updateAlpha(self, srcData, trgData, newTrgPoint, refPoints, alphah, kernelMatSrcData=None):
if len(np.shape(srcData)) == 1:
srcData = srcData[np.newaxis]
if len(np.shape(trgData)) == 1:
trgData = trgData[np.newaxis]
# calculate c
trgDataSize = np.shape(trgData)
nRowTrgData = trgDataSize[0]
nColTrgData = trgDataSize[len(trgDataSize) - 1]
if newTrgPoint.ndim == 1:
newTrgPoint = newTrgPoint[np.newaxis]
kernelNewTrgPoint = self.kernel_Gaussian_mdim(newTrgPoint, refPoints)
# alphah is a column vector, each row of kernel_x_new represents distances for one data point
c = np.dot(kernelNewTrgPoint, alphah)
# update alpha values
tmp = 1 - (self.kliepParEta * self.kliepParLambda)
alphah = alphah * tmp
alphah = alphah[1:, :]
alphah = np.append(alphah, self.kliepParEta/c, axis=0)
alphah, kernelMatSrcData = self.satConstraints(srcData, trgData, refPoints, alphah, kernelMatSrcData)
return alphah, kernelMatSrcData
def satConstraints(self, srcData, trgData, refPoints, alphah, kernelMatSrcData=None):
trgDataSize = np.shape(trgData)
nRowTrgData = trgDataSize[0]
nColTrgData = trgDataSize[len(trgDataSize) - 1]
if kernelMatSrcData is None:
kernelMatSrcData = self.kernel_Gaussian_mdim(srcData, refPoints)
meanDistSrcData = self.colWiseMeanTransposed(kernelMatSrcData)
# c = sum(np.power(mean_X_de, 2, dtype=np.float))
c = np.dot(meanDistSrcData.T, meanDistSrcData)
alphah = self.KLIEP_projection_wo_score(alphah, meanDistSrcData, c)
return alphah, kernelMatSrcData
"""
returns transpose of matrix resulting from taking column wise mean of mat.
"""
def colWiseMeanTransposed(self, mat):
return np.transpose(np.mean(mat, 0)[np.newaxis])
"""
Returns instance weights as a row vector
"""
def calcInstanceWeights(self, kernelMat, alphah):
return np.dot(kernelMat, alphah).T