-
Notifications
You must be signed in to change notification settings - Fork 0
/
src_cons_transformer.py
executable file
·613 lines (490 loc) · 23.4 KB
/
src_cons_transformer.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# coding=utf-8
# Copyright 2018 The THUMT Authors
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
import math
import tensorflow as tf
import thumt.interface as interface
import thumt.layers as layers
def _layer_process(x, mode):
if not mode or mode == "none":
return x
elif mode == "layer_norm":
return layers.nn.layer_norm(x)
else:
raise ValueError("Unknown mode %s" % mode)
def _residual_fn(x, y, keep_prob=None):
if keep_prob and keep_prob < 1.0:
y = tf.nn.dropout(y, keep_prob)
return x + y
def _ffn_layer(inputs, hidden_size, output_size, keep_prob=None,
dtype=None, scope=None):
with tf.variable_scope(scope, default_name="ffn_layer", values=[inputs],
dtype=dtype):
with tf.variable_scope("input_layer"):
hidden = layers.nn.linear(inputs, hidden_size, True, True)
hidden = tf.nn.relu(hidden)
if keep_prob and keep_prob < 1.0:
hidden = tf.nn.dropout(hidden, keep_prob)
with tf.variable_scope("output_layer"):
output = layers.nn.linear(hidden, output_size, True, True)
return output
def transformer_encoder(inputs, bias, params, dtype=None, scope=None):
with tf.variable_scope(scope, default_name="encoder", dtype=dtype,
values=[inputs, bias]):
#inputs = tf.Print(inputs, [dtype], 'dtype')
x = inputs
for layer in range(params.num_encoder_layers):
with tf.variable_scope("layer_%d" % layer):
with tf.variable_scope("self_attention"):
y = layers.attention.multihead_attention(
_layer_process(x, params.layer_preprocess),
None,
bias,
params.num_heads,
params.attention_key_channels or params.hidden_size,
params.attention_value_channels or params.hidden_size,
params.hidden_size,
1.0 - params.attention_dropout
)
weights = y["weights"]
y = y["outputs"]
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
with tf.variable_scope("feed_forward"):
y = _ffn_layer(
_layer_process(x, params.layer_preprocess),
params.filter_size,
params.hidden_size,
1.0 - params.relu_dropout,
)
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
outputs = _layer_process(x, params.layer_preprocess)
return outputs, weights
def transformer_decoder(inputs, memory, bias, mem_bias, params, state=None,
dtype=None, scope=None):
with tf.variable_scope(scope, default_name="decoder", dtype=dtype,
values=[inputs, memory, bias, mem_bias]):
x = inputs
next_state = {}
for layer in range(params.num_decoder_layers):
layer_name = "layer_%d" % layer
with tf.variable_scope(layer_name):
layer_state = state[layer_name] if state is not None else None
with tf.variable_scope("self_attention"):
y = layers.attention.multihead_attention(
_layer_process(x, params.layer_preprocess),
None,
bias,
params.num_heads,
params.attention_key_channels or params.hidden_size,
params.attention_value_channels or params.hidden_size,
params.hidden_size,
1.0 - params.attention_dropout,
state=layer_state
)
if layer_state is not None:
next_state[layer_name] = y["state"]
y = y["outputs"]
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
with tf.variable_scope("encdec_attention"):
y = layers.attention.multihead_attention(
_layer_process(x, params.layer_preprocess),
memory,
mem_bias,
params.num_heads,
params.attention_key_channels or params.hidden_size,
params.attention_value_channels or params.hidden_size,
params.hidden_size,
1.0 - params.attention_dropout,
)
y = y["outputs"]
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
with tf.variable_scope("feed_forward"):
y = _ffn_layer(
_layer_process(x, params.layer_preprocess),
params.filter_size,
params.hidden_size,
1.0 - params.relu_dropout,
)
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
outputs = _layer_process(x, params.layer_preprocess)
if state is not None:
return outputs, next_state
return outputs
def transformer_decoder_output_cur_encdec_atten(inputs, memory, bias, mem_bias, params, state=None,
dtype=None, scope=None):
"""
output current step's encdec attention result
"""
with tf.variable_scope(scope, default_name="decoder", dtype=dtype,
values=[inputs, memory, bias, mem_bias]):
x = inputs
next_state = {}
att_weight=[]
for layer in range(params.num_decoder_layers):
layer_name = "layer_%d" % layer
with tf.variable_scope(layer_name):
layer_state = state[layer_name] if state is not None else None
with tf.variable_scope("self_attention"):
y = layers.attention.multihead_attention(
_layer_process(x, params.layer_preprocess),
None,
bias,
params.num_heads,
params.attention_key_channels or params.hidden_size,
params.attention_value_channels or params.hidden_size,
params.hidden_size,
1.0 - params.attention_dropout,
state=layer_state
)
if layer_state is not None:
next_state[layer_name] = y["state"]
y = y["outputs"]
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
with tf.variable_scope("encdec_attention"):
y = layers.attention.multihead_attention(
_layer_process(x, params.layer_preprocess),
memory,
mem_bias,
params.num_heads,
params.attention_key_channels or params.hidden_size,
params.attention_value_channels or params.hidden_size,
params.hidden_size,
1.0 - params.attention_dropout,
)
combined_weight = tf.squeeze(y["weights"], 2) # remove the 1 dimension and become 2 8 45
y = y["outputs"]
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
att_weight.append(combined_weight)
with tf.variable_scope("feed_forward"):
y = _ffn_layer(
_layer_process(x, params.layer_preprocess),
params.filter_size,
params.hidden_size,
1.0 - params.relu_dropout,
)
x = _residual_fn(x, y, 1.0 - params.residual_dropout)
x = _layer_process(x, params.layer_postprocess)
outputs = _layer_process(x, params.layer_preprocess)
if state is not None:
return outputs, next_state, att_weight
return outputs
def encoding_graph(features, mode, params):
if mode != "train":
params.residual_dropout = 0.0
params.attention_dropout = 0.0
params.relu_dropout = 0.0
params.label_smoothing = 0.0
hidden_size = params.hidden_size
src_seq = features["source"]
#src_seq = tf.Print(src_seq, [features["align_pos"]], "align_pos", 10, 1000)
src_len = features["source_length"]
src_mask = tf.sequence_mask(src_len,
maxlen=tf.shape(features["source"])[1],
dtype=tf.float32)
svocab = params.vocabulary["source"]
src_vocab_size = len(svocab)
initializer = tf.random_normal_initializer(0.0, params.hidden_size ** -0.5)
if params.shared_source_target_embedding:
src_embedding = tf.get_variable("weights",
[src_vocab_size, hidden_size],
initializer=initializer)
else:
src_embedding = tf.get_variable("source_embedding",
[src_vocab_size, hidden_size],
initializer=initializer)
#src_embedding = tf.cast(src_embedding, dtype=tf.float64)
bias = tf.get_variable("bias", [hidden_size])
# id => embedding
# src_seq: [batch, max_src_length]
inputs = tf.gather(src_embedding, src_seq) * (hidden_size ** 0.5)
inputs = inputs * tf.expand_dims(src_mask, -1)
# Preparing encoder
encoder_input = tf.nn.bias_add(inputs, bias)
encoder_input = layers.attention.add_timing_signal(encoder_input)
#encoder_input = add_timing_signal_float64(encoder_input)
enc_attn_bias = layers.attention.attention_bias(src_mask, "masking")
if params.residual_dropout:
keep_prob = 1.0 - params.residual_dropout
encoder_input = tf.nn.dropout(encoder_input, keep_prob)
encoder_output, weights = transformer_encoder(encoder_input, enc_attn_bias, params) #, dtype=tf.float64
# encoder_output = tf.cast(encoder_output, dtype=tf.float32)
# encoder_output = tf.cast(encoder_output, dtype=tf.float32)
#
return encoder_output, weights
def decoding_graph(features, state, mode, params):
if mode != "train":
params.residual_dropout = 0.0
params.attention_dropout = 0.0
params.relu_dropout = 0.0
params.label_smoothing = 0.0
tgt_seq = features["target"]
src_len = features["source_length"]
tgt_len = features["target_length"]
src_mask = tf.sequence_mask(src_len,
maxlen=tf.shape(features["source"])[1],
dtype=tf.float32)
tgt_mask = tf.sequence_mask(tgt_len,
maxlen=tf.shape(features["target"])[1],
dtype=tf.float32)
hidden_size = params.hidden_size
tvocab = params.vocabulary["target"]
tgt_vocab_size = len(tvocab)
initializer = tf.random_normal_initializer(0.0, params.hidden_size ** -0.5)
if params.shared_source_target_embedding:
with tf.variable_scope(tf.get_variable_scope(), reuse=True):
tgt_embedding = tf.get_variable("weights",
[tgt_vocab_size, hidden_size],
initializer=initializer)
else:
tgt_embedding = tf.get_variable("target_embedding",
[tgt_vocab_size, hidden_size],
initializer=initializer)
if params.shared_embedding_and_softmax_weights:
weights = tgt_embedding
else:
weights = tf.get_variable("softmax", [tgt_vocab_size, hidden_size],
initializer=initializer)
# id => embedding
# tgt_seq: [batch, max_tgt_length]
#tgt_seq = tf.Print(tgt_seq, [tgt_seq], "tgt_seq", 1000, 1000)
targets = tf.gather(tgt_embedding, tgt_seq) * (hidden_size ** 0.5)
#targets = tf.Print(targets, [targets], "targets_after gather embedding", 10, 1000)
targets = targets * tf.expand_dims(tgt_mask, -1)
#targets = tf.Print(targets, [tf.shape(targets), tf.shape(tgt_mask)], "tf.shape(targets), tf.shape(tgt_mask)", 10, 1000)
# Preparing encoder and decoder input
enc_attn_bias = layers.attention.attention_bias(src_mask, "masking")
dec_attn_bias = layers.attention.attention_bias(tf.shape(targets)[1],
"causal")
#Shift left
if mode != "infer":
decoder_input = tf.pad(targets, [[0, 0], [1, 0], [0, 0]])[:, :-1, :]
decoder_input = layers.attention.add_timing_signal(decoder_input)
else:
decoder_input = tf.pad(targets, [[0, 0], [1, 0], [0, 0]])[:, -1:, :]
actual_len = tf.shape(state["decoder"]["layer_0"]["key"])[1]
not_bos_flag = tf.greater(actual_len, 0)
decoder_input = decoder_input * tf.cast(not_bos_flag,dtype=tf.float32)
decoder_input = add_onestep_timing_signal(decoder_input, actual_len+1)
if params.residual_dropout:
keep_prob = 1.0 - params.residual_dropout
decoder_input = tf.nn.dropout(decoder_input, keep_prob)
encoder_output = state["encoder"]
if mode != "infer":
# decoder_output = transformer_decoder(decoder_input, encoder_output,
# dec_attn_bias, enc_attn_bias,
# params)
decoder_outputs = transformer_decoder_output_cur_encdec_atten(decoder_input, encoder_output,
dec_attn_bias, enc_attn_bias,
params)
decoder_output, decoder_state, decoder_weight = decoder_outputs
else:
#decoder_input = tf.Print(decoder_input, [decoder_input], "decoder_input before -1", 1000, 1000)
decoder_input = decoder_input[:, -1:, :]
#decoder_input = tf.Print(decoder_input, [decoder_input], "decoder_input after -1", 1000, 1000)
dec_attn_bias = dec_attn_bias[:, :, -1:, :]
decoder_outputs = transformer_decoder_output_cur_encdec_atten(decoder_input, encoder_output,
dec_attn_bias, enc_attn_bias,
params, state=state["decoder"])
decoder_output, decoder_state, decoder_weight = decoder_outputs
decoder_output = decoder_output[:, -1, :]
logits = tf.matmul(decoder_output, weights, False, True)
log_prob = tf.nn.log_softmax(logits)
return log_prob, {"decoder": decoder_state, "att_weight": decoder_weight}
# [batch, length, channel] => [batch * length, vocab_size]
decoder_output = tf.reshape(decoder_output, [-1, hidden_size])
logits = tf.matmul(decoder_output, weights, False, True)
labels = features["target"]
# label smoothing
ce = layers.nn.smoothed_softmax_cross_entropy_with_logits(
logits=logits,
labels=labels,
smoothing=params.label_smoothing,
normalize=True
)
ce = tf.reshape(ce, tf.shape(tgt_seq))
if mode == "eval":
return -tf.reduce_sum(ce * tgt_mask, axis=1)
loss = tf.reduce_sum(ce * tgt_mask) / tf.reduce_sum(tgt_mask)
# layer_weight = decoder_weight["layer_5"][5] # layer5 head4
# loss = tf.Print(loss, [tf.shape(layer_weight)], 'layer_weight', 10, 1000)
# align_pos = features["align_pos"]
# loss = tf.Print(loss, [tf.shape(align_pos), align_pos ], 'len(align_pos), align_pos', 10, 10000)
# loss = tf.Print(loss, [tf.shape(labels), labels], 'tf.shape(labels), labels', 10, 10000)
return loss
def add_onestep_timing_signal(x, length, min_timescale=1.0, max_timescale=1.0e4, name=None):
"""
This function adds a bunch of sinusoids of different frequencies to a
Tensor. See paper: `Attention is all you need'
:param x: A tensor with shape [batch, length, channels]
:param length: the actual length of the current decoding target
:param min_timescale: A floating point number
:param max_timescale: A floating point number
:param name: An optional string
:returns: a Tensor the same shape as x.
"""
with tf.name_scope(name, default_name="add_timing_signal", values=[x]):
channels = tf.shape(x)[2]
position = tf.to_float(tf.range(length-1, length))
#position = tf.to_float(tf.range(length))
num_timescales = channels // 2
log_timescale_increment = (
math.log(float(max_timescale) / float(min_timescale)) /
(tf.to_float(num_timescales) - 1)
)
inv_timescales = min_timescale * tf.exp(
tf.to_float(tf.range(num_timescales)) * -log_timescale_increment
)
scaled_time = (tf.expand_dims(position, 1) *
tf.expand_dims(inv_timescales, 0))
signal = tf.concat([tf.sin(scaled_time), tf.cos(scaled_time)], axis=1)
signal = tf.pad(signal, [[0, 0], [0, tf.mod(channels, 2)]])
#signal = tf.reshape(signal, [1, length, channels])
signal = tf.reshape(signal, [1, 1, channels])
#x = tf.Print(x, [length, x, signal], "length, x, signal")
return x + signal[-1]
def model_graph(features, mode, params):
encoder_output = encoding_graph(features, mode, params)
state = {
"encoder": encoder_output[0]
}
output = decoding_graph(features, state, mode, params)
return output
class Transformer(interface.NMTModel):
def __init__(self, params, scope="transformer"):
super(Transformer, self).__init__(params=params, scope=scope)
def get_training_func(self, initializer):
def training_fn(features, params=None, reuse=None):
if params is None:
params = copy.copy(self.parameters)
else:
params = copy.copy(params)
with tf.variable_scope(self._scope, initializer=initializer,
reuse=reuse):
loss = model_graph(features, "train", params)
return loss
return training_fn
def get_evaluation_func(self):
def evaluation_fn(features, params=None):
if params is None:
params = copy.copy(self.parameters)
else:
params = copy.copy(params)
with tf.variable_scope(self._scope):
score = model_graph(features, "eval", params)
return score
return evaluation_fn
def get_rerank_inference_func(self):
def encoding_fn(features, params=None):
if params is None:
params = copy.copy(self.parameters)
else:
params = copy.copy(params)
with tf.variable_scope(self._scope):
encoder_output, encoder_weights = encoding_graph(features, "infer", params)
# batch = tf.shape(encoder_output)[0]
# state = { # 避免在state中添加weigtht,以免内存不够
# "encoder": encoder_output,
# "encoder_weight": encoder_weights,
# "decoder": {
# "layer_%d" % i: {
# "key": tf.zeros([batch, 0, params.hidden_size]),
# "value": tf.zeros([batch, 0, params.hidden_size]),
# "att_weight" : tf.zeros([batch, params.num_heads, tf.shape(encoder_output)[1]])
# }
# for i in range(params.num_decoder_layers)
# }
# }
return encoder_output, encoder_weights
def decoding_fn(features, state, params=None):
if params is None:
params = copy.copy(self.parameters)
else:
params = copy.copy(params)
with tf.variable_scope(self._scope):
log_prob, new_state = decoding_graph(features, state, "infer",
params)
return log_prob, new_state
return encoding_fn, decoding_fn
def get_inference_func(self):
def encoding_fn(features, params=None):
if params is None:
params = copy.copy(self.parameters)
else:
params = copy.copy(params)
with tf.variable_scope(self._scope):
encoder_output, weights = encoding_graph(features, "infer", params)
batch = tf.shape(encoder_output)[0]
state = {
"encoder": encoder_output,
"decoder": {
"layer_%d" % i: {
"key": tf.zeros([batch, 0, params.hidden_size]),
"value": tf.zeros([batch, 0, params.hidden_size]),
}
for i in range(params.num_decoder_layers)
}
}
return state
def decoding_fn(features, state, params=None):
if params is None:
params = copy.copy(self.parameters)
else:
params = copy.copy(params)
with tf.variable_scope(self._scope):
log_prob, new_state = decoding_graph(features, state, "infer",
params)
return log_prob, new_state
return encoding_fn, decoding_fn
@staticmethod
def get_name():
return "transformer"
@staticmethod
def get_parameters():
params = tf.contrib.training.HParams(
pad="<pad>",
bos="<eos>",
eos="<eos>",
unk="<unk>",
append_eos=False,
hidden_size=512,
filter_size=2048,
num_heads=8,
num_encoder_layers=6,
num_decoder_layers=6,
attention_dropout=0.0,
residual_dropout=0.1,
relu_dropout=0.0,
label_smoothing=0.1,
attention_key_channels=0,
attention_value_channels=0,
multiply_embedding_mode="sqrt_depth",
shared_embedding_and_softmax_weights=False,
shared_source_target_embedding=False,
# Override default parameters
learning_rate_decay="linear_warmup_rsqrt_decay",
initializer="uniform_unit_scaling",
initializer_gain=1.0,
learning_rate=1.0,
layer_preprocess="none",
layer_postprocess="layer_norm",
batch_size=4096,
constant_batch_size=False,
adam_beta1=0.9,
adam_beta2=0.98,
adam_epsilon=1e-9,
clip_grad_norm=0.0,
align_loss_model="square-mean",
align_layer=5,
align_head=1
)
return params