/
benefit_predictor.py
567 lines (459 loc) · 21.4 KB
/
benefit_predictor.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import matplotlib.pyplot as plt
import time
import argparse
import numpy as np
import tensorflow
from keras.models import Sequential, load_model
from keras.layers import LSTM, Dense, Flatten
from keras.callbacks import EarlyStopping
import sklearn
from sklearn import metrics
import os
import heapq
import pickle
from numpy.random import seed
from keras_self_attention import SeqSelfAttention
# seed(0)
# tensorflow.random.set_seed(0)
def difference(x):
x_diff = np.zeros(x.shape)
for i in range(x.shape[1] - 1, 0, -1):
x_diff[:, i] = x[:, i] - x[:, i - 1]
x_diff[:, 0] = 0
return x_diff
def get_benefit(x_mat, y_arr, labels, misclassification_cost, multivariate=False):
if multivariate:
T = max([d.shape[0] for d in x_mat])
N = len(x_mat)
else:
T = x_mat.shape[1] # length of each time series
N = x_mat.shape[0] # number of time series
benefit = {l: np.zeros((N, T)) for l in labels}
for i, y in enumerate(y_arr):
for l in labels:
benefit[l][i, :] = (T - 1 - np.arange(T)) - misclassification_cost[y, l]
return benefit
def split_sequences(sequences, shingle_size):
x, y = [], []
for sequence in sequences:
for i in range(len(sequence) - shingle_size + 1):
end_ix = i + shingle_size
if end_ix > len(sequence):
break
seq_x = sequence[i:end_ix, :-1]
seq_y = sequence[end_ix - 1, -1]
x.append(seq_x)
y.append(seq_y)
return np.array(x), np.array(y)
def fit_lstm(train_x, benefit_l, num_hidden_units=50, shingle_size=10, num_epochs=30, num_features=1, attention=False):
model = Sequential()
if attention:
model.add(LSTM(num_hidden_units, activation='relu', input_shape=(shingle_size, num_features),
return_sequences=True)) #
model.add(SeqSelfAttention(attention_activation='sigmoid', history_only=True))
model.add(Flatten())
else:
model.add(LSTM(num_hidden_units, activation='relu', input_shape=(shingle_size, num_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')
if num_features == 1:
N = train_x.shape[0]
data = [np.vstack((train_x[i, :], benefit_l[i, :])).T for i in range(N)]
train_x, train_y = split_sequences(data, shingle_size) # vectorize=False
print(train_x.shape, train_y.shape)
else:
train_x, train_y = split_multivariate_sequences(train_x, benefit_l, shingle_size=shingle_size)
print(train_x.shape, train_y.shape)
es = EarlyStopping(monitor='val_loss', verbose=1, patience=20)
model.fit(train_x, train_y, epochs=num_epochs, verbose=1, validation_split=0.1, callbacks=[es])
predictions = model.predict(train_x)[:, 0]
rmse = np.sqrt(sklearn.metrics.mean_squared_error(train_y, predictions))
print('RMSE: %.3f' % rmse)
return model
def predict_lstm(model, test_x, shingle_size=10):
N = test_x.shape[0]
data = [np.vstack((test_x[i, :], np.ones(test_x.shape[1]))).T for i in range(N)]
test_x, _ = split_sequences(data, shingle_size) # vectorize=False
return model.predict(test_x)[:, 0]
def split_multivariate_sequences(data_x, benefit=None, shingle_size=10):
x, y = [], []
for i in range(len(data_x)):
for j in range(data_x[i].shape[0] - shingle_size + 1):
x.append(data_x[i][j:j + shingle_size, :])
if benefit is not None: # test time
y.append(benefit[i, j + shingle_size - 1])
x = np.array(x)
if benefit is None:
y = None
else:
y = np.array(y)
return x, y
def predict_lstm_multivariate(model, test_x, shingle_size=10):
N = len(test_x)
lengths = [d.shape[0] for d in test_x] # assuming test_x dim is N X T X dim
# lengths = np.cumsum(lengths) # split indices based on lengths
test_x, _ = split_multivariate_sequences(test_x, shingle_size=shingle_size) # vectorize=False
predictions = model.predict(test_x)[:, 0]
reshaped_predictions = []
start_idx = 0
for series_len in lengths:
end_idx = series_len
reshaped_predictions.append(predictions[start_idx:start_idx + end_idx])
start_idx = series_len
# predictions = np.split(predictions, lengths)
# reshaped_predictions = [d.reshape(1, -1) for d in predictions]
assert N == len(reshaped_predictions)
return reshaped_predictions # model.predict(test_x)[:, 0]
def load_ucr_data(name, path='data/ucr/'):
train_path = os.path.join(path, name, name + '_TRAIN.tsv')
test_path = os.path.join(path, name, name + '_TEST.tsv')
train = np.loadtxt(train_path)
train_y = train[:, 0].astype(int)
train_x = difference(train[:, 1:])
test = np.loadtxt(test_path)
test_y = test[:, 0].astype(int)
test_x = difference(test[:, 1:]) # successive differences
return train_x, train_y, test_x, test_y
def trainer(args):
train_x, train_y, test_x, test_y = load_ucr_data(args.dataset_name, args.data_path)
labels = np.unique(train_y)
# get K from args
if args.K is None:
miss_cost = int(train_x.shape[1] * args.K_n_ratio)
else:
miss_cost = args.K
misclassification_cost = {(l1, l2): 0 if l1 == l2 else miss_cost # cost of l1 being classified as l2
for l1 in labels for l2 in labels}
benefit = get_benefit(train_x, train_y, labels, misclassification_cost)
print("Number of test samples", len(test_y))
print("Length of each test sample", len(test_x[0]))
# check if training is required or not
if args.load_existing_model:
models = {}
# scaler_transforms = {}
for l in labels:
models[l] = load_model(os.path.join(args.model_save_path, 'lbl' + str(l) + '_K' + str(miss_cost) +'.h5'))
else:
models = {}
start = time.time()
for l in labels:
print('label', l)
models[l] = fit_lstm(train_x, benefit[l], num_epochs=args.epochs, shingle_size=args.shingle_size,
attention=args.attention)
end = time.time()
print("Total traintime:", end - start)
# saving models
if args.model_save_path:
try:
os.makedirs(args.model_save_path)
except FileExistsError:
# directory already exists
pass
print("Saving model")
for l, model in models.items():
model.save(os.path.join(args.model_save_path, 'lbl' + str(l) + '_K' + str(miss_cost) +'.h5'))
# evaluation
test_y_pred = {l: predict_lstm(model, test_x, shingle_size=args.shingle_size).reshape(test_x.shape[0], -1)
for l, model in models.items()}
counts = {'correct': np.zeros(test_x.shape[1]),
'wrong': np.zeros(test_x.shape[1])}
prediction_times = []
prediction_labels = []
no_action_from_model = 0
start = time.time()
# minimum gap in benefit before outputting a label
min_gap = 0
if args.min_decision_gap:
b_at_0 = [benefit[l][0][0] for l in labels]
top_2_b = heapq.nlargest(2, b_at_0)
min_gap = (top_2_b[0]-top_2_b[1])*args.min_decision_gap
# test evaluation
for i in range(test_x.shape[0]):
pred_label, pred_time = np.random.choice(labels), test_x.shape[1]
for t in range(test_y_pred[labels[0]].shape[1]):
b = np.array([test_y_pred[l][i, t] for l in labels])
top_2_pred_b = heapq.nlargest(2, b)
if top_2_pred_b[0] - top_2_pred_b[1] >= min_gap:
if np.max(b) > 0 and np.sum(b == np.max(b)) == 1:
pred_label, pred_time = labels[np.argmax(b)], t
break
if pred_label == test_y[i]:
try:
counts['correct'][pred_time + args.shingle_size] += 1
prediction_labels.append(pred_label)
except IndexError:
no_action_from_model += 1
pred_label = labels[np.argmax(b)]
prediction_labels.append(pred_label) # adding just based on the last time tick comparison
continue
else:
try:
counts['wrong'][pred_time + args.shingle_size] += 1
prediction_labels.append(pred_label)
except IndexError:
no_action_from_model += 1
pred_label = labels[np.argmax(b)]
prediction_labels.append(pred_label) # adding just based on the last time tick comparison
continue
prediction_times.append(pred_time + args.shingle_size)
# prediction_labels.append(pred_label)
end = time.time()
print("Total test time:", end - start)
print("Average test time:", (end - start) / len(test_x))
avg_earliness = np.average(prediction_times) / len(test_x[0])
print("Earliness:", avg_earliness)
print("No action:", no_action_from_model)
####################
avg_earliness = np.average(prediction_times) / len(test_x[0])
accuracy = metrics.accuracy_score(test_y, prediction_labels) #(np.sum(counts['correct']) / (test_x.shape[0] - no_action_from_model))
acc_total = (np.sum(counts['correct']) / test_x.shape[0])
print("Accuracy (all):", accuracy, "Accuracy (remove no action)", acc_total)
print("Earliness:", avg_earliness)
print("No action from model:", no_action_from_model)
print("True:", len(test_y), ", Pred:", len(prediction_labels))
print("Precision:", metrics.precision_score(test_y, prediction_labels))
print("Recall:", metrics.recall_score(test_y, prediction_labels))
print("F1:", metrics.f1_score(test_y, prediction_labels))
# write report to a file for later use
if args.report_results_file is not None:
print("Writing results to file")
with open(os.path.join(args.model_save_path, args.report_results_file), "a+") as f:
f.write('\t'.join(['K', 'K_n_ratio', 'accuracy', 'acc_total', 'earliness', 'no_decision', 'min_gap']))
f.write('\n')
f.write('\t'.join([str(miss_cost), str(args.K_n_ratio), str(accuracy), str(acc_total), str(avg_earliness),
str(no_action_from_model), str(args.min_decision_gap)]))
f.write('\n')
def load_mimic_data(path='data/mimic'):
trx_path = os.path.join(path, 'train_x_subsampled.pkl')
tex_path = os.path.join(path, 'test_x.pkl')
try_path = os.path.join(path, 'train_y_subsampled.pkl')
tey_path = os.path.join(path, 'test_y.pkl')
with open(trx_path, 'rb') as f:
train_x = pickle.load(f)
with open(try_path, 'rb') as f:
train_y = pickle.load(f)
with open(tex_path, 'rb') as f:
test_x = pickle.load(f)
with open(tey_path, 'rb') as f:
test_y = pickle.load(f)
return train_x, train_y, test_x, test_y
def load_eeg_data(path='data/raw_data.pkl'):
with open(path, 'rb') as f:
X, test_X, y, test_y, train_pid, test_pid = pickle.load(f)
# keep only the one with length at least 24 and restrict only to 96 time steps
X_train = []
y_train_mult = []
for i in range(len(X)):
if len(X[i]) >= 24:
val = np.nan_to_num(X[i][:96])
val[val > 1000] = 1000.
val[val < -1000] = -1000.
X_train.append(val)
y_train_mult.append(y[i])
X_test = []
y_test_mult = []
for i in range(len(test_X)):
if len(test_X[i]) >= 24:
val = np.nan_to_num(test_X[i][:96])
val[val > 1000] = 1000.
val[val < -1000] = -1000.
X_test.append(val)
y_test_mult.append(test_y[i])
# converts targets to binary targets -- from 0, 1, 2, 3, 4 ---> 0, 1 with 1 being zombie
y_train = []
y_test = []
for lbl in y_train_mult:
if lbl == 0:
y_train.append(0)
else:
y_train.append(1)
for lbl in y_test_mult:
if lbl == 0:
y_test.append(0)
else:
y_test.append(1)
return X_train, y_train, X_test, y_test
def trainer_multivariate(args):
"""
In this method we will trin just one model for label 1
:param args:
:return:
"""
# dataset will be list of numpy array of varying lengths
if args.dataset_name == 'mimic':
train_x, train_y, test_x, test_y = load_mimic_data(args.data_path)
else:
train_x, train_y, test_x, test_y = load_eeg_data(args.data_path)
print("Trainin and test counts:", np.unique(train_y, return_counts=True), np.unique(test_y, return_counts=True))
# find unique lables
labels = np.unique(train_y)
num_features = train_x[0].shape[-1]
# max length of a series
max_length = max([d.shape[0] for d in train_x])
# get K from args
if args.K is None:
miss_cost = int(max_length * args.K_n_ratio)
else:
miss_cost = args.K
misclassification_cost = {(0, 1): miss_cost, # cost of l1 being classified as l2
(1, 1): 0}
# get benefit only for the label 1
benefit = get_benefit(train_x, train_y, labels[1:], misclassification_cost, multivariate=True)
print("Number of test samples", len(test_y))
print("Length of each test sample", len(test_x[0]))
# check if training is required or not
if args.load_existing_model:
models = {}
# scaler_transforms = {}
for l in labels[1:]:
models[l] = load_model(os.path.join(args.model_save_path, 'lbl' + str(l) + '_K' + str(miss_cost) +'.h5'))
else:
models = {}
start = time.time()
for l in labels[1:]:
print('label', l)
models[l] = fit_lstm(train_x, benefit[l], num_epochs=args.epochs, shingle_size=args.shingle_size,
num_features=num_features)
end = time.time()
print("Total traintime:", end - start)
# saving models
if args.model_save_path:
try:
os.makedirs(args.model_save_path)
except FileExistsError:
# directory already exists
pass
print("Saving model")
for l, model in models.items():
model.save(os.path.join(args.model_save_path, 'lbl' + str(l) + '_K' + str(miss_cost) +'.h5'))
# evaluation --- need to change so that variable size examples are predicted one by one
# it wil contain list of predictions
test_y_pred = {l: predict_lstm_multivariate(model, test_x, shingle_size=args.shingle_size)
for l, model in models.items()}
counts = {'correct': np.zeros(max_length),
'wrong': np.zeros(max_length)}
always_prediction_times = np.ones(len(test_x))*max_length
prediction_times = []
prediction_labels = []
earliness_fraction = []
no_action_from_model = 0
start = time.time()
# test evaluation
for i in range(len(test_x)):
pred_label, pred_time = np.random.choice(labels), max_length
for t in range(len(test_y_pred[labels[1]][i])):
b = np.array([test_y_pred[l][i][t] for l in labels[1:]])
if np.max(b) > 0 and np.sum(b == np.max(b)) == 1:
pred_label, pred_time = labels[np.argmax(b)], t
break
if pred_label == test_y[i]:
try:
counts['correct'][pred_time + args.shingle_size] += 1
prediction_labels.append(pred_label)
except IndexError:
no_action_from_model += 1
pred_label = labels[np.argmax(b)]
prediction_labels.append(pred_label) # adding just based on the last time tick comparison
continue
else:
try:
counts['wrong'][pred_time + args.shingle_size] += 1
prediction_labels.append(pred_label)
except IndexError:
no_action_from_model += 1
pred_label = labels[np.argmax(b)]
prediction_labels.append(pred_label) # adding just based on the last time tick comparison
continue
earliness_fraction.append((pred_time + args.shingle_size)/len(test_y_pred[labels[1]][i]))
prediction_times.append(pred_time + args.shingle_size)
always_prediction_times[i] = pred_time + args.shingle_size
# prediction_labels.append(pred_label)
end = time.time()
print("Total test time:", end - start)
print("Average test time:", (end - start) / len(test_x))
avg_earliness = np.average(earliness_fraction) # np.average(prediction_times) / len(test_x[0])
print("Earliness:", avg_earliness)
print("No action:", no_action_from_model)
accuracy = metrics.accuracy_score(test_y, prediction_labels) #(np.sum(counts['correct']) / (test_x.shape[0] - no_action_from_model))
acc_total = (np.sum(counts['correct']) / len(test_x))
print("Accuracy (all):", accuracy, "Accuracy (remove no action)", acc_total)
print("Earliness:", avg_earliness)
print("No action from model:", no_action_from_model)
print("True:", len(test_y), ", Pred:", len(prediction_labels))
print("Precision:", metrics.precision_score(test_y, prediction_labels))
print("Recall:", metrics.recall_score(test_y, prediction_labels))
print("F1:", metrics.f1_score(test_y, prediction_labels))
series_lens = [d.shape[0] for d in test_x]
benefit_val = calculate_benefit(series_lens, always_prediction_times, prediction_labels, test_y, K=300, d_label=1)
print("Incurred benefit:", benefit_val)
# write report to a file for later use
if args.report_results_file is not None:
print("Writing results to file")
with open(os.path.join(args.model_save_path, args.report_results_file), "a+") as f:
f.write('\t'.join(['K', 'K_n_ratio', 'accuracy', 'acc_total', 'earliness', 'no_decision', 'min_gap']))
f.write('\n')
f.write('\t'.join([str(miss_cost), str(args.K_n_ratio), str(accuracy), str(acc_total), str(avg_earliness),
str(no_action_from_model), str(args.min_decision_gap)]))
f.write('\n')
# find benefit of this model
def calculate_benefit(series_lens, pred_tau, pred_class, labels_test, K=300,d_label=None):
# d_label is the death label. Then we need to claculate the cost wrt to this
total_benefit = 0
if d_label:
for i in range(len(labels_test)):
pred_label, pred_time = pred_class[i], pred_tau[i]
if labels_test[i] == d_label and pred_label == labels_test[i]: # pred death, actual death
total_benefit += series_lens[i] - pred_time
elif labels_test[i] != d_label and pred_label == d_label: # actual survive, predited death
total_benefit += series_lens[i] - pred_time - K
else:
total_benefit += 0
else:
for i in range(len(labels_test)):
pred_label, pred_time = pred_class[i], pred_tau[i]
if pred_label == labels_test[i]: # pred death, actual death
total_benefit += series_lens[i] - pred_time
else:
total_benefit += series_lens[i] - pred_time - K
return total_benefit
def benefit_parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--dataset_name', type=str, default='ECG200', help='UCR Dataset') # None
parser.add_argument('--model_save_path', type=str, default="../kerasmodels/",
help="Path to save trained model") # None
parser.add_argument('--report_results_file', type=str, default=None, help="File to save experiment details "
"in models folder") # None
parser.add_argument('--data_path', type=str, default='../datasets/ucr/', help='Data path')
parser.add_argument('--batch_size', type=int, default=32, help='Batch Size')
parser.add_argument('--epochs', type=int, default=1, help='number of epochs')
parser.add_argument('--dropout', type=float, default=0., help='LSTM Dropout')
parser.add_argument('--attention', type=int, default=0, help='Attention')
parser.add_argument('--num_layers', type=int, default=1, help='LSTM Layers')
parser.add_argument('--hidden_size', type=int, default=16, help='Hidden Dim of LSTM')
parser.add_argument('--num_features', type=int, default=1, help='Input feature size')
parser.add_argument('--lr', type=float, default=0.01, help='Learning rate')
parser.add_argument('--min_t', type=int, default=5, help='Minimum length for evaluation')
parser.add_argument('--max_t', type=int, default=None, help='Maximum length for evaluation')
parser.add_argument('--K', type=float, default=None, help='Misclassification Cost') # K is None or K_n_ratio is None
parser.add_argument('--K_n_ratio', type=float, default=None, help='Misclassification Cost as a ratio')
parser.add_argument('--shingle_size', type=int, default=10, help='Shingle size to use')
parser.add_argument('--load_existing_model', type=str, default=False, help="Loads existing model to test") # False
parser.add_argument('--min_decision_gap', type=float, default=0.4, help='Decision threshold in multi-class')
args, _ = parser.parse_known_args()
return args
def main():
args = benefit_parse_args()
args.dataset_name = 'ECG200'
args.data_path = '../datasets/'
args.report_results_file = None
args.epochs = 10
args.shingle_size = 10
args.load_existing_model = 'False'
args.min_decision_gap = 0.6
args.model_save_path = None
args.K = 10
args.load_existing_model = False
trainer(args)
# trainer_multivariate(args)
if __name__ == '__main__':
main()