-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
317 lines (278 loc) · 10.4 KB
/
main.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
"""Hide-and-Seek Privacy Challenge Codebase.
Reference: James Jordon, Daniel Jarrett, Jinsung Yoon, Ari Ercole, Cheng Zhang, Danielle Belgrave, Mihaela van der Schaar,
"Hide-and-Seek Privacy Challenge: Synthetic Data Generation vs. Patient Re-identification with Clinical Time-series Data,"
Neural Information Processing Systems (NeurIPS) Competition, 2020.
Link: https://www.vanderschaar-lab.com/announcing-the-neurips-2020-hide-and-seek-privacy-challenge/
Last updated Date: June 21th 2020
Code author: Jinsung Yoon, Evgeny Saveliev
Modified by Ying-Jia Lin
-----------------------------
Note: We use TimeGAN or noise addition as a hider and KNN as the seeker as examples.
Pipeline
Step 1: Load and preprocess dataset
Step 2: Run hider algorithm
Step 3: Define enlarge data and its label
Step 4: Run seeker algorithm
Step 5: Evaluation
- feature-prediction
- one-step-ahead-prediction
- reidentification-score
"""
# Necessary packages
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os, joblib, time, pickle, shutil
import argparse
import numpy as np
import tensorflow as tf
import pickle
import warnings
warnings.filterwarnings("ignore")
from utils import tf2_set_seed, in_progress, tf_set_log_level
from hider.timegan.timegan import train_timegan
from hider.timegan import dp_timegan
# from hider.add_noise import add_noise
from seeker.knn_seeker import knn_seeker
from seeker.binary_predictor import binary_predictor
from data.data_utils import data_division
from data.data_preprocess import data_preprocess
from metrics.metric_utils import feature_prediction, one_step_ahead_prediction, reidentify_score
from sklearn.metrics import confusion_matrix
def main(args):
"""Hide-and-Seek Privacy Challenge main function.
Args:
- data_name: amsterdam or stock
- max_seq_len: maximum sequence length
- train_rate: ratio of training data
- feature_prediction_no: the number of features to be predicted for evaluation
- seed: random seed for train / test data division
- hider_model: timegan or add_noise
- noise_size: size of the noise for add_noise hider
Returns:
- feat_pred: feature prediction results (original & new)
- step_ahead_pred: step ahead prediction results (original & new)
- reidentification_score: reidentification score between hider and seeker
"""
# Set random seeds
tf2_set_seed(args.seed)
## Load & preprocess data
if args.data_name == 'amsterdam':
if args.use_gain:
print("Using GAIN-imputed data")
file_name = '/data/nips_hns/hide-and-seek/data/GAIN/amsterdam.csv'
ori_data = data_preprocess(file_name, args.max_seq_len)
else:
if os.path.exists('data/amsterdam/amsterdam-bin.jlb'):
ori_data = joblib.load('data/amsterdam/amsterdam-bin.jlb')
else:
file_name = 'data/amsterdam/train_longitudinal_data.csv'
ori_data, dynamic_time = data_preprocess(file_name, args.max_seq_len, args.imp_method)
elif args.data_name == 'stock':
with open('data/public_data/public_' + args.data_name + '_data.txt', 'rb') as fp:
ori_data = pickle.load(fp)
ori_data = np.asarray(ori_data)
# Divide the data into training and testing
divided_data, _ = data_division(ori_data, seed = args.seed, divide_rates = [args.train_rate, 1-args.train_rate])
divided_time, _ = data_division(dynamic_time, seed = args.seed, divide_rates = [args.train_rate, 1-args.train_rate])
args.feature_dim = ori_data.shape[-1]
train_data = np.asarray(divided_data[0])
test_data = np.asarray(divided_data[1])
train_time = np.asarray(divided_time[0])
test_time = np.asarray(divided_time[1])
print('Finish data loading: ' + str(args.data_name))
# For CuDNN bug
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# gpus = tf.config.experimental.list_physical_devices('GPU')
# print(gpus)
# for gpu in gpus:
# tf.config.experimental.set_visible_devices(gpu, device_type='GPU')
# tf.config.experimental.set_memory_growth(gpu, True)
## Run hider algorithm
hider_start = time.time()
if args.hider_model == 'timegan':
if args.use_dpsgd:
generated_data, train_log_dir = dp_timegan.train_timegan(train_data, 'train', args)
else:
generated_data, train_log_dir = train_timegan(train_data, train_time, args)
elif args.hider_model == 'add_noise':
generated_data = add_noise.add_noise(train_data, args.noise_size)
print('Finish hider algorithm (' + args.hider_model + ') training')
hider_end = time.time()
# Save the train and generated data for visualization
with open(os.path.join(train_log_dir, "ori.pickle"), "wb") as fb:
pickle.dump(train_data, fb)
with open(os.path.join(train_log_dir, "test.pickle"), "wb") as fb:
pickle.dump(test_data, fb)
with open(os.path.join(train_log_dir, "new.pickle"), "wb") as fb:
pickle.dump(generated_data, fb)
## Define enlarge data and its labels
enlarge_data = np.concatenate((train_data, test_data), axis = 0)
enlarge_data_label = np.concatenate((np.ones([train_data.shape[0],]), np.zeros([test_data.shape[0],])), axis = 0)
# Mix the order
idx = np.random.permutation(enlarge_data.shape[0])
enlarge_data = enlarge_data[idx]
enlarge_data_label = enlarge_data_label[idx]
## Run seeker algorithm
seeker_start = time.time()
if args.seeker_model == 'binary_predictor':
reidentified_data = binary_predictor(generated_data, enlarge_data, train_log_dir)
elif args.seeker_model == 'knn':
reidentified_data = knn_seeker(generated_data, enlarge_data)
print('Finish seeker algorithm (' + args.seeker_model + ') training')
seeker_end = time.time()
print('Hider needs {} sec'.format(hider_end - hider_start))
print('Seeker needs {} sec'.format(seeker_end - seeker_start))
## Evaluate the performance
# 1. Feature prediction
#feat_idx = np.random.permutation(train_data.shape[2])[:args.feature_prediction_no]
feat_idx = [55, 61, 63, 64, 65]
ori_feat_pred_perf = feature_prediction(train_data, test_data, feat_idx)
new_feat_pred_perf = feature_prediction(generated_data, test_data, feat_idx)
feat_pred = [ori_feat_pred_perf, new_feat_pred_perf]
print('Feature prediction results: ' +
'(1) Ori: ' + str(np.round(ori_feat_pred_perf, 4)) +
'(2) New: ' + str(np.round(new_feat_pred_perf, 4)))
# 2. One step ahead prediction
ori_step_ahead_pred_perf = one_step_ahead_prediction(train_data, test_data)
new_step_ahead_pred_perf = one_step_ahead_prediction(generated_data, test_data)
step_ahead_pred = [ori_step_ahead_pred_perf, new_step_ahead_pred_perf]
print('One step ahead prediction results: ' +
'(1) Ori: ' + str(np.round(ori_step_ahead_pred_perf, 4)) +
'(2) New: ' + str(np.round(new_step_ahead_pred_perf, 4)))
# 3. Reidentification score
reidentification_score = reidentify_score(enlarge_data_label, reidentified_data)
confusion_matrix(enlarge_data_label, reidentified_data)
print('Reidentification score: ' + str(np.round(reidentification_score, 4)))
return feat_pred, step_ahead_pred, reidentification_score
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ('yes', 'true', 't', 'y', '1'):
return True
elif v.lower() in ('no', 'false', 'f', 'n', '0'):
return False
else:
raise argparse.ArgumentTypeError('Boolean value expected.')
###
if __name__ == '__main__':
# Inputs for the main function
parser = argparse.ArgumentParser()
parser.add_argument(
'--exp_name',
default='timegan_test',
type=str)
parser.add_argument(
'--data_name',
choices=['amsterdam','stock'],
default='amsterdam',
type=str)
parser.add_argument(
'--use_gain',
default=False,
type=str2bool)
parser.add_argument(
'--max_seq_len',
default=100,
type=int)
parser.add_argument(
'--imp_method',
default='median',
type=str)
parser.add_argument(
'--train_rate',
default=0.5,
type=float)
parser.add_argument(
'--feature_prediction_no',
default=5,
type=int)
parser.add_argument(
'--seed',
default=0,
type=int)
parser.add_argument(
'--hider_model',
choices=['timegan','add_noise'],
default='timegan',
type=str)
parser.add_argument(
'--noise_size',
default=0.1,
type=float)
parser.add_argument(
'--seeker_model',
choices=['binary_predictor','knn'],
default='binary_predictor',
type=str)
##### hider params #####
parser.add_argument(
'--gen_type',
choices=['gan', 'autoencoder'],
default='gan',
type=str)
parser.add_argument(
'--module_name',
choices=['gru','lstm', 'lstmLN'],
default='gru',
type=str)
parser.add_argument(
'--epsilon',
default=1e-8,
type=float)
parser.add_argument(
'--optimizer',
default='adam',
type=str)
parser.add_argument(
'--use_dpsgd',
default=False,
type=str2bool)
parser.add_argument(
'--batch_size',
default=128,
type=int)
parser.add_argument(
'--z_dim',
default=-1,
type=int)
parser.add_argument(
'--hidden_dim',
default=10,
type=int)
parser.add_argument(
'--num_layers',
default=3,
type=int)
parser.add_argument(
'--embedding_iterations',
default=2000,
type=int)
parser.add_argument(
'--supervised_iterations',
default=500,
type=int)
parser.add_argument(
'--joint_iterations',
default=6000,
type=int)
parser.add_argument(
'--eta',
default=0.1,
type=int)
##### DP params #####
parser.add_argument(
'--l2_norm_clip',
default=1.0,
type=float)
parser.add_argument(
'--noise_multiplier',
default=0.1,
type=float)
parser.add_argument(
'--dp_lr',
default=0.15,
type=float)
args = parser.parse_args()
# Call main function
feat_pred, step_ahead_pred, reidentification_score = main(args)