1
- import pickle
2
- import random
3
- import keras
4
1
import argparse
5
2
import numpy as np
6
3
import tensorflow as tf
7
-
8
4
from sklearn .model_selection import StratifiedKFold
9
5
from sklearn .metrics import average_precision_score as auprc
10
6
from sklearn .metrics import roc_auc_score as auc_score
7
+ import keras
11
8
from keras .utils import multi_gpu_model
12
- from keras .layers import Input , Dense , Masking , GRU , Dropout , Lambda , Permute
13
- from keras .models import load_model , Model , Sequential
9
+ from keras .layers import Input , Dense , GRU , Lambda , Permute
10
+ from keras .models import Model
14
11
from interpolation_layer import single_channel_interp , cross_channel_interp
15
12
16
13
np .random .seed (10 )
17
14
tf .set_random_seed (10 )
18
15
19
16
# Loading dataset
20
- """
21
- y : (N,) discrete for classification, real values for regression
22
- x : (N, D, tn) input multivariate time series data with dimension
23
- where N is number of data cases, D is the dimension of
24
- sparse and irregularly sampled time series and tn is the union
25
- of observed time stamps in all the dimension for a data case n.
26
- Since each tn is of variable length, we pad them with zeros to
27
- have an array representation.
28
- m : (N, D, tn) where m[i,j,k] = 0 means that x[i,j,k] is not observed.
29
- T : (N, D, tn) represents the actual time stamps of observation;
30
- """
31
-
32
- """To implement the autoencoder component of the loss, we introduce a set
33
- of masking variables mr (and mr1) for each data point. If drop_mask = 0, then we remove
34
- the data point as an input to the interpolation network, and include
35
- the predicted value at this time point when assessing
36
- the autoencoder loss. In practice, we randomly select 20% of the
17
+ # y : (N,) discrete for classification, real values for regression
18
+ # x : (N, D, tn) input multivariate time series data with dimension
19
+ # where N is number of data cases, D is the dimension of
20
+ # sparse and irregularly sampled time series and tn is the union
21
+ # of observed time stamps in all the dimension for a data case n.
22
+ # Since each tn is of variable length, we pad them with zeros to
23
+ # have an array representation.
24
+ # m : (N, D, tn) where m[i,j,k] = 0 means that x[i,j,k] is not observed.
25
+ # T : (N, D, tn) represents the actual time stamps of observation;
26
+
27
+
28
+ """To implement the autoencoder component of the loss, we introduce a set
29
+ of masking variables mr (and mr1) for each data point. If drop_mask = 0,
30
+ then we removecthe data point as an input to the interpolation network,
31
+ and includecthe predicted value at this time point when assessing
32
+ the autoencoder loss. In practice, we randomly select 20% of the
37
33
observed data points to hold out from
38
34
every input time series."""
39
35
40
36
41
- def drop_mask (mask , perc = 0.2 ):
37
+ def hold_out (mask , perc = 0.2 ):
42
38
drop_mask = np .ones_like (mask )
43
39
drop_mask *= mask
44
40
for i in range (mask .shape [0 ]):
@@ -56,8 +52,8 @@ def drop_mask(mask, perc=0.2):
56
52
return drop_mask
57
53
58
54
59
- x = np .concatenate ((x , m , T , drop_mask (m )), axis = 1 ) # input format
60
- print ( x .shape , y .shape )
55
+ x = np .concatenate ((x , m , T , hold_out (m )), axis = 1 ) # input format
56
+ print x .shape , y .shape
61
57
62
58
ap = argparse .ArgumentParser ()
63
59
ap .add_argument ("-g" , "--gpus" , type = int , default = 4 ,
@@ -75,7 +71,7 @@ def drop_mask(mask, perc=0.2):
75
71
76
72
args = vars (ap .parse_args ())
77
73
gpu_num = args ["gpus" ]
78
- iter = args ["epochs" ]
74
+ epoch = args ["epochs" ]
79
75
hid = args ["hidden_units" ]
80
76
timestamp = x .shape [2 ]
81
77
num_features = x .shape [1 ]/ 4
@@ -86,8 +82,8 @@ def drop_mask(mask, perc=0.2):
86
82
else :
87
83
batch = args ["batch_size" ]
88
84
89
- # Autoencoder loss
90
-
85
+ """ Autoencoder loss
86
+ """
91
87
92
88
def customloss (ytrue , ypred ):
93
89
# standard deviation of each feature mentioned in paper for MIMIC_III data
@@ -141,7 +137,7 @@ def interp_net():
141
137
model = multi_gpu_model (orig_model , gpus = gpu_num )
142
138
else :
143
139
model = orig_model
144
- print ( orig_model .summary () )
140
+ print orig_model .summary ()
145
141
return model
146
142
147
143
@@ -154,19 +150,30 @@ def interp_net():
154
150
i = 0
155
151
kfold = StratifiedKFold (n_splits = 5 , shuffle = True , random_state = seed )
156
152
for train , test in kfold .split (np .zeros (len (y )), y ):
157
- print ( "Running Fold:" , i + 1 )
153
+ print "Running Fold:" , i + 1
158
154
model = interp_net () # re-initializing every time
159
- model .compile (optimizer = 'adam' , loss = {'main_output' : 'binary_crossentropy' , 'aux_output' : customloss },
160
- loss_weights = {'main_output' : 1. , 'aux_output' : 1. }, metrics = {'main_output' : 'accuracy' })
161
- model .fit ({'input' : x [train ]}, {'main_output' : y [train ], 'aux_output' : x [train ]},
162
- batch_size = batch , callbacks = callbacks_list , nb_epoch = iter , validation_split = 0.20 , verbose = 2 )
155
+ model .compile (
156
+ optimizer = 'adam' ,
157
+ loss = {'main_output' : 'binary_crossentropy' , 'aux_output' : customloss },
158
+ loss_weights = {'main_output' : 1. , 'aux_output' : 1. },
159
+ metrics = {'main_output' : 'accuracy' })
160
+ model .fit (
161
+ {'input' : x [train ]}, {'main_output' : y [train ], 'aux_output' : x [train ]},
162
+ batch_size = batch ,
163
+ callbacks = callbacks_list ,
164
+ nb_epoch = epoch ,
165
+ validation_split = 0.20 ,
166
+ verbose = 2 )
163
167
y_pred = model .predict (x [test ], batch_size = batch )
164
168
y_pred = y_pred [0 ]
165
- total_loss , score , reconst_loss , acc = model .evaluate (
166
- {'input' : x [test ]}, {'main_output' : y [test ], 'aux_output' : x [test ]}, batch_size = batch , verbose = 0 )
169
+ total_loss , score , reconst_loss , acc = model .evaluate (
170
+ {'input' : x [test ]},
171
+ {'main_output' : y [test ], 'aux_output' : x [test ]},
172
+ batch_size = batch ,
173
+ verbose = 0 )
167
174
results ['loss' ].append (score )
168
175
results ['acc' ].append (acc )
169
176
results ['auc' ].append (auc_score (y [test ], y_pred ))
170
177
results ['auprc' ].append (auprc (y [test ], y_pred ))
171
- print ( results )
178
+ print results
172
179
i += 1
0 commit comments