-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiment_IPS_tIPS.py
96 lines (74 loc) · 3.24 KB
/
Experiment_IPS_tIPS.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# etips
#
# Copyright (c) Siemens AG, 2020
# Authors:
# Zhiliang Wu <[email protected]>
# License-Identifier: MIT
import pickle
from pathlib import Path
from functools import partial
import numpy as np
import pandas as pd
from sklearn.model_selection import KFold
from hyperopt import fmin, hp, tpe, Trials
from hyperopt.pyll import scope
from tuning import hyperopt_ips
from utils import fix_random_seed, InvokeTimes, load_bandit_data
if __name__ == '__main__':
fix_random_seed(0)
data_fp = Path('./data/')
exp_name = 'ips1' # or tips1
cv_index = 0 # 0-4
# (t)ips
x, y, a, p, d = load_bandit_data(fp=data_fp, fn='Bandit_1.pickle')
test_size = int(0.1 * x.shape[0])
x_tr, x_te = x[test_size:, :, :], x[:test_size, :, :]
y_tr, y_te = y[test_size:, :], y[:test_size, :]
a_tr, a_te = a[test_size:, :], a[:test_size, :]
d_tr, d_te = d[test_size:], d[:test_size]
p_tr = p[test_size:]
print(f'shape of x_tr, x_te: {x_tr.shape}, {x_te.shape}')
print(f'shape of y_tr, y_te: {y_tr.shape}, {y_te.shape}')
print(f'shape of a_tr, a_te: {a_tr.shape}, {a_te.shape}')
print(f'shape of d_tr, d_te: {d_tr.shape}, {d_te.shape}')
print(f'shape of p_tr: {p_tr.shape}')
kf = KFold(n_splits=5, shuffle=False, random_state=None)
tr_idx, val_idx = list(kf.split(x_tr))[cv_index]
it = InvokeTimes()
data_list = [x_tr[tr_idx, :, :], a_tr[tr_idx, :], p_tr[tr_idx], d_tr[tr_idx],
x_tr[val_idx, :, :], a_tr[val_idx, :], p_tr[val_idx], d_tr[val_idx],
x_te, y_te
]
print(f'size of validation data: {data_list[-3].shape[0]}')
exp_fp = Path(f'./Exps/{exp_name}/CV{cv_index}/')
exp_fp.mkdir(parents=True, exist_ok=True)
temp_fp = Path(f'./Exps/TEMP_CV{cv_index}_{exp_name}/')
temp_fp.mkdir(parents=True, exist_ok=True)
# if tips, translation=True
func = partial(hyperopt_ips, data=data_list, counter=it, fp=temp_fp, translation=False)
config = {'repr_size': 16 * scope.int(hp.quniform('repr_size', 1, 8, 1)),
'activation': hp.choice('activation', ['sigmoid', 'relu', 'tanh']),
'l2_coef': np.power(10, scope.int(hp.quniform('l2_coef', -10, -1, 1))),
'lr': np.power(10, scope.int(hp.quniform('lr', -10, -1, 1))),
# uncomment following if tips
# 'translation': scope.roundup(hp.uniform('translation', 0.1, 1.5)),
'batch_size': np.power(2, scope.int(hp.quniform('batch_size', 4, 7, 1))),
}
try:
trials = pickle.load(open(exp_fp / 'trials.hyperopt', 'rb'))
print('Resume from existing trials')
except FileNotFoundError:
print('Create new trials')
trials = Trials()
fmin(fn=func, space=config, algo=tpe.suggest, max_evals=100, trials=trials,
rstate=np.random.RandomState(0), return_argmin=False, show_progressbar=True)
df = pd.DataFrame(trials.results)
df.to_csv(exp_fp / 'trials.csv')
best_acc = df.loc[df.loss.idxmin(), 'test_acc']
print(f'best in {cv_index}: {best_acc}')
with open(exp_fp / 'trials.hyperopt', 'wb') as f:
pickle.dump(trials, f)
print(f'Information is saved in {exp_fp} for further analysis and training')