-
Notifications
You must be signed in to change notification settings - Fork 2
/
algorithms.py
128 lines (97 loc) · 4.43 KB
/
algorithms.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
import math
from baseline_classes import *
import numpy as np
import random
class HumanMachine:
def __init__(self, data_dict):
self.X = data_dict['X']
self.Y = data_dict['Y']
self.c = data_dict['c']
self.Pr_H = data_dict['Pr_H']
self.Pr_M = data_dict['Pr_M']
self.Pr_M_Alg = data_dict['Pr_M_Alg']
self.dim = self.X.shape[1]
self.n = self.X.shape[0]
self.V = np.arange(self.n)
def get_Sc(self, subset):
return np.array([int(i) for i in self.V if i not in subset])
def get_added(self, subset, elm):
return np.concatenate((subset, np.array([int(elm)])), axis=0)
def set_param(self, lamb, K):
self.lamb = lamb
self.K = K
def stochastic_distort_greedy(self, g, K, gamma, epsilon, svm_type):
c_mod = C({'X': self.X, 'Y': self.Y, 'c': self.c, 'lamb': self.lamb, 'svm_type': svm_type})
subset = np.array([]).astype(int)
g.reset()
s = int(math.ceil(self.n * np.log(float(1) / epsilon) / float(K)))
print 'subset_size', s, 'K-->', K, ', n --> ', self.n
for itr in range(K):
frac = (1 - gamma / float(K)) ** (K - itr - 1)
subset_c = self.get_Sc(subset)
if s < subset_c.shape[0]:
subset_chosen = np.array(random.sample(subset_c, s))
else:
subset_chosen = subset_c
c_mod_inc = c_mod.get_inc_arr(subset, rest_flag=True, subset_rest=subset_chosen)
g_inc_arr, subset_c_ret = g.get_inc_arr(subset, rest_flag=True, subset_rest=subset_chosen)
g_pos_inc = g_inc_arr.flatten()
inc_vec = frac * g_pos_inc - c_mod_inc
if np.max(inc_vec) <= 0:
print 'no increment'
return subset
sel_ind = np.argmax(inc_vec)
elm = subset_chosen[sel_ind]
subset = self.get_added(subset, elm)
g.update_data_str(elm)
return subset
def distort_greedy(self, g, K, gamma, svm_type):
c_mod = C({'X': self.X, 'Y': self.Y, 'c': self.c, 'lamb': self.lamb, 'svm_type': svm_type})
subset = np.array([]).astype(int)
g.reset()
for itr in range(K):
frac = (1 - gamma / float(K)) ** (K - itr - 1)
subset_c = self.get_Sc(subset)
c_mod_inc = c_mod.get_inc_arr(subset).flatten()
g_inc_arr, subset_c_ret = g.get_inc_arr(subset)
g_pos_inc = g_inc_arr.flatten() # + c_mod_inc
inc_vec = frac * g_pos_inc - c_mod_inc
if np.max(inc_vec) <= 0:
print 'no increment'
return subset
sel_ind = np.argmax(inc_vec)
elm = subset_c[sel_ind]
subset = self.get_added(subset, elm)
g.update_data_str(elm)
return subset
def gamma_sweep_distort_greedy(self, svm_type, delta=0.001, T=1, flag_stochastic=None):
g = G({'X': self.X, 'Y': self.Y, 'c': self.c, 'lamb': self.lamb, 'svm_type': svm_type})
subset = {}
G_subset = []
gamma = 0.01
for t in range(T):
if flag_stochastic:
subset_sel = self.stochastic_distort_greedy(g, self.K, gamma, delta, svm_type=svm_type)
else:
subset_sel = self.distort_greedy(g, self.K, gamma, svm_type=svm_type)
subset[str(t)] = subset_sel
G_subset.append(g.eval(subset_sel))
gamma = gamma * (1 - delta)
max_set_ind = np.argmax(np.array(G_subset))
return subset[str(max_set_ind)]
def triage_subset(self, triage_type):
triage_obj = Triage({'X': self.X, 'Y': self.Y, 'c': self.c, 'lamb': self.lamb,
'Pr_H': self.Pr_H, 'Pr_M': self.Pr_M, 'Pr_M_Alg': self.Pr_M_Alg})
return triage_obj.get_subset(self.K, triage_type)
def algorithms(self, param, optim):
self.set_param(param['lamb'], int(param['K'] * self.n))
if optim == 'distort_greedy':
subset = self.gamma_sweep_distort_greedy(svm_type=param['svm_type'], T=1)
if optim == 'stochastic_distort_greedy':
subset = self.gamma_sweep_distort_greedy(flag_stochastic=True, T=3, svm_type=param['svm_type'])
if optim == 'kl_triage_estimated':
subset = self.triage_subset(triage_type='estimated')
if optim == 'kl_triage_Alg':
subset = self.triage_subset(triage_type='Alg')
res = {'subset': subset}
return res