-
Notifications
You must be signed in to change notification settings - Fork 3
/
attention.py
233 lines (186 loc) · 10.1 KB
/
attention.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
import tensorflow as tf
from config import MultiLayerPerceptronAttentionConfig
from utils import get_k_layer_logits
class MultiLayerPerceptronAttention(object):
"""
Authors: Yatian Shen, Xuanjing Huang
Paper: https://www.aclweb.org/anthology/C16-1238
"""
H_W_we = "H_W_we"
H_W_a = "H_W_a"
H_b_we = "H_b_we"
H_b_a = "H_b_a"
I_x = "I_x"
I_pos = "I_pos"
I_dist_obj = "I_dist_obj"
I_dist_subj = "I_dist_subj"
I_entities = "I_e"
def __init__(self, cfg, batch_size, terms_per_context,
term_embedding_size,
pos_embedding_size,
dist_embedding_size):
assert(isinstance(cfg, MultiLayerPerceptronAttentionConfig))
self.__cfg = cfg
self.__batch_size = batch_size
self.__terms_per_context = terms_per_context
self.__term_embedding_size = term_embedding_size + \
pos_embedding_size + \
2 * dist_embedding_size
self.__input = {}
self.__hidden = {}
@property
def Input(self):
return self.__input
@property
def AttentionEmbeddingSize(self):
return self.__cfg.EntitiesPerContext * self.__term_embedding_size
def set_input(self, x, pos, dist_obj, dist_subj, keys):
self.__input[self.I_x] = x
self.__input[self.I_pos] = pos
self.__input[self.I_dist_subj] = dist_subj
self.__input[self.I_dist_obj] = dist_obj
self.__input[self.I_entities] = keys
def init_input(self):
self.__input[self.I_x] = tf.compat.v1.placeholder(
dtype=tf.int32,
shape=[self.__batch_size, self.__terms_per_context])
self.__input[self.I_pos] = tf.compat.v1.placeholder(
dtype=tf.int32,
shape=[self.__batch_size, self.__terms_per_context])
self.__input[self.I_dist_obj] = tf.compat.v1.placeholder(
dtype=tf.int32,
shape=[self.__batch_size, self.__terms_per_context])
self.__input[self.I_dist_subj] = tf.compat.v1.placeholder(
dtype=tf.int32,
shape=[self.__batch_size, self.__terms_per_context])
self.__input[self.I_entities] = tf.compat.v1.placeholder(
dtype=tf.int32,
shape=[self.__batch_size, self.__cfg.EntitiesPerContext])
def init_hidden(self):
self.__hidden[self.H_W_we] = tf.Variable(tf.random.normal([2 * self.__term_embedding_size, self.__cfg.HiddenSize]),
dtype=tf.float32)
self.__hidden[self.H_b_we] = tf.Variable(tf.random.normal([self.__cfg.HiddenSize]),
dtype=tf.float32)
self.__hidden[self.H_W_a] = tf.Variable(tf.random.normal([self.__cfg.HiddenSize, 1]),
dtype=tf.float32)
self.__hidden[self.H_b_a] = tf.Variable(tf.random.normal([1]),
dtype=tf.float32)
def init_body(self, term_embedding, pos_embedding, dist_embedding):
assert(isinstance(term_embedding, tf.Tensor))
assert(isinstance(pos_embedding, tf.Tensor))
assert(isinstance(dist_embedding, tf.Tensor))
embedded_terms = tf.concat(
[tf.nn.embedding_lookup(params=term_embedding, ids=self.__input[self.I_x]),
tf.nn.embedding_lookup(params=pos_embedding, ids=self.__input[self.I_pos]),
tf.nn.embedding_lookup(params=dist_embedding, ids=self.__input[self.I_dist_subj]),
tf.nn.embedding_lookup(params=dist_embedding, ids=self.__input[self.I_dist_obj])],
axis=-1)
with tf.name_scope("attention"):
def filter_batch_elements(elements, inds, handler):
"""
elements: [batch_size, terms_per_context]
"""
batch_size = elements.shape[0]
filtered = tf.TensorArray(
dtype=tf.int32,
name="context_iter",
size=batch_size,
infer_shape=False,
dynamic_size=True)
_, _, _, filtered = tf.while_loop(
lambda i, *_: tf.less(i, batch_size),
handler,
(0, elements, inds, filtered))
return filtered.stack()
def select_entity_related_elements(i, elements, inds, filtered):
"""
elements: [batch, terms_per_context]
inds: [batch, terms_per_context]
"""
row_elements = tf.squeeze(tf.gather(elements, [i], axis=0))
row_inds = tf.squeeze(tf.gather(inds, [i], axis=0))
result = tf.gather(row_elements, row_inds) # row: [entities_per_context]
return (i + 1,
elements,
inds,
filtered.write(i, tf.squeeze(result)))
def iter_by_entities(entities, e_pos, e_dist_obj, e_dist_subj, handler):
"""
entities: [batch_size, entities]
e_pos: [batch_size, terms_per_context]
e_dists: [batch_size, terms_per_context]
"""
att_sum_array = tf.TensorArray(
dtype=tf.float32,
name="context_iter",
size=self.__cfg.EntitiesPerContext,
infer_shape=False,
dynamic_size=True)
att_weights_array = tf.TensorArray(
dtype=tf.float32,
name="context_iter",
size=self.__cfg.EntitiesPerContext,
infer_shape=False,
dynamic_size=True)
_, _, _, _, _, att_sum, att_weights = tf.while_loop(
lambda i, *_: tf.less(i, self.__cfg.EntitiesPerContext),
handler,
(0, entities, e_pos, e_dist_obj, e_dist_subj, att_sum_array, att_weights_array))
return att_sum.stack(), att_weights.stack()
def process_entity(i, entities, e_pos, e_dist_obj, e_dist_subj, att_sum, att_weights):
"""
entities: [batch_size, entities_per_context]
"""
e_term_index = tf.gather(entities, [i], axis=1) # [batch_size, 1] -- term positions
e_term_indices = tf.tile(e_term_index, [1, self.__terms_per_context]) # [batch_size, terms_per_context]
e_pos_indices = filter_batch_elements(
elements=e_pos,
inds=e_term_indices,
handler=select_entity_related_elements)
e_dist_obj_indices = filter_batch_elements(
elements=e_dist_obj,
inds=e_term_indices,
handler=select_entity_related_elements)
e_dist_subj_indices = filter_batch_elements(
elements=e_dist_subj,
inds=e_term_indices,
handler=select_entity_related_elements)
e = tf.concat(
[tf.nn.embedding_lookup(term_embedding, e_term_indices), # [batch_size, terms_per_context, embedding_size]
tf.nn.embedding_lookup(pos_embedding, e_pos_indices),
tf.nn.embedding_lookup(dist_embedding, e_dist_obj_indices),
tf.nn.embedding_lookup(dist_embedding, e_dist_subj_indices)],
axis=-1) # [batch_size, terms_per_context, embedding_size]
merged = tf.concat([embedded_terms, e], axis=-1)
merged = tf.reshape(merged, [self.__batch_size * self.__terms_per_context,
2 * self.__term_embedding_size])
u = get_k_layer_logits(g=merged,
W=[self.__hidden[self.H_W_we], self.__hidden[self.H_W_a]],
b=[self.__hidden[self.H_b_we], self.__hidden[self.H_b_a]],
activations=[None,
lambda tensor: tf.tanh(tensor),
None]) # [batch_size * terms_per_context, 1]
alphas = tf.reshape(u, [self.__batch_size, self.__terms_per_context])
alphas = tf.nn.softmax(alphas)
alphas = tf.reshape(alphas, [self.__batch_size * self.__terms_per_context, 1])
original_embedding = tf.reshape(embedded_terms,
[self.__batch_size * self.__terms_per_context, self.__term_embedding_size])
w_embedding = tf.multiply(alphas, original_embedding)
w_embedding = tf.reshape(w_embedding, [self.__batch_size, self.__terms_per_context, self.__term_embedding_size])
w_sum = tf.reduce_sum(w_embedding, axis=1) # [batch_size, embedding_size]
return (i + 1,
entities, e_pos, e_dist_obj, e_dist_subj,
att_sum.write(i, w_sum),
att_weights.write(i, tf.reshape(alphas, [self.__batch_size, self.__terms_per_context])))
att_sum, att_weights = iter_by_entities(
entities=self.__input[self.I_entities],
e_pos=self.__input[self.I_pos],
e_dist_obj=self.__input[self.I_dist_obj],
e_dist_subj=self.__input[self.I_dist_subj],
handler=process_entity)
# att_sum: [entity_per_context, batch_size, term_embedding_size]
# att_weights: [entity_per_context, batch_size, terms_per_context]
att_sum = tf.transpose(att_sum, perm=[1, 0, 2]) # [batch_size, entity_per_context, term_embedding_size]
att_sum = tf.reshape(att_sum, shape=[self.__batch_size, self.AttentionEmbeddingSize])
att_weights = tf.transpose(att_weights, perm=[1, 0, 2]) # [batch_size, entity_per_context, terms_per_context]
return att_sum, att_weights