-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
142 lines (126 loc) · 4.92 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
import os
import json
import datetime
import numpy as np
import tensorflow as tf
from init_args import init_args
from match_lstm.match_lstm import MatchLSTM
from cudnn_match_lstm.match_lstm import CudnnMatchLSTM
from base_model.base_model import BaseModel
from RNet.RNet import RNet
from cudnnRNet.RNet import cudnnRNet
from lstm.lstm import LSTM
from QANet.QANet import QANet
from qa_system import QASystem
from load_data import SquadDataset
from logger import setup_logger
def main():
FLAGS = init_args()
kwargs = FLAGS.flag_values_dict()
if not FLAGS.use_out_dir:
timestamp = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
out_dir = os.path.join(FLAGS.out_dir, timestamp)
os.makedirs(os.path.join(out_dir))
else:
timestamp = FLAGS.use_out_dir
out_dir = os.path.join(FLAGS.out_dir, timestamp)
# logger
logger = setup_logger(name='phrase_level_qa', out_dir=out_dir)
logger.info("saving to {}".format(out_dir))
# print parameters
logger.info("Parameters:")
for attr, value in sorted(FLAGS.flag_values_dict().items()):
logger.info("{}={}".format(attr.upper(), value))
# load evaluation files
with open(os.path.join(kwargs["data_dir"], "train_eval.json"), "r") as fh:
train_eval_file = json.load(fh)
with open(os.path.join(kwargs["data_dir"], "dev_eval.json"), "r") as fh:
dev_eval_file = json.load(fh)
train_set = SquadDataset(data_dir=kwargs['data_dir'], use='train', batch_size=FLAGS.batch_size)
train_init, train_iter = train_set.setup_data_pipeline(shuffle=True)
train_batches = train_set.get_num_batches()
dev_set = SquadDataset(data_dir=kwargs['data_dir'], use='dev', batch_size=FLAGS.batch_size)
dev_init, dev_iter = dev_set.setup_data_pipeline(shuffle=False)
dev_batches = 150 # dev_set.get_num_batches()
logger.info("INFO: train: {} dev: {}".format(len(train_set), len(dev_set)))
initW = np.fromfile(os.path.join(
FLAGS.data_dir, 'initW_' + FLAGS.use + '.dat')).reshape((-1, FLAGS.word_emb_size))
if FLAGS.model_name == 'base':
logger.info("INFO: using Base Model")
model = BaseModel(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
elif FLAGS.model_name == 'lstm':
logger.info("INFO: using LSTM Decoder")
model = LSTM(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
elif FLAGS.model_name == 'match':
logger.info("INFO: using Match-LSTM")
model = MatchLSTM(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
elif FLAGS.model_name == 'cudnn_match':
logger.info("INFO: using Match-LSTM")
model = CudnnMatchLSTM(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
elif FLAGS.model_name == 'rnet':
logger.info("INFO: using RNet")
model = RNet(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
elif FLAGS.model_name == 'cudnn-rnet':
logger.info("INFO: using cudnn-RNet")
model = cudnnRNet(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
elif FLAGS.model_name == 'qanet':
logger.info("INFO: using QANet")
model = QANet(
embeddings=initW,
output_types=train_set.get_output_types(),
output_shapes=train_set.get_output_shapes(),
**kwargs
)
else:
raise NotImplementedError("model name not implemented")
qa_sys = QASystem(model=model,
outdir=out_dir,
timestamp=timestamp,
inits=[train_init, dev_init],
iterators=[train_iter, dev_iter],
num_batches=[train_batches, dev_batches],
**kwargs)
logger.info("INFO: qa system defined")
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=FLAGS.gpu_fraction)
session_conf = tf.ConfigProto(
allow_soft_placement=FLAGS.allow_soft_placement,
log_device_placement=FLAGS.log_device_placement,
gpu_options=gpu_options)
sess = tf.Session(config=session_conf)
logger.info("INFO: started session")
with sess.as_default():
qa_sys.init_model(sess)
qa_sys.train(sess, train_eval_file, dev_eval_file)
logger.info("INFO: training finished")
if __name__ == '__main__':
main()