forked from jaywalnut310/Transformer-variations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformer_chrawr.py
371 lines (301 loc) · 12 KB
/
transformer_chrawr.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
# coding=utf-8
# Copyright 2017 The Tensor2Tensor Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""transformer (attention).
encoder: [Self-Attention, Feed-forward] x n
decoder: [Self-Attention, Source-Target-Attention, Feed-forward] x n
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
# Dependency imports
from tensor2tensor.layers import common_layers
from tensor2tensor.layers import common_attention
from tensor2tensor.utils import registry
from tensor2tensor.models import transformer
import tensorflow as tf
@registry.register_model
class TransformerChrawr(transformer.Transformer):
"""Transformer with Character-Aware Embedding."""
def encode(self, inputs, target_space, hparams):
"""Encode transformer inputs.
Args:
inputs: Transformer inputs [batch_size, input_length, hidden_dim]
target_space: scalar, target space ID.
hparams: hyperparmeters for model.
Returns:
Tuple of:
encoder_output: Encoder representation.
[batch_size, input_length, hidden_dim]
encoder_decoder_attention_bias: Bias and mask weights for
encodre-decoder attention. [batch_size, input_length]
"""
inputs = common_layers.flatten4d3d(inputs)
### Character-Aware Embedding ###
inputs = chrawr_embedding(inputs, hparams)
encoder_input, self_attention_bias, encoder_decoder_attention_bias = (
transformer.transformer_prepare_encoder(inputs, target_space, hparams))
encoder_input = tf.nn.dropout(
encoder_input, 1.0 - hparams.layer_prepostprocess_dropout)
encoder_output = transformer.transformer_encoder(
encoder_input,
self_attention_bias,
hparams)
return encoder_output, encoder_decoder_attention_bias
def chrawr_embedding(emb, hparams):
emb_mask = embedding_mask(emb)
tf.summary.image('emb_mask', tf.expand_dims(emb_mask[:32], 0))
# rescale dimension(depth)
emb = tf.layers.conv1d(emb, hparams.reduced_input_size, 1, 1, 'same', name="rescaled_embedding")
if hparams.chr_pos_enc:
emb = common_attention.add_timing_signal_1d(emb)
emb = emb * emb_mask
# chracter aware convolution
emb = conv_emb(emb, hparams, emb_mask)
emb_mask_in = embedding_mask(emb)
tf.summary.image('emb_mask_in', tf.expand_dims(emb_mask_in[:32], 0))
emb = tf.nn.dropout(emb, 1.0 - hparams.chr_dropout_rate)
emb = highway(emb, emb.get_shape()[-1], hparams)
emb = emb * emb_mask_in
encoder_input = tf.nn.dropout(emb, 1.0 - hparams.chr_dropout_rate)
# restore dimension(depth)
emb = tf.layers.conv1d(emb, hparams.hidden_size, 1, 1, 'same', name="restored_embedding")
emb = emb * emb_mask_in
return emb
def embedding_mask(emb):
emb_sum = tf.reduce_sum(tf.abs(emb), axis=-1)
return tf.expand_dims(tf.to_float(tf.not_equal(emb_sum, 0.0)), -1)
def highway(inputs, size, hparams, bias=-2.0, f=tf.nn.relu, scope='Highway'):
"""Highway Network (cf. http://arxiv.org/abs/1505.00387).
t = sigmoid(Wy + b)
z = t * g(Wy + b) + (1 - t) * y
where g is nonlinearity, t is transform gate, and (1 - t) is carry gate.
"""
with tf.variable_scope(scope):
for idx in range(hparams.num_highway_layers):
t = tf.layers.conv1d(inputs, size, 1, 1, 'same', bias_initializer=tf.constant_initializer(bias), activation=tf.nn.sigmoid, name='highway_lin_%d' % idx)
g = tf.layers.conv1d(inputs, size, 1, 1, 'same', activation=hparams.chr_nonlinearity, name='highway_gate_%d' % idx)
output = t * g + (1. - t) * inputs
inputs = output
return output
def conv_emb(inputs, hparams, input_mask, scope='ConvEmb'):
'''
:inputs: input float tensor of shape [(batch_size) x time_step x embed_size]
:kernels: array of kernel sizes
:kernel_features: array of kernel feature sizes (parallel to kernels)
'''
assert len(hparams.chr_kernels) == len(hparams.chr_kernel_features), 'Kernel and Features must have the same size'
layers = []
with tf.variable_scope(scope):
for kernel_size, kernel_feature_size in zip(hparams.chr_kernels, hparams.chr_kernel_features):
# [batch_size x time_step x kernel_feature_size]
conv = tf.layers.conv1d(inputs, kernel_feature_size, kernel_size, 1, 'same', activation=hparams.chr_nonlinearity, name="kernel_%d" % kernel_size)
conv = conv * input_mask # remove calculate values for zero padding
# [batch_size x modified_time_step x kernel_feature_size]
pool = tf.layers.max_pooling1d(conv, hparams.chr_maxpool_size, hparams.chr_maxpool_size, 'same', name="kernel_%d_pool" % kernel_size)
layers.append(pool)
if len(hparams.chr_kernels) > 1:
output = tf.concat(layers, 2)
else:
output = layers[0]
return output # [batch_size x modified_time_step x hidden_dim]
@registry.register_hparams
def transformer_chrawr_base():
"""Base hparams for Transformer with Character Aware Embedding."""
hparams = transformer.transformer_base()
hparams.num_highway_layers = 4
hparams.reduced_input_size = 128
hparams.hidden_size = 512
hparams.chr_kernels = [1,2,3,4,5,6,7,8]
hparams.chr_kernel_features = [200,200,250,250,300,300,300,300]
hparams.chr_maxpool_size = 5
hparams.chr_nonlinearity = tf.nn.tanh
hparams.chr_dropout_rate = 0.
hparams.chr_pos_enc = False
hparams.target_modality="symbol:tgtemb"
return hparams
@registry.register_hparams
def transformer_chrawr_big():
"""HParams for transfomer_chrawr big model on WMT."""
hparams = transformer_chrawr_base()
hparams.hidden_size = 1024
hparams.filter_size = 4096
hparams.num_heads = 16
hparams.layer_prepostprocess_dropout = 0.3
return hparams
@registry.register_hparams
def transformer_chrawr_big_single_gpu():
"""HParams for transformer_chrawr big model for single gpu."""
hparams = transformer_chrawr_big()
hparams.layer_prepostprocess_dropout = 0.1
hparams.learning_rate_warmup_steps = 16000
hparams.optimizer_adam_beta2 = 0.998
return hparams
@registry.register_hparams
def transformer_chrawr_base_single_gpu():
"""HParams for transformer_chrawr base model for single gpu."""
hparams = transformer_chrawr_base()
hparams.batch_size = 2048
hparams.learning_rate_warmup_steps = 16000
return hparams
# For Fast Test
@registry.register_hparams
def transformer_chrawr_l2():
hparams = transformer_chrawr_base()
hparams.num_hidden_layers = 2
return hparams
@registry.register_hparams
def transformer_chrawr_test0():
hparams = transformer_chrawr_l2()
return hparams
@registry.register_hparams
def transformer_chrawr_test1(): # small kernel, small pooling
hparams = transformer_chrawr_l2()
hparams.chr_kernels = [1,2,3,4,5]
hparams.chr_kernel_features = [250,250,300,300,300]
hparams.chr_maxpool_size = 3
return hparams
@registry.register_hparams
def transformer_chrawr_test2(): # small kernel
hparams = transformer_chrawr_l2()
hparams.chr_kernels = [1,2,3,4,5]
hparams.chr_kernel_features = [250,250,300,300,300]
return hparams
@registry.register_hparams
def transformer_chrawr_test3(): # small pooling
hparams = transformer_chrawr_l2()
hparams.chr_maxpool_size = 3
return hparams
@registry.register_hparams
def transformer_chrawr_test4(): # small highway
hparams = transformer_chrawr_l2()
hparams.num_highway_layers = 1
return hparams
@registry.register_hparams
def transformer_chrawr_test5(): # non target emb sharing
hparams = transformer_chrawr_l2()
hparams.target_modality="default"
return hparams
@registry.register_hparams
def transformer_chrawr_test6(): # relu
hparams = transformer_chrawr_l2()
hparams.chr_nonlinearity = tf.nn.relu
return hparams
@registry.register_hparams
def transformer_chrawr_test7(): # elu
hparams = transformer_chrawr_l2()
hparams.chr_nonlinearity = tf.nn.elu
return hparams
@registry.register_hparams
def transformer_chrawr_test8(): # dropout
hparams = transformer_chrawr_l2()
hparams.chr_dropout_rate = .2
return hparams
@registry.register_hparams
def transformer_chrawr_test9(): # positional_encoding
hparams = transformer_chrawr_l2()
hparams.chr_pos_enc = True
return hparams
@registry.register_hparams
def transformer_chrawr_test10(): # more small pooling
hparams = transformer_chrawr_l2()
hparams.chr_maxpool_size = 2
return hparams
@registry.register_hparams
def transformer_chrawr_test11(): # more small pooling & more small kernels
hparams = transformer_chrawr_l2()
hparams.chr_maxpool_size = 2
hparams.chr_kernels = [1,2,3,4,5]
hparams.chr_kernel_features = [250,250,300,300,300]
return hparams
@registry.register_hparams
def transformer_chrawr_test12(): # more small pooling & positional_encoding
hparams = transformer_chrawr_l2()
hparams.chr_maxpool_size = 2
hparams.chr_pos_enc = True
return hparams
@registry.register_hparams
def transformer_chrawr_test13(): # more small pooling & more small kernels & positional_encoding
hparams = transformer_chrawr_l2()
hparams.chr_maxpool_size = 2
hparams.chr_kernels = [1,2,3,4,5]
hparams.chr_kernel_features = [250,250,300,300,300]
hparams.chr_pos_enc = True
return hparams
@registry.register_hparams
def transformer_chrawr_test14():
# more small pooling & more small kernels & positional_encoding & small highway & relu &non target emb sharing
hparams = transformer_chrawr_l2()
hparams.chr_maxpool_size = 2
hparams.chr_kernels = [1,2,3,4,5]
hparams.chr_kernel_features = [250,250,300,300,300]
hparams.chr_pos_enc = True
hparams.num_highway_layers = 1
hparams.chr_nonlinearity = tf.nn.relu
hparams.target_modality="default"
return hparams
### MOS ###
@registry.register_hparams
def transformer_mos():
hparams = transformer.transformer_base()
hparams.n_experts = 15
hparams.target_modality="symbol:mos"
return hparams
@registry.register_hparams
def transformer_mos_single_gpu():
hparams = transformer.transformer_base_single_gpu()
hparams.n_experts = 15
hparams.target_modality="symbol:mos"
return hparams
@registry.register_hparams
def transformer_chrawr_mos():
hparams = transformer_chrawr_base()
hparams.n_experts = 15
hparams.target_modality="symbol:mos"
return hparams
@registry.register_hparams
def transformer_chrawr_mos_single_gpu():
hparams = transformer_chrawr_base_single_gpu()
hparams.n_experts = 15
hparams.target_modality="symbol:mos"
return hparams
### USELESS ###
@registry.register_hparams
def transformer_chrawr_long_single_gpu():
"""HParams for transformer_chrawr model for single gpu."""
hparams = transformer_chrawr_base_single_gpu()
hparams.chr_maxpool_size = 1
return hparams
@registry.register_hparams
def transformer_chrawr_many_single_gpu():
"""HParams for transformer_chrawr model for single gpu."""
hparams = transformer_chrawr_base_single_gpu()
hparams.batch_size = 2 * hparams.batch_size
hparams.chr_kernel_features = [224,224,224,224,224,224,224,224]
hparams.chr_maxpool_size = 3
return hparams
@registry.register_hparams
def transformer_chrawr_general_single_gpu():
"""HParams for transformer_chrawr model for single gpu."""
hparams = transformer_chrawr_base_single_gpu()
hparams.chr_kernel_features = [224,224,224,224,224,224,224,224]
hparams.chr_maxpool_size = 3
return hparams
@registry.register_hparams
def transformer_chrawr_general_long_single_gpu():
"""HParams for transformer_chrawr model for single gpu."""
hparams = transformer_chrawr_base_single_gpu()
hparams.chr_kernel_features = [224,224,224,224,224,224,224,224]
hparams.chr_maxpool_size = 1
return hparams