forked from haugjo/fires
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fires_prints.py
558 lines (454 loc) · 25 KB
/
fires_prints.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
import numpy as np
from warnings import warn
from scipy.stats import norm
from sklearn.preprocessing import MinMaxScaler
try:
import cupy as cp
cupy_available = True
except ModuleNotFoundError:
warn("Cupy couldn't be imported, so using FIRES with Multiclass can be slow.")
cupy_available = False
class FIRES:
def __init__(self, n_total_ftr, target_values, mu_init=0, sigma_init=1, penalty_s=0.01, penalty_r=0.01, epochs=1,
lr_mu=0.01, lr_sigma=0.01, scale_weights=True, model='probit', number_monte_carlo_samples=100,
class_probabilities=None):
"""
FIRES: Fast, Interpretable and Robust Evaluation and Selection of features
cite:
Haug et al. 2020. Leveraging Model Inherent Variable Importance for Stable Online Feature Selection.
In Proceedings of the 26th ACM SIGKDD Conference on Knowledge Discovery and Data Mining (KDD ’20),
August 23–27, 2020, Virtual Event, CA, USA.
:param n_total_ftr: (int) Total no. of features
:param target_values: (np.ndarray) Unique target values (class labels) or None for regression
:param mu_init: (int/np.ndarray) Initial importance parameter
:param sigma_init: (int/np.ndarray) Initial uncertainty parameter
:param penalty_s: (float) Penalty factor for the uncertainty (corresponds to gamma_s in the paper)
:param penalty_r: (float) Penalty factor for the regularization (corresponds to gamma_r in the paper)
:param epochs: (int) No. of epochs that we use each batch of observations to update the parameters
:param lr_mu: (float) Learning rate for the gradient update of the importance
:param lr_sigma: (float) Learning rate for the gradient update of the uncertainty
:param scale_weights: (bool) If True, scale feature weights into the range [0,1]
:param model: (str) Name of the base model to compute the likelihood (default is 'probit')
:param number_monte_carlo_samples: (int) amount of monte_carlo samples used for estimate marginal_likelihood
:param class_probability: (np.ndarray) Probabilities of the classes, if not set, all classes are assumend
equaly likely
"""
self.n_total_ftr = n_total_ftr
self.target_values = target_values
self.penalty_s = penalty_s
self.penalty_r = penalty_r
self.epochs = epochs
self.lr_mu = lr_mu
self.lr_sigma = lr_sigma
self.scale_weights = scale_weights
self.model = model
self.n_mc_samples = number_monte_carlo_samples
# Additional model-specific parameters
self.model_param = {}
# Probit model
if self.model == 'probit' and tuple(target_values) != (-1, 1):
if len(np.unique(target_values)) == 2:
self.mu = np.ones(n_total_ftr) * mu_init
self.sigma = np.ones(n_total_ftr) * sigma_init
# Indicates that we need to encode the target variable into {-1,1}
self.model_param['probit'] = True
warn('FIRES WARNING: The target variable will be encoded as: {} = -1, {} = 1'.format(
self.target_values[0], self.target_values[1]))
else:
raise ValueError('The target variable y must be binary.')
# Multinominal logit (softmax) model
if self.model == 'softmax':
if cupy_available:
self.model_param["n_classes"] = len(target_values)
# maybe check for scale of mu init
self.mu = cp.ones(
(n_total_ftr, self.model_param["n_classes"]))*mu_init
self.sigma = cp.ones(
(n_total_ftr, self.model_param["n_classes"]))*sigma_init
if class_probabilities != None:
if sum(class_probabilities) != 1:
raise ValueError("Class probs don't sum up to 1")
elif len(class_probabilities) != self.model_param["n_classes"]:
raise Exception(
"Length of target_values and class_probilities don't match")
else:
self.model_param["class_probs"] = True
self.class_probabilities = class_probabilities
else:
warn("Without Cupy the softmax function can be very slow")
self.model_param["n_classes"] = len(target_values)
# maybe check for scale of mu init
self.mu = np.ones(
(n_total_ftr, self.model_param["n_classes"]))*mu_init
self.sigma = np.ones(
(n_total_ftr, self.model_param["n_classes"]))*sigma_init
if class_probabilities != None:
if sum(class_probabilities) != 1:
raise ValueError("Class probs don't sum up to 1")
elif len(class_probabilities) != self.model_param["n_classes"]:
raise Exception(
"Length of target_values and class_probilities don't match")
else:
self.model_param["class_probs"] = True
self.class_probabilities = class_probabilities
if self.model == "regression":
self.mu = np.ones(n_total_ftr) * mu_init
self.sigma = np.ones(n_total_ftr) * sigma_init
# ### ADD YOUR OWN MODEL PARAMETERS HERE #######################################
# if self.model == 'your_model':
# self.model_param['your_model'] = {}
################################################################################
def weigh_features(self, x, y):
"""
Compute feature weights, given a batch of observations and corresponding labels
:param x: (np.ndarray) Batch of observations
:param y: (np.ndarray) Batch of labels
:return: feature weights
:rtype np.ndarray
"""
# Update estimates of mu and sigma given the predictive model
if self.model == 'probit':
self.__probit(x, y)
elif self.model == 'softmax':
if cupy_available:
self.__softmax_cp(x, y)
else:
self.__softmax_np(x, y)
elif self.model == "regression":
self.__regression(x, y)
# ### ADD YOUR OWN MODEL HERE ##################################################
# elif self.model == 'your_model':
# self.__yourModel(x, y)
################################################################################
else:
raise NotImplementedError('The given model name does not exist')
# Limit sigma to range [0, inf]
if np.sum(self.sigma < 0) > 0:
self.sigma[self.sigma < 0] = 0
warn(
'Sigma has automatically been rescaled to [0, inf], because it contained negative values.')
# Compute feature weights
return self.__compute_weights()
def __probit(self, x, y):
"""
Update the distribution parameters mu and sigma by optimizing them in terms of the (log) likelihood.
Here we assume a Bernoulli distributed target variable. We use a Probit model as our base model.
This corresponds to the FIRES-GLM model in the paper.
:param x: (np.ndarray) Batch of observations (numeric values only, consider normalizing data for better results)
:param y: (np.ndarray) Batch of labels: type binary, i.e. {-1,1} (bool, int or str will be encoded accordingly)
"""
for epoch in range(self.epochs):
# Shuffle the observations
random_idx = np.random.permutation(len(y))
x = x[random_idx]
y = y[random_idx]
# Encode target as {-1,1}
if 'probit' in self.model_param:
y[y == self.target_values[0]] = -1
y[y == self.target_values[1]] = 1
# Iterative update of mu and sigma
try:
# Helper functions
dot_mu_x = np.dot(x, self.mu)
rho = np.sqrt(1 + np.dot(x**2, self.sigma**2))
# Gradients
nabla_mu = norm.pdf(y/rho * dot_mu_x) * (y/rho * x.T)
nabla_sigma = norm.pdf(
y/rho * dot_mu_x) * (- y/(2 * rho**3) * 2 * (x**2 * self.sigma).T * dot_mu_x)
# Marginal Likelihood
marginal = norm.cdf(y/rho * dot_mu_x)
# Update parameters
self.mu += self.lr_mu * np.mean(nabla_mu / marginal, axis=1)
self.sigma += self.lr_sigma * \
np.mean(nabla_sigma / marginal, axis=1)
except TypeError as e:
raise TypeError(
'All features must be a numeric data type.') from e
def __softmax_cp(self, x, y):
"""
Update the distribution parameters mu and sigma by optimizing them in terms of the (log) likelihood.
Here we assume a multinominal distributed target variable. We use a Multinominal model as our base model.
Funciton with cupy functions.
:param x: (np.ndarray) Batch of observations (numeric values only, consider normalizing data for better results)
:param y: (np.ndarray) Batch of labels: type integer e.g. 0,1,2,3,4 etc.
"""
print("one call")
batch_size = len(y)
print(batch_size)
if len(x.shape) != 2:
x = x.reshape(1,len(x))
observed_classes = np.unique(y)
for obs_class in observed_classes:
observations_index = np.where(y == obs_class)[0]
x_obs = cp.array(x[observations_index])
n_obs = len(x_obs)
print("For class {} there are {} obs".format(obs_class, n_obs))
for epoch in range(self.epochs):
# Iterative update of mu and sigma
try:
# o number of obs, l number of samples, j features,
# c classes
# r shape: oxlxjxc
r = cp.random.randn(n_obs, self.n_mc_samples,
self.n_total_ftr,
self.model_param["n_classes"])
print("r shape{}".format(r.shape))
print(cp.max(r))
print(cp.min(r))
# theta shape: oxlxjxc
theta = (r * self.sigma + self.mu)
print("theta:")
print(cp.max(theta))
print(cp.min(theta))
# eta shape: oxlxc
# multiply all ftr_cols with given ftr_vector x
eta = cp.einsum("oljc,oj->oljc", theta, x_obs)
# sum up all theta^cl_j * x_tj so we got l samples
# for all c classes
eta = cp.einsum("oljc->olc", eta)
print("theta times x")
print(eta.shape)
print(cp.max(eta))
print(cp.min(eta))
print(cp.min(cp.amax(eta, axis=2)))
# get a for numerical stability, shape oxl
a = cp.amax(eta, axis=2) * -1
print("mean:")
test = cp.mean(eta, axis=2)
print(test.shape)
print(cp.max(test))
print(cp.min(test))
print("median:")
test = cp.median(eta, axis=2)
print(test.shape)
print(cp.max(test))
print(cp.min(test))
print("a shape {}".format(a.shape))
print(cp.max(a))
print(cp.min(a))
eta = cp.einsum("olc->col", eta) + a
eta = cp.einsum("col->olc", eta)
print("eta:")
#there should be a zero for each ???????
print(eta.shape)
#print(cp.amax(eta, axis=2))
print(cp.any(cp.amax(eta, axis=2) != 0.0))
print(cp.max(eta))
print(cp.min(eta))
# final eta with a and exponential
eta = cp.exp(eta)
print("exp eta:")
print(cp.max(eta))
print(cp.min(eta))
#print(eta[:,:,obs_class])
# eta_sum shape: oxl
eta_sum = cp.einsum("olc->ol", eta)
print("eta_sum")
print(eta_sum.shape)
print(cp.max(eta_sum))
print(cp.min(eta_sum))
# calculate softmax(k, ...) for all classes k
# divide all etas by eta_sum
softmax_all = np.einsum("olc,ol->olc", eta, (1/eta_sum))
print("softmax_all")
print(softmax_all.shape)
print(cp.max(softmax_all))
print(cp.min(softmax_all))
print(cp.isnan(softmax_all).any())
print(softmax_all[:,:,obs_class].shape)
# marginal shape: o
marginal = cp.einsum("ol->o",
softmax_all[:,:,obs_class]) / \
self.n_mc_samples
print("marginal")
print(marginal.shape)
print(cp.max(marginal))
print(cp.min(marginal))
print(cp.isnan(marginal).any())
# calculate softmax derivative to theta:
softmax_c = softmax_all[:,:,obs_class]
# first calculate derivative for all as k != c
softmax_derivative = -1 * cp.einsum("oj,ol,olc->oljc",
(x_obs),
softmax_c,
softmax_all)
# then for observed class c
softmax_derivative_c = cp.einsum("oj,ol,ol->olj",
x_obs,
softmax_c,
(1-softmax_c))
softmax_derivative[:,:,:,obs_class] = \
softmax_derivative_c
print("softmax_derivative")
print(softmax_derivative.shape)
print(cp.max(softmax_derivative))
print(cp.min(softmax_derivative))
print(cp.isnan(softmax_derivative).any())
nabla_mu = cp.einsum("oljc->ojc", softmax_derivative) /\
self.n_mc_samples
nabla_mu = cp.einsum("ojc->jco", nabla_mu)
nabla_mu = cp.einsum("jco->jc", (nabla_mu / marginal))
nabla_mu = nabla_mu / batch_size
self.mu += self.lr_mu * nabla_mu
nabla_sigma = cp.einsum("oljc,oljc->ojc",
softmax_derivative,r) / \
self.n_mc_samples
nabla_sigma = cp.einsum("ojc->jco", nabla_sigma)
nabla_sigma = cp.einsum("jco->jc", (nabla_sigma / marginal))
nabla_sigma = nabla_sigma / batch_size
self.sigma += self.lr_sigma * nabla_sigma
print(cp.max(self.mu))
print(cp.max(self.sigma))
except TypeError as e:
raise TypeError('All features must be a numeric data type.') from e
def __softmax_np(self, x, y):
"""
Update the distribution parameters mu and sigma by optimizing them in terms of the (log) likelihood.
Here we assume a multinominal distributed target variable. We use a Multinominal model as our base model.
This is the function which is used if cupy isn't installed, not recommended!
:param x: (np.ndarray) Batch of observations (numeric values only, consider normalizing data for better results)
:param y: (np.ndarray) Batch of labels: type integer e.g. 0,1,2,3,4 etc.
"""
if len(x.shape) != 2:
x = x.reshape(1,len(x))
observed_classes = np.unique(y)
for obs_class in observed_classes:
observations_index = np.where(y == obs_class)[0]
x_obs = np.array(x[observations_index])
n_obs = len(x_obs)
#print("obs_class: {}, n obs: {}".format(obs_class, n_obs))
for epoch in range(self.epochs):
# Iterative update of mu and sigma
try:
# o number of obs, l number of samples, j features,
# c classes
# r shape: oxlxjxc
r = np.random.randn(n_obs, self.n_mc_samples,
self.n_total_ftr,
self.model_param["n_classes"])
# theta shape: oxlxjxc
theta = (r * self.sigma + self.mu)
# eta shape: oxlxc
# multiply all ftr_cols with given ftr_vector x
eta = np.einsum("oljc,oj->oljc", theta, x_obs)
# sum up all theta^cl_j * x_tj so we got l samples
# for all c classes
eta = np.einsum("oljc->olc", eta)
# get a for numerical stability, shape oxl
a = np.amax(eta, axis=2) * -1
eta = np.einsum("olc->col", eta) + a
eta = np.einsum("col->olc", eta)
# final eta with a and exponential
eta = np.exp(eta)
# eta_sum shape: oxl
eta_sum = np.einsum("olc->ol", eta)
# calculate softmax(k, ...) for all classes k
# divide all etas by eta_sum
softmax_all = np.einsum("olc,ol->olc", eta, (1/eta_sum))
# marginal shape: o
marginal = np.einsum("ol->o",
softmax_all[:,:,obs_class]) / \
self.n_mc_samples
# calculate softmax derivative to theta:
softmax_c = softmax_all[:,:,obs_class]
# first calculate derivative for all as k != c
softmax_derivative = -1 * np.einsum("oj,ol,olc->oljc",
(x_obs),
softmax_c,
softmax_all)
# then for observed class c
softmax_derivative_c = np.einsum("oj,ol,ol->olj",
x_obs,
softmax_c,
(1-softmax_c))
softmax_derivative[:,:,:,obs_class] = \
softmax_derivative_c
nabla_mu = np.einsum("oljc->ojc", softmax_derivative) /\
self.n_mc_samples
nabla_sigma = np.einsum("oljc,oljc->ojc",
softmax_derivative,r) / \
self.n_mc_samples
nabla_mu = np.einsum("ojc->jco", nabla_mu)
self.mu += self.lr_mu * \
np.einsum("jco->jc",
(nabla_mu/ marginal))
nabla_sigma = np.einsum("ojc->jco", nabla_sigma)
self.sigma += self.lr_sigma * \
np.einsum("jco->jc",
(nabla_sigma /
marginal))
except TypeError as e:
raise TypeError('All features must be a numeric data type.') from e
def __regression(self, x, y):
"""
Update the distribution parameters mu and sigma by optimizing them in terms of the likelihood.
Here we assume a normal distributed target variable. We use a identity model as our base model.
:param x: (np.ndarray) Batch of observations (numeric values only, consider normalizing data for better results)
:param y: (np.ndarray) Batch of labels: type float
"""
for epoch in range(self.epochs):
# Shuffle the observations
try:
n_obs = len(y)
random_idx = np.random.permutation(len(y))
x = x[random_idx]
y = y[random_idx]
except:
n_obs = 1
x.reshape(1, len(x))
# Iterative update of mu and sigma
try:
# has shape o: observations, l: samples, j: features
r = np.random.randn(n_obs, self.n_mc_samples, self.n_total_ftr)
theta = np.einsum("olj,j->olj", r, self.sigma) + self.mu
marginal = np.einsum("olj,oj->olj", theta, x) # theta *x
marginal = np.einsum("olj->o", marginal) / self.n_mc_samples
# calculate derivatives
nabla_mu = x
nabla_sigma = x * (np.einsum("olj->oj", r) /
self.n_mc_samples)
# update mu and sigma
self.mu += self.lr_mu * np.mean(nabla_mu.T / marginal, axis=1)
self.sigma += self.lr_sigma * \
np.mean(nabla_sigma.T / marginal, axis=1)
except TypeError as e:
raise TypeError('All features must be a numeric data type.') from e
'''
# ### ADD YOUR OWN MODEL HERE #################################################
def __yourModel(self):
"""
Your own model description.
:param x: (np.ndarray) Batch of observations
:param y: (np.ndarray) Batch of labels
"""
gradientMu = yourFunction() # Gradient of the (log) likelihood with respect to mu
gradientSigma = yourFunction() # Gradient of the (log) likelihood with respect to sigma
self.mu += self.lr_mu * gradientMu
self.sigma += self.lr_sigma * gradientSigma
################################################################################
'''
def __compute_weights(self):
"""
Compute optimal weights according to the objective function proposed in the paper.
We compute feature weights in a trade-off between feature importance and uncertainty.
Thereby, we aim to maximize both the discriminative power and the stability/robustness of feature weights.
:return: feature weights
:rtype np.ndarray
"""
if type(self.mu) == cp.core.core.ndarray:
mu = cp.asnumpy(self.mu)
sigma = cp.asnumpy(self.sigma)
else:
mu, sigma = self.mu, self.sigma
if len(mu.shape) == 2: # multinominal case
if "class_probs" in self.model_param:
mu = np.sum(mu * self.class_probabilities, axis=1)
sigma = np.sum(sigma * self.class_probabilities, axis=1)
else:
mu = np.mean(mu, axis=1)
sigma = np.mean(sigma, axis=1)
# Compute optimal weights
weights = (mu**2 - self.penalty_s * sigma**2) / (2 * self.penalty_r)
if self.scale_weights: # Scale weights to [0,1]
weights = MinMaxScaler().fit_transform(weights.reshape(-1, 1)).flatten()
return weights