-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadaptive_alg.py
216 lines (184 loc) · 5.99 KB
/
adaptive_alg.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
import numpy as np
import pandas as pd
import sklearn as sk
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import r2_score
from statsmodels.api import OLS
import itertools
import math
import random
import scipy.sparse
from sklearn.externals import joblib
from sklearn.externals.joblib.parallel import Parallel, delayed
import multiprocessing
import boto3, botocore
import time
import warnings
from sklearn import preprocessing
warnings.filterwarnings("ignore")
# params
# can choose bayes_synth, linear_synth or log_synth for dat
dat = 'linear'
print(dat)
data_folder = 'data/'
isParallel = False
ep = 0.05
alpha = 0.95
# (number of elements to select, number of rounds, guess for OPT)
val_list = [(40,2,.75)]
num_cores = multiprocessing.cpu_count()
print('num_cores:')
print(num_cores)
print('Fetching Files')
if dat == 'linear':
data = np.loadtxt(open(data_folder + "slice_localization_data.csv", "rb"), delimiter=",", skiprows=1)
y_cat = data[:,-1]
X1 = data[:,:-1]
all_predictors = list(range(X1.shape[1]))
elif dat == 'log':
X1 = np.load(data_folder + 'synth_log_X.npy')
y_cat = np.load(data_folder + 'synth_log_y.npy')
all_predictors = list(range(X1.shape[1]))
elif dat == 'bayes':
X1 = np.load(data_folder + 'synth_bayes_X.npy')
all_predictors = list(range(X1.shape[1]))
X = preprocessing.normalize(X1, norm='l2', axis=0)
d = X1.shape[0]
Lambda = np.eye(d)
sigma = 1
print('Finished Parsing Files')
print('Objective function')
if dat == 'linear':
def obj_fun(x_t, y_t):
model = OLS(y_t, x_t).fit()
pred = model.predict(x_t)
return r2_score(y_t, pred)
elif dat == 'bayes':
def obj_fun(S):
trace_sig = np.trace(np.linalg.inv(Lambda))
X1 = X[:,S]
return trace_sig - np.trace(np.linalg.inv(Lambda + sigma**(-2)*np.matmul(X1, X1.T)))
elif dat == 'log':
def log_likelihood(features, target, weights):
scores = np.dot(features, weights)
ll = np.sum( target*scores - np.log(1 + np.exp(scores)) )
return ll
# we use log likelihood to maximize but plot classification rate
def obj_fun(x_t, y_t):
logitm = LogisticRegression(C = 1e15,solver='lbfgs')
logitm.fit (x_t, y_t)
y_logit = logitm.predict(x_t)
class_rate = np.sum(y_logit == y_t)/len(y_t)
return 1/(-1*log_likelihood(x_t, y_t, logitm.coef_[0]))
def get_class_rate(x_t, y_t):
# Create logistic regression object
logitm = LogisticRegression(C = 1e15,solver='lbfgs')
logitm.fit (x_t, y_t)
y_logit = logitm.predict(x_t)
class_rate = np.sum(y_logit == y_t)/len(y_t)
return class_rate
if 'bayes' in dat:
def oracle(cols):
if cols == []:
return 0.0
else:
r2 = obj_fun(cols)
return r2
else:
# given set of features, return obj function
def oracle(cols):
if cols == []:
return 0.0
else:
r2 = obj_fun(X1[:, cols], y_cat)
return r2
# get random sample of size num from array X
def randomSample(X, num):
# if not enough elements, just return X
X_copy = X.copy()
if len(X_copy) < int(num):
R = X_copy
else:
R = [X_copy[i] for i in sorted(random.sample(range(len(X_copy)), int(num)))]
return R
# estimate set value by sampling m times
def estimateSet(X, S, k, r, m=5):
est = 0
fS = oracle(S)
# repeat m times
for it in range(m):
# sample size k/r m times
R = randomSample(X, k/r)
est += oracle(R + S)
return (est - m*fS)/m
# estimate marginal contribution by sampling m times
def estimateMarginal(X, S, a, k, r, m=5):
est = 0
# repeat m times
for it in range(m):
# if there are not enough elements
R = randomSample(X, k/r)
marg1 = oracle(R+S+[a])
if a in R:
R.remove(a)
marg2 = oracle(S + R)
else:
marg2 = oracle(S + R)
est += marg1 - marg2
return est/m
def union(A, B):
return list(set(A + B))
# DASH
def dash(k, r, ep, OPT, X, alpha, debug=True, parallel=False):
m = 5
S = []
# cache values for plotting
y_adap = []
# for r iterations
for i in range(r):
T = []
print(i)
fS = oracle(S)
fST = oracle(union(S,T))
while ((fST - fS) < (ep/20)*(OPT - fS)) and (len(union(S,T)) < k) and len(X)>1:
# FILTER Step
# this only changes X
vs = estimateSet(X, union(S,T), k, r, m)
while (vs < alpha**(2)*(1-ep)*(OPT - fST)/r) and (len(X) > 0):
# get marginal contribution
if parallel:
marg_a = Parallel(n_jobs=-1)(delayed(estimateMarginal)(X, union(S,T), a, k, r, m) for a in X)
else:
marg_a = [estimateMarginal(X, union(S,T), a, k, r, m) for a in X]
Xnew = [X[idx] for idx, el in enumerate(marg_a) if el >= alpha*(1+ep/2)*(1-ep)*(OPT - fST)/k]
X = Xnew
vs = estimateSet(X, union(S,T), k, r, m)
# update sets
R = randomSample(X, k/r)
T = union(T, R)
# T changes but S doesn't
fST = oracle(union(S,T))
# outer loop numbers
print('Outer Loop: Val')
print(len(union(S,T)))
print(fST)
S = union(S,T)
fS = oracle(S)
end = time.time()
timePassed = end-start
if 'bayes' in dat:
y_adap.append((len(S),obj_fun(S), timePassed, fS))
else:
y_adap.append((len(S), obj_fun(X1[:,S], y_cat), timePassed, fS))
return y_adap
res_dict = {}
for i in val_list:
print(i)
print('k=' + str(i[0]))
print('OPT=' + str(i[2]))
start = time.time()
y_adap = dash(i[0], i[1], ep, i[2], all_predictors, alpha, parallel=isParallel)
res_dict.update({i[0]: y_adap})
print(res_dict)