-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeepatt.py
287 lines (226 loc) · 10.7 KB
/
deepatt.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
"""
Author: Anonymity
Email: anonymity@email.com
Semantic Role Labeling with Multi-mask Tensorized Self-Attention (MTSA)
place this file to folder "Tagger/models" to replace original one.
!!!Please initialize trainable variables using "tf.glorot_normal_initializer()" and follow the instructions
provided in README.md
"""
import ops
import copy
import tensorflow as tf
from ops.layers import layer_norm, linear
from ops.attention import multihead_attention
from ops.my_attn import split_head, combine_head, new_exp_mask, new_mask
import math
# read
def deepatt_default_params():
params = tf.contrib.training.HParams(
feature_size=100,
hidden_size=200,
filter_size=800,
num_hidden_layers=10,
residual_dropout=0.2,
attention_dropout=0.1,
relu_dropout=0.1,
label_smoothing=0.1,
fix_embedding=False,
multiply_embedding_mode="sqrt_depth",
num_heads=8, # for multi_head
)
return params
def _residual_fn(x, y, params):
if params.residual_dropout > 0.0:
y = tf.nn.dropout(y, 1.0 - params.residual_dropout)
return layer_norm(x + y)
def _ffn_layer(inputs, hidden_size, output_size, keep_prob=None,
data_format="NHWC", dtype=None, scope=None):
with tf.variable_scope(scope, default_name="ffn_layer", values=[inputs],
dtype=dtype):
with tf.variable_scope("input_layer"):
hidden = linear(inputs, hidden_size, True, data_format=data_format)
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 = linear(hidden, output_size, True, data_format=data_format)
return output
def encoder(encoder_input, mask, params, dtype=None, scope=None):
with tf.variable_scope(scope, default_name="encoder", dtype=dtype,
values=[encoder_input, mask]):
x = encoder_input
seq_len = tf.to_int32(tf.reduce_sum(mask, -1))
attn_bias = ops.attention.attention_bias(mask, "masking")
# multiple masks generation for multi-head
rep_mask = tf.cast(mask, tf.bool)
bs, sl = tf.shape(rep_mask)[0], tf.shape(rep_mask)[1]
rep_mask_epd1 = tf.expand_dims(rep_mask, 1) # bs,1,sl
rep_mask_epd2 = tf.expand_dims(rep_mask, 2) # bs,sl,1
rep_mask_mat = tf.logical_and(rep_mask_epd1, rep_mask_epd2) # bs,sl,sl
sl_indices = tf.range(sl, dtype=tf.int32)
sl_col, sl_row = tf.meshgrid(sl_indices, sl_indices)
fw_mask = tf.greater(sl_row, sl_col) # sl,sl
bw_mask = tf.less(sl_row, sl_col) # sl,sl
direct_mask = tf.stack([fw_mask, bw_mask], 0) # 2,sl,sl
direct_mask = tf.reshape( # num,sl,sl
tf.tile(tf.expand_dims(direct_mask, 1), [1, int(params.num_heads/2), 1, 1]), # 2,4,sl,sl
[params.num_heads, sl, sl]
)
final_mask = tf.logical_and( # num,bs,sl,sl
tf.expand_dims(rep_mask_mat, 0),
tf.expand_dims(direct_mask, 1),
)
final_mask_ft = tf.cast(final_mask, tf.float32)
for layer in xrange(params.num_hidden_layers):
with tf.variable_scope("layer_%d" % layer):
with tf.variable_scope("computation"):
y = _ffn_layer(
x,
params.filter_size,
params.hidden_size,
1.0 - params.relu_dropout,
)
x = _residual_fn(x, y, params)
with tf.variable_scope("self_attention"):
y = multi_mask_tensorized_self_attn(
x, rep_mask, final_mask_ft, params.hidden_size, params.num_heads,
1.0 - params.attention_dropout
)
x = _residual_fn(x, y, params)
return x
def deepatt_model(features, mode, params):
hparams = params
params = copy.copy(hparams)
# disable dropout in evaluation/inference mode
if mode != tf.contrib.learn.ModeKeys.TRAIN:
params.attention_dropout = 0.0
params.residual_dropout = 0.0
params.relu_dropout = 0.0
vocab_size = len(params.vocabulary["inputs"])
label_size = len(params.vocabulary["targets"])
hidden_size = params.hidden_size
feature_size = params.feature_size
tok_seq = features["inputs"]
pred_seq = features["preds"]
mask = tf.to_float(tf.not_equal(tok_seq, 0))
# shared embedding and softmax weights
initializer = None
if mode == tf.contrib.learn.ModeKeys.TRAIN:
if not params.use_global_initializer:
initializer = tf.random_normal_initializer(0.0,
feature_size ** -0.5)
weights = tf.get_variable("weights", [2, feature_size],
initializer=initializer)
if mode == tf.contrib.learn.ModeKeys.TRAIN:
if params.embedding is not None:
initializer = lambda shape, dtype, partition_info: params.embedding
else:
initializer = None
embedding = tf.get_variable("embedding", [vocab_size, feature_size],
initializer=initializer,
trainable=not params.fix_embedding)
bias = tf.get_variable("bias", [hidden_size])
# id => embedding
# src_seq: [batch, max_src_length]
# tgt_seq: [batch, max_tgt_length]
inputs = tf.gather(embedding, tok_seq)
if mode == tf.contrib.learn.ModeKeys.INFER:
if features.get("mask") is not None:
keep_mask = features["mask"][:, :, None]
unk_emb = features["embedding"]
inputs = inputs * keep_mask + (1.0 - keep_mask) * unk_emb
preds = tf.gather(weights, pred_seq)
inputs = tf.concat([inputs, preds], -1)
if params.multiply_embedding_mode == "sqrt_depth":
inputs = inputs * (hidden_size ** 0.5)
inputs = inputs * tf.expand_dims(mask, -1)
# preparing encoder & decoder input
encoder_input = tf.nn.bias_add(inputs, bias)
# !!! delete positional embedding in multi-head self-attention
"""
if params.pos == "timing":
encoder_input = ops.attention.add_timing_signal(encoder_input)
elif params.pos == "embedding":
initializer = tf.random_normal_initializer(0.0, hidden_size ** -0.5)
embedding = tf.get_variable("position_embedding", [1000, hidden_size],
initializer=initializer)
indices = tf.range(tf.shape(features["inputs"])[1])[None, :]
pos_emb = tf.gather(embedding, indices)
pos_emb = tf.tile(pos_emb, [tf.shape(features["inputs"])[0], 1, 1])
encoder_input = encoder_input + pos_emb
if params.residual_dropout:
keep_prob = 1.0 - params.residual_dropout
encoder_input = tf.nn.dropout(encoder_input, keep_prob)
"""
encoder_output = encoder(encoder_input, mask, params)
initializer = None
if mode == tf.contrib.learn.ModeKeys.TRAIN:
if not params.use_global_initializer:
initializer = tf.random_normal_initializer(0.0,
hidden_size ** -0.5)
with tf.variable_scope("prediction", initializer=initializer):
logits = linear(encoder_output, label_size, True, scope="logits")
if mode == tf.contrib.learn.ModeKeys.INFER:
outputs = tf.to_int32(tf.argmax(logits, axis=-1))
return outputs, tf.nn.softmax(logits)
labels = features["targets"]
targets = features["targets"]
logits = tf.reshape(logits, [-1, label_size])
labels = tf.reshape(labels, [-1])
# label smoothing
ce = ops.layers.smoothed_softmax_cross_entropy_with_logits(
logits=logits,
labels=labels,
label_smoothing=params.label_smoothing,
normalize=True
)
ce = tf.reshape(ce, tf.shape(targets))
cost = tf.reduce_sum(ce * mask) / tf.reduce_sum(mask)
# greedy decoding
if mode == tf.contrib.learn.ModeKeys.EVAL:
outputs = tf.to_int32(tf.argmax(logits, axis=-1))
return cost, tf.reshape(outputs, tf.shape(targets))
return cost
def multi_mask_tensorized_self_attn(
rep_tensor, rep_mask, final_mask_ft, hn, head_num, keep_prob=None, scope=None):
data_format = "NHWC"
assert hn % head_num == 0, "hn (%d) must be divisible by the number of " \
"attention heads (%d)." % (hn, head_num)
head_dim = int(hn / head_num)
bs, sl = tf.shape(rep_tensor)[0], tf.shape(rep_tensor)[1]
with tf.variable_scope(scope or 'proposed_self_attention'):
combined = linear(rep_tensor, 3 * hn, True, True, data_format=data_format,
scope="qkv_transform")
q, k, v = tf.split(combined, 3, 2) # bs,sl,hn
q = split_head(q, head_num)
k = split_head(k, head_num)
v = split_head(v, head_num) # num,bs,sl,dim
with tf.name_scope("dot_product_attention"):
dot_logits = tf.matmul(q, k, transpose_b=True) * (head_dim ** -0.5) # num,bs,sl,sl
e_dot_logits = tf.exp(new_exp_mask(dot_logits, final_mask_ft)) # num,bs,sl,sl
with tf.variable_scope("s2t_multi_dim_attention"):
multi_logits_before = linear(
rep_tensor, hn, True, True, data_format=data_format, scope="multi_logits_before")
multi_logits = split_head(multi_logits_before, head_num) # num,bs,sl,dim
e_multi_logits = tf.exp(new_exp_mask( # mul,bs,sl,dim
multi_logits, rep_mask, multi_head=True, high_dim=True))
with tf.name_scope("hybrid_attn"):
accum_z_deno = tf.matmul(e_dot_logits, e_multi_logits) # num,bs,sl,dim
accum_z_deno = tf.where( # in case of nan
tf.greater(accum_z_deno, tf.zeros_like(accum_z_deno)),
accum_z_deno,
tf.ones_like(accum_z_deno)
)
if keep_prob is not None and keep_prob < 1.0:
real_keep_prob = keep_prob
e_multi_logits = tf.nn.dropout(e_multi_logits, real_keep_prob)
e_dot_logits = tf.nn.dropout(e_dot_logits, real_keep_prob)
rep_mul_score = new_mask(v, rep_mask, multi_head=True, high_dim=True) * e_multi_logits
accum_rep_mul_score = tf.matmul(e_dot_logits, rep_mul_score)
attn_res = accum_rep_mul_score / accum_z_deno
with tf.variable_scope("output"):
attn_output = combine_head(attn_res) # bs,sl,hn
final_out = linear(attn_output, hn, True, data_format=data_format,
scope="output_transform")
final_out = new_mask(final_out, rep_mask, high_dim=True) # bs,sl,hn
return final_out