-
Notifications
You must be signed in to change notification settings - Fork 2
/
translation_train.py
337 lines (266 loc) · 9.93 KB
/
translation_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
330
331
332
333
334
335
336
337
import tensorflow as tf
import numpy as np
import transformer
import inference_helper
import bucket_data_helper
import os
from tqdm import tqdm
import warnings
import argparse
warnings.filterwarnings('ignore')
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
parser = argparse.ArgumentParser()
parser.add_argument(
'-train_path_2017',
help="train_path",
required=True
)
parser.add_argument(
'-valid_path_2014',
help="valid_path",
required=True
)
parser.add_argument(
'-test_path_2015',
help="test_path",
required=True
)
parser.add_argument(
'-test_path_2016',
help="test_path",
required=True
)
parser.add_argument(
'-voca_path',
help="Vocabulary_path",
required=True
)
args = parser.parse_args()
train_path_2017 = args.train_path_2017
valid_path_2014 = args.valid_path_2014
test_path_2015 = args.test_path_2015
test_path_2016 = args.test_path_2016
voca_path = args.voca_path
saver_path = './saver/'
tensorboard_path = './tensorboard/'
def read_voca(path):
sorted_voca = []
with open(path, 'r', encoding='utf-8') as f:
for bpe_voca in f:
bpe_voca = bpe_voca.strip()
if bpe_voca:
bpe_voca = bpe_voca.split()
sorted_voca.append(bpe_voca)
return sorted_voca
def _read_csv(path):
data = np.loadtxt(
path,
delimiter=",",
dtype=np.int32,
ndmin=2 # csv가 1줄이여도 2차원으로 출력.
)
return data
def _read_txt(path):
with open(path, 'r', encoding='utf-8') as f:
documents = f.readlines()
data = []
for sentence in documents:
data.append(sentence.strip().split())
return data
def _get_bucket_name(path):
bucket = {}
for filename in os.listdir(path):
bucket[filename.split('.')[-2].split('_')[-1]] = 1
return tuple(bucket.keys())
def make_bpe2idx(voca):
bpe2idx = {'</p>':0, '</UNK>':1, '</g>':2, '</e>':3}
idx2bpe = ['</p>', '</UNK>', '</g>', '</e>']
idx = 4
for word, _ in voca:
bpe2idx[word] = idx
idx += 1
idx2bpe.append(word)
return bpe2idx, idx2bpe
def read_data_set(path, target_type='csv'):
buckets = _get_bucket_name(path)
dictionary = {}
total_sentence = 0
for i in tqdm(range(len(buckets)), ncols=50):
bucket = buckets[i] # '(35, 35)' string
source_path = os.path.join(path, 'source_'+bucket+'.csv')
sentence = _read_csv(source_path)
if target_type == 'csv':
target_path = os.path.join(path, 'target_'+bucket+'.csv')
target = _read_csv(target_path)
else:
target_path = os.path.join(path, 'target_'+bucket+'.txt')
target = _read_txt(target_path)
# 개수가 0인 bucket은 버림.
sentence_num = len(sentence)
if sentence_num != 0:
total_sentence += sentence_num
sentence_bucket, target_bucket = bucket[1:-1].split(',')
tuple_bucket = (int(sentence_bucket), int(target_bucket))
dictionary[tuple_bucket] = [sentence, target]
print('data_path:', path, 'data_size:', total_sentence, '\n')
return dictionary
def get_lr(embedding_size, step_num):
'''
https://ufal.mff.cuni.cz/pbml/110/art-popel-bojar.pdf
step_num(training_steps): number of iterations, ie. the number of times the optimizer update was run
This number also equals the number of mini batches that were processed.
'''
lr = (embedding_size**-0.5) * min( (step_num**-0.5), (step_num*(warmup_steps**-1.5)) )
return lr
def train(model, data, epoch):
loss = 0
dataset = data.get_dataset(bucket_shuffle=True, dataset_shuffle=True)
total_iter = len(dataset)
for i in tqdm(range(total_iter), ncols=50):
step_num = ((epoch-1)*total_iter)+(i+1)
lr = get_lr(embedding_size=embedding_size, step_num=step_num) # epoch: [1, @], i:[0, total_iter)
encoder_input, temp = dataset[i]
decoder_input = temp[:, :-1]
#print(encoder_input.shape, decoder_input.shape, 4*np.multiply(*encoder_input.shape)*512/1000000000,"GB", 4*np.multiply(*decoder_input.shape)*40297/1000000000,'GB')
target = temp[:, 1:] # except '</g>'
train_loss, _ = sess.run([model.train_cost, model.minimize],
{
model.lr:lr,
model.encoder_input:encoder_input,
model.decoder_input:decoder_input,
model.target:target,
model.keep_prob:0.9 # dropout rate = 0.1
}
)
loss += train_loss
#if (i+1) % 5000 == 0:
# print(i+1,loss/(i+1), 'lr:', lr)
print('current step_num:', step_num, 'lr:', lr)
return loss/total_iter
def infer(model, data):
pred_list = []
target_list = []
dataset = data.get_dataset(bucket_shuffle=False, dataset_shuffle=False)
total_iter = len(dataset)
for i in tqdm(range(total_iter), ncols=50):
encoder_input, target = dataset[i]
target_length = encoder_input.shape[1] + 30
pred = infer_helper.decode(encoder_input, target_length) # [N, target_length]
del encoder_input
first_eos = np.argmax(pred == bpe2idx['</e>'], axis=1) # [N] 최초로 eos 나오는 index.
for _pred, _first_eos, _target in (zip(pred, first_eos, target)):
if _first_eos != 0:
_pred = _pred[:_first_eos]
_pred = [idx2bpe[idx] for idx in _pred] # idx2bpe
_pred = ''.join(_pred) # 공백 없이 전부 concat
_pred = _pred.replace('</w>', ' ') # 공백 symbol을 공백으로 치환.
pred_list.append(_pred.split())
target_list.append([_target])
bleu = utils.bleu(target_list, pred_list) * 100
return bleu
def run(model, trainset2017, validset2014, testset2015, testset2016, restore=0):
if restore != 0:
model.saver.restore(sess, saver_path+str(restore)+".ckpt")
print('restore:', restore)
with tf.name_scope("tensorboard"):
train_loss_tensorboard_2017 = tf.placeholder(tf.float32, name='train_loss_2017')
valid_bleu_tensorboard_2014 = tf.placeholder(tf.float32, name='valid_bleu_2014')
test_bleu_tensorboard_2015 = tf.placeholder(tf.float32, name='test_bleu_2015')
test_bleu_tensorboard_2016 = tf.placeholder(tf.float32, name='test_bleu_2016')
train_summary_2017 = tf.summary.scalar("train_loss_wmt17", train_loss_tensorboard_2017)
valid_summary_2014 = tf.summary.scalar("valid_bleu_newstest2014", valid_bleu_tensorboard_2014)
test_summary_2015 = tf.summary.scalar("test_bleu_newstest2015", test_bleu_tensorboard_2015)
test_summary_2016 = tf.summary.scalar("test_bleu_newstest2016", test_bleu_tensorboard_2016)
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter(tensorboard_path, sess.graph)
#merged_train_valid = tf.summary.merge([train_summary, valid_summary])
#merged_test = tf.summary.merge([test_summary])
if not os.path.exists(saver_path):
print("create save directory")
os.makedirs(saver_path)
for epoch in range(restore+1, 20000+1):
#train
train_loss_2017 = train(model, trainset2017, epoch)
#save
model.saver.save(sess, saver_path+str(epoch)+".ckpt")
#validation
valid_bleu_2014 = infer(model, validset2014)
#test
test_bleu_2015 = infer(model, testset2015)
test_bleu_2016 = infer(model, testset2016)
print("epoch:", epoch)
print('train_loss_wmt17:', train_loss_2017, 'valid_bleu_newstest2014:', valid_bleu_2014)
print('test_bleu_newstest2015:', test_bleu_2015, 'test_bleu_newstest2016:', test_bleu_2016, '\n')
#tensorboard
summary = sess.run(merged, {
train_loss_tensorboard_2017:train_loss_2017,
valid_bleu_tensorboard_2014:valid_bleu_2014,
test_bleu_tensorboard_2015:test_bleu_2015,
test_bleu_tensorboard_2016:test_bleu_2016,
}
)
writer.add_summary(summary, epoch)
print('Data read') # key: bucket_size(tuple) , value: [source, target]
train_dict_2017 = read_data_set(train_path_2017)
valid_dict_2014 = read_data_set(valid_path_2014, 'txt')
test_dict_2015 = read_data_set(test_path_2015, 'txt')
test_dict_2016 = read_data_set(test_path_2016, 'txt')
train_set_2017 = bucket_data_helper.bucket_data(train_dict_2017, batch_token = 11000) # batch_token // len(sentence||target token) == batch_size
valid_set_2014 = bucket_data_helper.bucket_data(valid_dict_2014, batch_token = 9000) # batch_token // len(sentence||target token) == batch_size
test_set_2015 = bucket_data_helper.bucket_data(test_dict_2015, batch_token = 9000) # batch_token // len(sentence||target token) == batch_size
test_set_2016 = bucket_data_helper.bucket_data(test_dict_2016, batch_token = 9000) # batch_token // len(sentence||target token) == batch_size
del train_dict_2017, valid_dict_2014, test_dict_2015, test_dict_2016
print("Model read")
config = tf.ConfigProto()
config.gpu_options.allow_growth=True
sess = tf.Session(config=config)
voca = read_voca(voca_path)
bpe2idx, idx2bpe = make_bpe2idx(voca)
warmup_steps = 4000 * 8 # paper warmup_steps: 4000(with 8-gpus), so warmup_steps of single gpu: 4000*8
embedding_size = 512
encoder_decoder_stack = 6
multihead_num = 8
label_smoothing = 0.1
beam_width = 4
length_penalty = 0.6
print('voca_size:', len(bpe2idx))
print('warmup_steps:', warmup_steps)
print('embedding_size:', embedding_size)
print('encoder_decoder_stack:', encoder_decoder_stack)
print('multihead_num:', multihead_num)
print('label_smoothing:', label_smoothing)
print('beam_width:', beam_width)
print('length_penalty:', length_penalty, '\n')
model = transformer.Transformer(
sess = sess,
voca_size = len(bpe2idx),
embedding_size = embedding_size,
is_embedding_scale = True,
PE_sequence_length = 300,
encoder_decoder_stack = encoder_decoder_stack,
multihead_num = multihead_num,
eos_idx=bpe2idx['</e>'],
pad_idx=bpe2idx['</p>'],
label_smoothing=label_smoothing
)
# beam search
infer_helper = inference_helper.beam(
sess = sess,
model = model,
go_idx = bpe2idx['</g>'],
eos_idx = bpe2idx['</e>'],
beam_width = beam_width,
length_penalty = length_penalty
)
# bleu util
utils = inference_helper.utils()
print('run')
run(model, train_set_2017, valid_set_2014, test_set_2015, test_set_2016)
'''
# greedy search
infer_helper = inference_helper.greedy(
sess = sess,
model = model,
go_idx = bpe2idx['</g>']
)
'''