-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmain.py
199 lines (158 loc) · 7.81 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
# Time-Aware LSTM
# main function for supervised task
# An example dataset is shared but the original synthetic dataset
# can be accessed from http://www.emrbots.org/.
# Inci M. Baytas, 2017
#
# How to run: Give the correct path to the data
# Data is a list where each element is a 3 dimensional matrix which contains same length sequences.
# Instead of zero padding, same length sequences are put in the same batch.
# Example: L is the list containing all the batches with a length of N.
# L[0].shape gives [number of samples x sequence length x dimensionality]
# Please refer the example bash script
# You can use Split0 as the data.
import tensorflow as tf
import numpy as np
import scipy.io as sio
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score
from sklearn.utils import shuffle
import sys
import math
import cPickle
from TLSTM import TLSTM
def load_pkl(path):
with open(path) as f:
obj = cPickle.load(f)
return obj
def convert_one_hot(label_list):
for i in range(len(label_list)):
sec_col = np.ones([label_list[i].shape[0],label_list[i].shape[1],1])
label_list[i] = np.reshape(label_list[i],[label_list[i].shape[0],label_list[i].shape[1],1])
sec_col -= label_list[i]
label_list[i] = np.concatenate([label_list[i],sec_col],2)
return label_list
def training(path,learning_rate,training_epochs,train_dropout_prob,hidden_dim,fc_dim,key,model_path):
path_string = path + '/data_train.pkl'
data_train_batches = load_pkl(path_string)
path_string = path + '/elapsed_train.pkl'
elapsed_train_batches = load_pkl(path_string)
path_string = path + '/label_train.pkl'
labels_train_batches = load_pkl(path_string)
path_string = path + '/hidden_ind_train.pkl'
hidden_ind_train = load_pkl(path_string)
number_train_batches = len(data_train_batches)
print("Train data is loaded!")
input_dim = data_train_batches[0].shape[2]
output_dim = labels_train_batches[0].shape[1]
lstm = TLSTM(input_dim, output_dim, hidden_dim, fc_dim,key)
cross_entropy, y_pred, y, logits, labels = lstm.get_cost_acc()
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cross_entropy)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
for epoch in range(training_epochs): #
# Loop over all batches
total_cost = 0
for i in range(number_train_batches): #
# batch_xs is [number of patients x sequence length x input dimensionality]
batch_xs, batch_ys, batch_ts = data_train_batches[i], labels_train_batches[i], \
elapsed_train_batches[i]
batch_ts = np.reshape(batch_ts, [batch_ts.shape[0], batch_ts.shape[2]])
sess.run(optimizer,feed_dict={lstm.input: batch_xs, lstm.labels: batch_ys,\
lstm.keep_prob:train_dropout_prob, lstm.time:batch_ts})
print("Training is over!")
saver.save(sess,model_path)
Y_pred = []
Y_true = []
Labels = []
Logits = []
for i in range(number_train_batches): #
batch_xs, batch_ys, batch_ts = data_train_batches[i], labels_train_batches[i], \
elapsed_train_batches[i]
batch_ts = np.reshape(batch_ts, [batch_ts.shape[0], batch_ts.shape[2]])
c_train, y_pred_train, y_train, logits_train, labels_train = sess.run(lstm.get_cost_acc(), feed_dict={
lstm.input:
batch_xs, lstm.labels: batch_ys, \
lstm.keep_prob: train_dropout_prob, lstm.time: batch_ts})
if i > 0:
Y_true = np.concatenate([Y_true, y_train], 0)
Y_pred = np.concatenate([Y_pred, y_pred_train], 0)
Labels = np.concatenate([Labels, labels_train], 0)
Logits = np.concatenate([Logits, logits_train], 0)
else:
Y_true = y_train
Y_pred = y_pred_train
Labels = labels_train
Logits = logits_train
total_acc = accuracy_score(Y_true, Y_pred)
total_auc = roc_auc_score(Labels, Logits, average='micro')
total_auc_macro = roc_auc_score(Labels, Logits, average='macro')
print("Train Accuracy = {:.3f}".format(total_acc))
print("Train AUC = {:.3f}".format(total_auc))
print("Train AUC Macro = {:.3f}".format(total_auc_macro))
def testing(path,hidden_dim,fc_dim,key,model_path):
path_string = path + '/data_test.pkl'
data_test_batches = load_pkl(path_string)
path_string = path + '/elapsed_test.pkl'
elapsed_test_batches = load_pkl(path_string)
path_string = path + '/label_test.pkl'
labels_test_batches = load_pkl(path_string)
path_string = path + '/hidden_ind_test.pkl'
number_test_batches = len(data_test_batches)
print("Test data is loaded!")
input_dim = data_test_batches[0].shape[2]
output_dim = labels_test_batches[0].shape[1]
test_dropout_prob = 1.0
lstm_load = TLSTM(input_dim, output_dim, hidden_dim, fc_dim, key)
saver = tf.train.Saver()
with tf.Session() as sess:
saver.restore(sess, model_path)
Y_true = []
Y_pred = []
Logits = []
Labels = []
for i in range(number_test_batches):
batch_xs, batch_ys, batch_ts = data_test_batches[i], labels_test_batches[i], \
elapsed_test_batches[i]
batch_ts = np.reshape(batch_ts, [batch_ts.shape[0], batch_ts.shape[2]])
c_test, y_pred_test, y_test, logits_test, labels_test = sess.run(lstm_load.get_cost_acc(),
feed_dict={lstm_load.input: batch_xs,
lstm_load.labels: batch_ys,\
lstm_load.time: batch_ts,\
lstm_load.keep_prob: test_dropout_prob})
if i > 0:
Y_true = np.concatenate([Y_true, y_test], 0)
Y_pred = np.concatenate([Y_pred, y_pred_test], 0)
Labels = np.concatenate([Labels, labels_test], 0)
Logits = np.concatenate([Logits, logits_test], 0)
else:
Y_true = y_test
Y_pred = y_pred_test
Labels = labels_test
Logits = logits_test
total_auc = roc_auc_score(Labels, Logits, average='micro')
total_auc_macro = roc_auc_score(Labels, Logits, average='macro')
total_acc = accuracy_score(Y_true, Y_pred)
print("Test Accuracy = {:.3f}".format(total_acc))
print("Test AUC Micro = {:.3f}".format(total_auc))
print("Test AUC Macro = {:.3f}".format(total_auc_macro))
def main(argv):
training_mode = int(sys.argv[1])
path = str(sys.argv[2])
if training_mode == 1:
learning_rate = float(sys.argv[3])
training_epochs = int(sys.argv[4])
dropout_prob = float(sys.argv[5])
hidden_dim = int(sys.argv[6])
fc_dim = int(sys.argv[7])
model_path = str(sys.argv[8])
training(path, learning_rate, training_epochs, dropout_prob, hidden_dim, fc_dim, training_mode, model_path)
else:
hidden_dim = int(sys.argv[3])
fc_dim = int(sys.argv[4])
model_path = str(sys.argv[5])
testing(path, hidden_dim, fc_dim, training_mode, model_path)
if __name__ == "__main__":
main(sys.argv[1:])