-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExperiments.py
153 lines (124 loc) · 6.13 KB
/
Experiments.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
import sys
sys.path.append("utils")
import numpy as np
import re
import os
import pickle
from Results import test_results, plot_train_history
from datetime import datetime
from ReadData import get_seq, get_reversed_seq, get_opn_probs, get_bub8_probs, get_bub10_probs, get_bub12_probs, get_vrnorm_probs
from contextlib import redirect_stdout
import keras
import tensorflow as tf
from keras import layers
from keras.models import Sequential, Model
from keras.layers import Conv1D, Conv2D, Conv3D, Dropout, MaxPooling1D, MaxPooling2D, Flatten, Dense
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.callbacks import LearningRateScheduler
from Models import lstm, lstm_att, cnn, cnnxlstm, cnnxlstm_allprobs, att
def single_train(model_definition, X_train, X_val, X_test, y_train, y_val, y_test, run_id):
log_file = "logs/"+run_id+".log"
hist_file = "logs/"+run_id+".pkl"
plot_file = "logs/"+run_id+".png"
model_file = "logs/"+run_id+".h5"
logdir = os.path.dirname(log_file)
if not os.path.exists(logdir):
os.mkdir(logdir)
model = model_definition
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=["accuracy", 'AUC'])
with open(log_file, 'w') as f:
with redirect_stdout(f):
for layer in model.layers:
print(layer.get_config())
early_stopping_monitor = EarlyStopping( monitor='val_loss', min_delta=0, patience=10,
verbose=1, mode='min', baseline=None,
restore_best_weights=True)
reduce_lr_loss = ReduceLROnPlateau(monitor='val_auc', factor=0.5, patience=3, verbose=1, min_delta=1e-4, mode='max')
history = model.fit(X_train, y_train,
shuffle=True,
batch_size=32,
epochs=100,
verbose=True,
validation_data=(X_val, y_val),
callbacks=[early_stopping_monitor, reduce_lr_loss])
print("Train results:\n")
test_results(X_train, y_train, model)
print("Val results:\n")
test_results(X_val, y_val, model)
print("Test results:\n")
test_results(X_test, y_test, model)
with open(hist_file, 'wb') as file_pi:
pickle.dump(history.history, file_pi)
model.save(model_file)
plot_train_history(history.history, plot_file)
if __name__ == "__main__":
strategy = tf.distribute.MirroredStrategy(devices=["/gpu:1", "/gpu:2"])
#with tf.device('/device:GPU:0'):
with strategy.scope():
seed = 42
np.random.seed(seed)
X_train_file = open('../databubbles/serialized/X_train.pkl', 'rb')
y_train_file = open('../databubbles/serialized/y_train.pkl', 'rb')
X_val_file = open('../databubbles/serialized/X_val.pkl', 'rb')
y_val_file = open('../databubbles/serialized/y_val.pkl', 'rb')
X_test_file = open('../databubbles/serialized/X_test.pkl', 'rb')
y_test_file = open('../databubbles/serialized/y_test.pkl', 'rb')
X_train = pickle.load(X_train_file)
y_train = pickle.load(y_train_file)
X_val = pickle.load(X_val_file)
y_val = pickle.load(y_val_file)
X_test = pickle.load(X_test_file)
y_test = pickle.load(y_test_file)
X_train_file.close()
y_train_file.close()
X_val_file.close()
y_val_file.close()
X_test_file.close()
y_test_file.close()
X_train_seq = get_seq(X_train)
# X_train_opn = get_opn_probs(X_train)
# X_train_bub8 = get_bub8_probs(X_train)
# X_train_bub10 = get_bub10_probs(X_train)
# X_train_bub12 = get_bub12_probs(X_train)
# X_train_vrnorm = get_vrnorm_probs(X_train)
X_val_seq = get_seq(X_val)
# X_val_opn = get_opn_probs(X_val)
# X_val_bub8 = get_bub8_probs(X_val)
# X_val_bub10 = get_bub10_probs(X_val)
# X_val_bub12 = get_bub12_probs(X_val)
# X_val_vrnorm = get_vrnorm_probs(X_val)
X_test_seq = get_seq(X_test)
# X_test_opn = get_opn_probs(X_test)
# X_test_bub8 = get_bub8_probs(X_test)
# X_test_bub10 = get_bub10_probs(X_test)
# X_test_bub12 = get_bub12_probs(X_test)
# X_test_vrnorm = get_vrnorm_probs(X_test)
print('X_train_seq shape: ', X_train_seq.shape)
# print('X_train_opn shape: ', X_train_opn.shape)
# print('X_train_bub8 shape: ', X_train_bub8.shape)
# print('X_train_bub10 shape: ', X_train_bub10.shape)
# print('X_train_bub12 shape: ', X_train_bub12.shape)
# print('X_train_vrnorm shape: ', X_train_vrnorm.shape)
#X_train = np.concatenate((X_train_seq, X_train_probs), axis=2)
#X_val = np.concatenate((X_val_seq, X_val_probs), axis=2)
#X_test = np.concatenate((X_test_seq, X_test_probs), axis=2)
# del X_train
# del X_val
# del X_test
y_train = y_train.reshape(*y_train.shape, 1)
y_val = y_val.reshape(*y_val.shape, 1)
y_test = y_test.reshape(*y_test.shape, 1)
if len(sys.argv) < 2:
run_id = str(datetime.now()).replace(" ", "_").replace("-", "_").replace(":", "_").split(".")[0]
else:
run_id = sys.argv[1]
# single_train(cnnxlstm_allprobs(X_train_seq.shape[1:], X_train_opn.shape[1:], X_train_bub8.shape[1:], X_train_bub10.shape[1:], X_train_bub12.shape[1:], X_train_vrnorm.shape[1:]),
# (X_train_seq, X_train_opn, X_train_bub8, X_train_bub10, X_train_bub12, X_train_vrnorm),
# (X_val_seq, X_val_opn, X_val_bub8, X_val_bub10, X_val_bub12, X_val_vrnorm),
# (X_test_seq, X_test_opn, X_test_bub8, X_test_bub10, X_test_bub12, X_test_vrnorm),
# y_train, y_val, y_test, run_id)
single_train(lstm_att(X_train_seq.shape[1:]),
X_train_seq,
X_val_seq,
X_test_seq,
y_train, y_val, y_test, run_id)