-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
206 lines (167 loc) · 7.61 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
import os
import time
import json
import argparse
import numpy as np
import tensorflow as tf
import jieba
from src.config import Config
from src.judger import Judger
from src.data_reader import DataReader
from src.model import get_model
from src.util import read_dict, load_embedding, make_batch_iter, pad_batch, get_task_result, id_2_impr
jieba.add_word('PAD', 9999, 'n')
jieba.add_word('UNK', 9999, 'n')
jieba.add_word('NUM', 9999, 'n')
jieba.add_word('TIME', 9999, 'n')
parser = argparse.ArgumentParser()
parser.add_argument('--model', '-m', type=str, required=True)
parser.add_argument('--num_epoch', type=int, default=30)
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--optimizer', type=str, default='Adam')
parser.add_argument('--lr', type=float, default=0.001)
parser.add_argument('--embedding_trainable', action='store_true', default=False)
parser.add_argument('--use_batch_norm', action='store_true', default=False)
args = parser.parse_args()
current_model = args.model
num_epoch = args.num_epoch
batch_size = args.batch_size
optimizer = args.optimizer
lr = args.lr
embedding_trainable = args.embedding_trainable
use_batch_norm = args.use_batch_norm
config = Config('./', current_model,
num_epoch=num_epoch, batch_size=batch_size, optimizer=optimizer, lr=lr,
embedding_trainable=embedding_trainable, use_batch_norm=use_batch_norm)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
config_proto = tf.ConfigProto(allow_soft_placement=True) # 创建配置,允许将无法放入GPU的操作放在CUP上执行
config_proto.gpu_options.allow_growth = True # 运行时动态增加内存使用量
judger = Judger(config.accu_dict, config.art_dict)
def save_result(outputs, result_file, id_2_accu, id_2_art):
task_1_output, task_2_output, task_3_output = outputs
task_1_result = [get_task_result(s, config.threshold) for s in task_1_output]
task_2_result = [get_task_result(s, config.threshold) for s in task_2_output]
task_3_result = np.argmax(task_3_output, axis=-1)
print('write file: ', result_file)
with open(result_file, 'w', encoding='utf-8') as fout:
for t1, t2, t3 in zip(task_1_result, task_2_result, task_3_result):
t1 = [id_2_accu[v] for v in t1]
t2 = [int(id_2_art[v]) for v in t2]
t3 = id_2_impr(t3)
res = {
'accusation': t1,
'relevant_articles': t2,
'imprisonment': t3
}
print(json.dumps(res, ensure_ascii=False), file=fout)
def inference(sess, model, batch_iter, art_data, verbose=True):
art, art_len = art_data
task_1_output = []
task_2_output = []
task_3_output = []
start_time = time.time()
for i, batch in enumerate(batch_iter):
if verbose:
print('processing batch: %6d' % i, end='\r')
fact, fact_len, accu, relevant_art, impr = list(zip(*batch))
fact = pad_batch(fact, config.pad_id, config.sequence_len)
bs = len(fact)
feed_dict = {
model.batch_size: bs,
model.fact: fact,
model.fact_len: fact_len,
model.art: [art] * bs,
model.art_len: [art_len] * bs,
model.accu: accu,
model.relevant_art: relevant_art,
model.impr: impr
}
_task_1_output, _task_2_output = sess.run(
[model.task_1_output, model.task_2_output],
feed_dict=feed_dict
)
task_1_output.extend(_task_1_output.tolist())
task_2_output.extend(_task_2_output.tolist())
task_3_output.extend([[0.0] * config.impr_num] * bs)
print('\ncost time: %.4fs' % (time.time() - start_time))
return task_1_output, task_2_output, task_3_output
def run_epoch(sess, model, batch_iter, art_data, verbose=True):
art, art_len = art_data
steps = 0
total_loss = 0.0
_global_step = 0
start_time = time.time()
for batch in batch_iter:
fact, fact_len, accu, relevant_art, impr = list(zip(*batch))
fact = pad_batch(fact, config.pad_id, config.sequence_len)
bs = len(fact)
feed_dict = {
model.batch_size: bs,
model.fact: fact,
model.fact_len: fact_len,
model.art: [art] * bs,
model.art_len: [art_len] * bs,
model.accu: accu,
model.relevant_art: relevant_art,
model.impr: impr
}
_, _loss, _global_step = sess.run(
[model.train_op, model.loss, model.global_step],
feed_dict=feed_dict
)
steps += 1
total_loss += _loss
if verbose and steps % 1000 == 1:
current_time = time.time()
print('After %6d batch(es), global step is %6d, loss is %.4f, cost time %.4fs'
% (steps, _global_step, _loss, current_time - start_time))
start_time = current_time
return total_loss / steps, _global_step
def train():
if not os.path.exists(config.result_dir):
os.makedirs(config.result_dir)
print('load data...')
word_2_id, id_2_word = read_dict(config.word_dict)
accu_2_id, id_2_accu = read_dict(config.accu_dict)
art_2_id, id_2_art = read_dict(config.art_dict)
if os.path.exists(config.word2vec_model):
embedding_matrix = load_embedding(config.word2vec_model, word_2_id.keys())
else:
embedding_matrix = np.random.uniform(-0.5, 0.5, [len(word_2_id), config.embedding_size])
data_reader = DataReader(config)
train_data = data_reader.read_train_data(word_2_id, accu_2_id, art_2_id)
valid_data = data_reader.read_valid_data(word_2_id, accu_2_id, art_2_id)
art_data = data_reader.read_article(art_2_id.keys(), word_2_id)
print('build model...')
with tf.variable_scope('model'):
model = get_model(config, embedding_matrix, is_training=True)
print('========== Trainable Variables ==========')
for v in tf.trainable_variables():
print(v)
saver = tf.train.Saver(max_to_keep=1)
with tf.Session(config=config_proto) as sess:
tf.global_variables_initializer().run()
saver.save(sess, config.model_file)
for i in range(config.num_epoch):
print('========== Epoch %2d Train ==========' % (i + 1))
train_batch_iter = make_batch_iter(list(zip(*train_data)), config.batch_size, shuffle=True)
train_loss, _ = run_epoch(sess, model, train_batch_iter, art_data, verbose=True)
print('The average train loss of epoch %2d is %.4f' % ((i + 1), train_loss))
print('========== Epoch %2d Valid ==========' % (i + 1))
valid_batch_iter = make_batch_iter(list(zip(*valid_data)), config.batch_size, shuffle=False)
outputs = inference(sess, model, valid_batch_iter, art_data, verbose=True)
print('========== Saving model ==========')
saver.save(sess, config.model_file)
save_result(outputs, config.valid_result, id_2_accu, id_2_art)
result = judger.get_result(config.valid_data, config.valid_result)
accu_micro_f1, accu_macro_f1 = judger.calc_f1(result[0])
article_micro_f1, article_macro_f1 = judger.calc_f1(result[1])
score = [(accu_micro_f1 + accu_macro_f1) / 2, (article_micro_f1 + article_macro_f1) / 2]
print('Micro-F1 of accusation: %.4f' % accu_micro_f1)
print('Macro-F1 of accusation: %.4f' % accu_macro_f1)
print('Micro-F1 of relevant articles: %.4f' % article_micro_f1)
print('Macro-F1 of relevant articles: %.4f' % article_macro_f1)
print('Score: ', score)
if __name__ == '__main__':
train()