-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
344 lines (300 loc) · 10.5 KB
/
utils.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
""" Packages import """
import os
import csv
import inspect
import numpy as np
import random as rd
import itertools as it
import scipy.stats as st
import matplotlib.pyplot as plt
from tqdm import tqdm
from logger import configure
cmap = {
0: "black",
1: "blue",
2: "yellow",
3: "green",
4: "red",
5: "grey",
6: "purple",
7: "brown",
8: "pink",
9: "cyan",
}
mapping_methods_labels = {
"Hyper": "Hyper",
"EpiNet": "EpiNet",
"Ensemble": "Ensemble",
}
mapping_methods_colors = {
"Hyper": "blue",
"EpiNet": "green",
"Ensemble": "red",
}
def labelColor(methods):
"""
Map methods to labels and colors for regret curves
:param methods: list, list of methods
:return: lists, labels and vectors
"""
labels = [
mapping_methods_labels[m] if m in mapping_methods_labels.keys() else m
for m in methods
]
colors = [
mapping_methods_colors[m] if m in mapping_methods_colors.keys() else None
for m in methods
]
return labels, colors
def sigmoid(x):
return np.where(x >= 0, 1 / (1 + np.exp(-x)), np.exp(x) / (1 + np.exp(x)))
def rd_argmax(vector):
"""
Compute random among eligible maximum indices
:param vector: np.array
:return: int, random index among eligible maximum indices
"""
m = np.amax(vector)
indices = np.nonzero(vector == m)[0]
return rd.choice(indices)
def rd_max(vector):
"""
Compute random among eligible maximum value
:param vector: np.array
:return: int, random index among eligible maximum value
"""
index = rd_argmax(vector)
return vector[index]
def haar_matrix(M):
"""
Haar random matrix generation
"""
z = np.empty((M, M), dtype=np.float32)
z[:] = np.random.randn(M, M)
q, r = np.linalg.qr(z)
d = np.diag(r)
ph = d / np.abs(d)
return np.multiply(q, ph)
def sphere_matrix(N, M):
v = np.empty((N, M), dtype=np.float32)
v[:] = np.random.randn(N, M)
v /= np.linalg.norm(v, axis=1, keepdims=True)
return v
def normal_matrix(N, M):
v = np.empty((N, M), dtype=np.float32)
v[:] = np.random.randn(N, M)
return v
def random_choice_noreplace(m, n, axis=-1):
# m, n are the number of rows, cols of output
return np.random.rand(m, n).argsort(axis=axis)
def sample_buffer_noise(noise_type, M, sparsity=2):
# ensure the sampled vector is isotropic
assert M > 0
if noise_type == "Sphere":
return sphere_matrix(1, M)[0]
elif noise_type == "Gaussian" or noise_type == "Normal":
return normal_matrix(1, M)[0] / np.sqrt(M)
elif noise_type == "PMCoord":
i = np.random.choice(M)
B = np.zeros(M, dtype=np.float32)
B[i] = random_sign()
return B
elif noise_type == "OH":
i = np.random.randint(0, M)
B = np.zeros(M, dtype=np.float32)
B[i] = 1
return B
elif noise_type == "Sparse":
i = random_choice_noreplace(1, M)[0, :sparsity]
B = np.zeros(M, dtype=np.float32)
B[i] = random_sign(1 * sparsity)
return B / np.sqrt(sparsity)
elif noise_type == "SparseConsistent":
i = random_choice_noreplace(1, M)[0, :sparsity]
B = np.zeros(M, dtype=np.float32)
B[i] = random_sign(1)
return B / np.sqrt(sparsity)
elif noise_type == "UnifCube":
B = np.empty(M, dtype=np.float32)
B[:] = 2 * np.random.binomial(1, 0.5, M) - 1
return B / np.sqrt(M)
else:
raise NotImplementedError
def sample_action_noise(noise_type, M, dim=1, sparsity=2):
# ensure the sampled vector is isotropic
assert M > 0
if noise_type == "Sphere":
return sphere_matrix(dim, M) * np.sqrt(M)
elif noise_type == "Gaussian" or noise_type == "Normal":
return normal_matrix(dim, M)
elif noise_type == "PMCoord":
i = np.random.choice(M, dim)
B = np.zeros((dim, M), dtype=np.float32)
B[np.arange(dim), i] = random_sign(dim)
return B * np.sqrt(M)
elif noise_type == "OH":
i = np.random.randint(0, M, dim)
B = np.zeros((dim, M), dtype=np.float32)
B[np.arange(dim), i] = 1
return B
elif noise_type == "Sparse":
i = random_choice_noreplace(dim, M)[:, :sparsity]
B = np.zeros((dim, M), dtype=np.float32)
B[np.expand_dims(np.arange(dim), axis=1), i] = random_sign(
dim * sparsity
).reshape(dim, sparsity)
return B / np.sqrt(sparsity) * np.sqrt(M)
elif noise_type == "SparseConsistent":
i = random_choice_noreplace(dim, M)[:, :sparsity]
B = np.zeros((dim, M), dtype=np.float32)
B[np.expand_dims(np.arange(dim), axis=1), i] = random_sign(dim).reshape(dim, 1)
return B / np.sqrt(sparsity) * np.sqrt(M)
elif noise_type == "UnifCube":
B = np.empty((dim, M), dtype=np.float32)
B[:] = 2 * np.random.binomial(1, 0.5, (dim, M)) - 1
return B
else:
raise NotImplementedError
def sample_update_noise(noise_type, M, dim=1, sparsity=2, batch_size=1):
# ensure the sampled vector is isotropic
assert M > 0
if noise_type == "Sphere":
v = np.empty((batch_size, dim, M), dtype=np.float32)
v[:] = np.random.randn(batch_size, dim, M)
v /= np.linalg.norm(v, axis=-1, keepdims=True)
return v * np.sqrt(M)
elif noise_type == "Gaussian" or noise_type == "Normal":
v = np.empty((batch_size, dim, M), dtype=np.float32)
v[:] = np.random.randn(batch_size, dim, M)
return v
elif noise_type == "PMCoord":
B = np.zeros((M * 2, M), dtype=np.float32)
B[np.arange(M), np.arange(M)] = 1
B[np.arange(M) + M, np.arange(M)] = -1
B = np.expand_dims(B, 0).repeat(batch_size, 0)
return B * np.sqrt(M)
elif noise_type == "OH":
B = np.eye(M, dtype=np.float32)
B = np.expand_dims(B, 0).repeat(batch_size, 0)
return B
elif noise_type == "Sparse":
index = np.array([list(c) for c in it.combinations(list(range(M)), sparsity)])
elements = list(it.product([1, -1], repeat=sparsity))
n = len(index)
B = []
for e in elements:
e = np.expand_dims(np.array(e), 0).repeat(n, 0)
b = np.zeros((n, M), dtype=np.float32)
b[np.expand_dims(np.arange(n), axis=1), index] = e
B.append(b)
B = np.concatenate(B, 0)
B = np.expand_dims(B, 0).repeat(batch_size, 0)
return B / np.sqrt(sparsity) * np.sqrt(M)
elif noise_type == "SparseConsistent":
index = np.array([list(c) for c in it.combinations(list(range(M)), sparsity)])
n = len(index)
B = []
for element in [1, -1]:
e = np.ones((n, sparsity)) * element
b = np.zeros((n, M), dtype=np.float32)
b[np.expand_dims(np.arange(n), axis=1), index] = e
B.append(b)
B = np.concatenate(B, 0)
B = np.expand_dims(B, 0).repeat(batch_size, 0)
return B / np.sqrt(sparsity) * np.sqrt(M)
elif noise_type == "UnifCube":
B = np.array(list((it.product(range(2), repeat=M))), dtype=np.float32)
B = B * 2 - 1
B = np.expand_dims(B, 0).repeat(batch_size, 0)
return B
else:
raise NotImplementedError
def multi_haar_matrix(N, M):
v = np.zeros(((N // M + 1) * M, M))
for _ in range(N // M + 1):
v[np.arange(M), :] = haar_matrix(M)
return v[np.arange(N), :]
def display_results(delta, g, ratio, p_star):
"""
Display quantities of interest in IDS algorithm
"""
print("delta {}".format(delta))
print("g {}".format(g))
print("ratio : {}".format(ratio))
print("p_star {}".format(p_star))
def plotRegret(labels, regret, colors, title, path, log=False):
"""
Plot Bayesian regret
:param labels: list, list of labels for the different curves
:param mean_regret: np.array, averaged regrets from t=1 to T for all methods
:param colors: list, list of colors for the different curves
:param title: string, plot's title
"""
all_regrets = regret["all_regrets"]
mean_regret = regret["mean_regret"]
plt.figure(figsize=(10, 8), dpi=80)
T = mean_regret.shape[1]
for i, l in enumerate(labels):
c = cmap[i] if not colors else colors[i]
x = np.arange(T)
low_CI_bound, high_CI_bound = st.t.interval(
0.95, T - 1, loc=mean_regret[i], scale=st.sem(all_regrets[i])
)
# low_CI_bound = np.quantile(all_regrets[i], 0.05, axis=0)
# high_CI_bound = np.quantile(all_regrets[i], 0.95, axis=0)
plt.plot(x, mean_regret[i], c=c, label=l)
plt.fill_between(x, low_CI_bound, high_CI_bound, color=c, alpha=0.2)
if log:
plt.yscale("log")
plt.grid(color="grey", linestyle="--", linewidth=0.5)
plt.title(title)
plt.ylabel("Cumulative regret")
plt.xlabel("Time period")
plt.legend(loc="best")
plt.savefig(path + "/regret.pdf")
def storeRegret(
models, methods, param_dic, n_expe, T, path, seed=2022, use_torch=False
):
"""
Compute the experiment for all specified models and methods
:param models: list of MAB
:param methods: list of algorithms
:param param_dic: parameters for all the methods
:param n_expe: number of trials
:param T: Time horizon
:return: Dictionnary with results from the experiments
"""
all_regrets = np.zeros((len(methods), n_expe, T), dtype=np.float32)
for i, m in enumerate(methods):
set_seed(seed, use_torch=use_torch)
alg_name = m.split(":")[0]
logger = configure(path, ["csv"])
for j in tqdm(range(n_expe)):
model = models[j]
alg = model.__getattribute__(alg_name)
args = inspect.getfullargspec(alg)[0][3:]
args = [T, logger] + [param_dic[m][i] for i in args]
reward, regret = alg(*args)
all_regrets[i, j, :] = np.cumsum(regret)
print(f"{m}: {np.mean(all_regrets[i], axis=0)[-1]}")
mean_regret = all_regrets.mean(axis=1)
results = {
"mean_regret": mean_regret,
"all_regrets": all_regrets,
}
return results
def random_sign(N=None):
if (N is None) or (N == 1):
return np.random.randint(0, 2, 1) * 2 - 1
elif N > 1:
return np.random.randint(0, 2, N) * 2 - 1
def set_seed(seed, use_torch=False):
np.random.seed(seed)
rd.seed(seed)
if use_torch:
import torch
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.cuda.manual_seed(seed)