-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
329 lines (274 loc) · 12.3 KB
/
train.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
import pickle
import sys
import os
import math
import traceback
import argparse
import signal
import atexit
import time
import h5py
import random
#import tensorflow as tf
import numpy as np
import os
import logging
import tensorflow as tf
import gc
seed = 1337
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)
#import tensorflow.keras as keras
#import tensorflow.keras.utils
from tensorflow.keras.callbacks import ModelCheckpoint, LambdaCallback, Callback
#import tensorflow.keras.backend as K
#from model import create_model
##from myutils import prep, drop, batch_gen, seq2sent
from nltk.translate.bleu_score import corpus_bleu, sentence_bleu
import tokenizer
class HistoryCallback(Callback):
def setCatchExit(self, outdir, modeltype, timestart, mdlconfig):
self.outdir = outdir
self.modeltype = modeltype
self.history = {}
self.timestart = timestart
self.mdlconfig = mdlconfig
atexit.register(self.handle_exit)
signal.signal(signal.SIGTERM, self.handle_exit)
signal.signal(signal.SIGINT, self.handle_exit)
def handle_exit(self, *args):
if len(self.history.keys()) > 0:
try:
fn = outdir+'/histories/'+self.modeltype+'_hist_'+str(self.timestart)+'.pkl'
histoutfd = open(fn, 'wb')
pickle.dump(self.history, histoutfd)
print('saved history to: ' + fn)
fn = outdir+'/histories/'+self.modeltype+'_conf_'+str(self.timestart)+'.pkl'
confoutfd = open(fn, 'wb')
pickle.dump(self.mdlconfig, confoutfd)
print('saved config to: ' + fn)
except Exception as ex:
print(ex)
traceback.print_exc(file=sys.stdout)
sys.exit()
def on_train_begin(self, logs=None):
self.epoch = []
self.history = {}
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
self.epoch.append(epoch)
for k, v in logs.items():
self.history.setdefault(k, []).append(v)
if len(self.history.keys()) > 0:
try:
fn = outdir+'/histories/'+self.modeltype+'_hist_'+str(self.timestart)+'.pkl'
histoutfd = open(fn, 'wb')
pickle.dump(self.history, histoutfd)
#print('saved history to: ' + fn)
fn = outdir+'/histories/'+self.modeltype+'_conf_'+str(self.timestart)+'.pkl'
confoutfd = open(fn, 'wb')
pickle.dump(self.mdlconfig, confoutfd)
#print('saved config to: ' + fn)
except Exception as ex:
print(ex)
traceback.print_exc(file=sys.stdout)
gc.collect()
tf.keras.backend.clear_session()
if __name__ == '__main__':
timestart = int(round(time.time()))
parser = argparse.ArgumentParser(description='')
parser.add_argument('--gpu', type=str, help='0 or 1', default='0')
parser.add_argument('--batch-size', dest='batch_size', type=int, default=200)
parser.add_argument('--batchgen', dest='batchgen', type=str, default='regular')
parser.add_argument('--epochs', dest='epochs', type=int, default=10)
parser.add_argument('--lm-mode', dest='lmmode', action='store_true', default=False)
parser.add_argument('--model-type', dest='modeltype', type=str, default='vanilla')
parser.add_argument('--with-graph', dest='withgraph', action='store_true', default=False)
parser.add_argument('--with-calls', dest='withcalls', action='store_true', default=False)
parser.add_argument('--with-biodats', dest='withbiodats', type=str , default='vanilla')
parser.add_argument('--with-simmat', dest='withsimmats', action='store_true', default=False)
parser.add_argument('--rand-resplit', dest='randresplit', action='store_true', default=False)
parser.add_argument('--simmat-file', dest='simmatfile', type=str, default='softmax_usec.pkl')
parser.add_argument('--loss-type', dest='losstype', type=str, default='cce')
parser.add_argument('--vmem-limit', dest='vmemlimit', type=int, default=0)
parser.add_argument('--data', dest='dataprep', type=str, default='/nfs/projects/funcom/data/javastmt/q90')
parser.add_argument('--outdir', dest='outdir', type=str, default='outdir')
parser.add_argument('--hops', dest='hops', type=int, default= 5)
parser.add_argument('--dtype', dest='dtype', type=str, default='float32')
parser.add_argument('--tf-loglevel', dest='tf_loglevel', type=str, default='3')
parser.add_argument('--datfile', dest='datfile', type=str, default='dataset.pkl')
parser.add_argument('--only-print-summary', dest='onlyprintsummary', action='store_true', default=False)
parser.add_argument('--load-model', dest='load_model', action='store_true', default=False)
parser.add_argument('--model-file', dest='model_file', type=str, default=None)
args = parser.parse_args()
outdir = args.outdir
dataprep = args.dataprep
gpu = args.gpu
hops = args.hops
batch_size = args.batch_size
batchgen = args.batchgen
epochs = args.epochs
lmmode = args.lmmode
modeltype = args.modeltype
withgraph = args.withgraph
withcalls = args.withcalls
withbiodats = False
withsimmat = args.withsimmats
simmatfile = args.simmatfile
randresplit = args.randresplit
losstype = args.losstype
vmemlimit = args.vmemlimit
onlyprintsummary = args.onlyprintsummary
load_model = args.load_model
modelfile = args.model_file
#datfile = args.datfile
if args.withbiodats != 'vanilla':
withbiodats = True
biodatfile = args.withbiodats
os.environ['TF_CPP_MIN_LOG_LEVEL'] = args.tf_loglevel
os.environ['CUDA_VISIBLE_DEVICES'] = gpu
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
try:
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
if(vmemlimit > 0):
tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=vmemlimit)])
except RuntimeError as e:
print(e)
#if(vmemlimit > 0):
# if gpus:
# try:
# tf.config.experimental.set_virtual_device_configuration(gpus[0], [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=vmemlimit)])
# except RuntimeError as e:
# print(e)
import tensorflow.keras as keras
import tensorflow.keras.utils
#from tensorflow.keras.callbacks import ModelCheckpoint, LambdaCallback, Callback
import tensorflow.keras.backend as K
from model import create_model
K.set_floatx(args.dtype)
if batchgen == 'qs':
from qs_myutils import prep, drop, batch_gen, seq2sent
else:
from myutils import prep, drop, batch_gen_lm, batch_gen, seq2sent
prep('loading sequences... ')
sqlfile = '{}/rawdats.sqlite'.format(dataprep)
extradata = pickle.load(open('%s/dataset_short.pkl' % (dataprep), 'rb'))
seqdata = h5py.File('%s/dataset_seqs.h5' % (dataprep), 'r')
drop()
if withgraph:
prep('loading graph data... ')
graphdata = pickle.load(open('%s/dataset_graph.pkl' % (dataprep), 'rb'))
for k, v in extradata.items():
graphdata[k] = v
extradata = graphdata
drop()
if withcalls:
prep('loading call data... ')
callnodes = pickle.load(open('%s/callsnodes.pkl' % (dataprep), 'rb'))
calledges = pickle.load(open('%s/callsedges.pkl' % (dataprep), 'rb'))
callnodesdata = pickle.load(open('%s/callsnodedata.pkl' % (dataprep), 'rb'))
extradata['callnodes'] = callnodes
extradata['calledges'] = calledges
extradata['callnodedata'] = callnodesdata
drop()
if withbiodats:
prep('loading biomodel results... ')
biodats = pickle.load(open(biodatfile, 'rb'))
extradata['biodats'] = biodats
drop()
if withsimmat:
prep('loading target comwords distribution... ')
softmax_usemat = pickle.load(open('%s/useopt/%s' % (dataprep, simmatfile), 'rb'))
extradata['target_dist'] = softmax_usemat
drop()
prep('loading tokenizers... ')
comstok = extradata['comstok']
tdatstok = extradata['tdatstok']
sdatstok = tdatstok
smlstok = extradata['smlstok']
if withgraph:
graphtok = extradata['graphtok']
drop()
if batchgen == 'qs':
steps = int(np.array(seqdata.get('/ctrain').shape[0])/batch_size)*int(np.array(seqdata.get('/ctrain')).shape[1])
valsteps = int(np.array(seqdata.get('/cval').shape[0])/batch_size)*int(np.array(seqdata.get('/ctrain')).shape[1])
else:
steps = int(np.array(seqdata.get('/ctrain').shape[0])/batch_size)
valsteps = int(np.array(seqdata.get('/cval').shape[0])/batch_size)
tdatvocabsize = tdatstok.vocab_size
comvocabsize = comstok.vocab_size
smlvocabsize = smlstok.vocab_size
print('tdatvocabsize %s' % (tdatvocabsize))
print('comvocabsize %s' % (comvocabsize))
print('smlvocabsize %s' % (smlvocabsize))
print('batch size {}'.format(batch_size))
print('steps {}'.format(steps))
print('training data size {}'.format(steps*batch_size))
print('vaidation data size {}'.format(valsteps*100))
print('------------------------------------------')
config = dict()
config['hops'] = hops
config['tdatvocabsize'] = tdatvocabsize
config['comvocabsize'] = comvocabsize
config['smlvocabsize'] = smlvocabsize
config['modelfile'] = ''
try:
config['fidloc'] = extradata['fidloc']
config['locfid'] = extradata['locfid']
config['comstok'] = extradata['comstok']
config['comlen'] = int(np.array(seqdata.get('/ctrain')).shape[1])
config['tdatlen'] = int(np.array(seqdata.get('/dttrain')).shape[1])
config['smllen'] = int(np.array(seqdata.get('/strain')).shape[1])
config['batchgen'] = batchgen
config['target_dist'] = extradata['target_dist']
config['sdatlen'] = extradata['config']['sdatlen']
except KeyError:
pass # some configurations do not have all data, which is fine
config['batch_size'] = batch_size
config['loss_type'] = losstype
print(config.keys())
prep('creating model... ')
if(load_model):
(modeltype_load, mid_load, timestart_load) = modelfile.split('_')
(timestart_load, ext_load) = timestart_load.split('.')
modeltype_load = modeltype_load.split('/')[-1]
config = pickle.load(open(outdir+'/histories/'+modeltype_load+'_conf_'+timestart_load+'.pkl', 'rb'))
config['modelfile'] = '../'+modelfile
config, model = create_model(modeltype, config)
print(modeltype)
model.load_weights(modelfile)
#model = keras.models.load_model(modelfile, custom_objects={"tf":tf, "keras":keras, "GCNLayer":GCNLayer)
#print(model.summary())
else:
config, model = create_model(modeltype, config)
drop()
print(model.summary())
if onlyprintsummary:
sys.exit()
if lmmode:
gen = batch_gen_lm(seqdata, extradata, 'train', config)
else:
gen = batch_gen(seqdata, extradata, 'train', config)
#checkpoint = ModelCheckpoint(outdir+'/'+modeltype+'_E{epoch:02d}_TA{acc:.2f}_VA{val_acc:.2f}_VB{val_bleu:}.h5', monitor='val_loss')
checkpoint = ModelCheckpoint(outdir+'/models/'+modeltype+'_E{epoch:02d}_'+str(timestart)+'.h5')
savehist = HistoryCallback()
savehist.setCatchExit(outdir, modeltype, timestart, config)
print('timestart id: ', timestart)
if lmmode:
valgen = batch_gen_lm(seqdata, extradata, 'val', config)
else:
valgen = batch_gen(seqdata, extradata, 'val', config)
# If you want it to calculate BLEU Score after each epoch use callback_valgen and test_cb
#####
#callback_valgen = batch_gen_train_bleu(seqdata, comvocabsize, 'val', modeltype, batch_size=batch_size)
#test_cb = mycallback(callback_valgen, steps)
#####
callbacks = [ checkpoint, savehist ]
try:
history = model.fit(x=gen, steps_per_epoch=steps, epochs=epochs, verbose=1, max_queue_size=8, workers=1, use_multiprocessing=False, callbacks=callbacks, validation_data=valgen, validation_steps=valsteps)
except Exception as ex:
print(ex)
traceback.print_exc(file=sys.stdout)