/
token_predictor.py
387 lines (313 loc) · 14.6 KB
/
token_predictor.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
"""Predicts a token."""
from collections import namedtuple
import dynet as dy
import dynet_utils as du
from attention import Attention
class PredictionInput(namedtuple('PredictionInput',
('decoder_state',
'input_hidden_states',
'snippets',
'input_sequence'))):
""" Inputs to the token predictor. """
__slots__ = ()
class TokenPrediction(namedtuple('TokenPrediction',
('scores',
'aligned_tokens',
'attention_results',
'decoder_state'))):
"""A token prediction.
Attributes:
scores (dy.Expression): Scores for each possible output token.
aligned_tokens (list of str): The output tokens, aligned with the scores.
attention_results (AttentionResult): The result of attending on the input
sequence.
"""
__slots__ = ()
def score_snippets(snippets, scorer):
""" Scores snippets given a scorer.
Inputs:
snippets (list of Snippet): The snippets to score.
scorer (dy.Expression): Dynet vector against which to score the snippets.
Returns:
dy.Expression, list of str, where the first is the scores and the second
is the names of the snippets that were scored.
"""
snippet_expressions = [snippet.embedding for snippet in snippets]
all_snippet_embeddings = dy.concatenate(snippet_expressions, d=1)
if du.is_vector(scorer):
scorer = du.add_dim(scorer)
scores = dy.transpose(dy.transpose(scorer) * all_snippet_embeddings)
if scores.dim()[0][0] != len(snippets):
raise ValueError("Got " + str(scores.dim()[0][0]) + " scores for "
+ str(len(snippets)) + " snippets")
return scores, [snippet.name for snippet in snippets]
class TokenPredictor():
""" Predicts a token given a (decoder) state.
Attributes:
vocabulary (Vocabulary): A vocabulary object for the output.
attention_module (Attention): An attention module.
state_transformation_weights (dy.Parameters): Transforms the input state
before predicting a token.
vocabulary_weights (dy.Parameters): Final layer weights.
vocabulary_biases (dy.Parameters): Final layer biases.
"""
def __init__(self, model, params, vocabulary, attention_key_size):
self.vocabulary = vocabulary
self.attention_module = Attention(model,
params.decoder_state_size,
attention_key_size,
attention_key_size)
self.state_transform_weights = du.add_params(
model,
(params.decoder_state_size +
attention_key_size,
params.decoder_state_size),
"weights-state-transform")
self.vocabulary_weights = du.add_params(
model, (params.decoder_state_size, len(vocabulary)), "weights-vocabulary")
self.vocabulary_biases = du.add_params(model,
tuple([len(vocabulary)]),
"biases-vocabulary")
def _get_intermediate_state(self, state, dropout_amount=0.):
intermediate_state = dy.tanh(
du.linear_layer(
state, self.state_transform_weights))
return dy.dropout(intermediate_state, dropout_amount)
def _score_vocabulary_tokens(self, state):
scores = dy.transpose(du.linear_layer(state,
self.vocabulary_weights,
self.vocabulary_biases))
if scores.dim()[0][0] != len(self.vocabulary.inorder_tokens):
raise ValueError("Got " +
str(scores.dim()[0][0]) +
" scores for " +
str(len(self.vocabulary.inorder_tokens)) +
" vocabulary items")
return scores, self.vocabulary.inorder_tokens
def __call__(self,
prediction_input,
dropout_amount=0.):
decoder_state = prediction_input.decoder_state
input_hidden_states = prediction_input.input_hidden_states
attention_results = self.attention_module(decoder_state,
input_hidden_states)
state_and_attn = dy.concatenate(
[decoder_state, attention_results.vector])
intermediate_state = self._get_intermediate_state(
state_and_attn, dropout_amount=dropout_amount)
vocab_scores, vocab_tokens = self._score_vocabulary_tokens(
intermediate_state)
return TokenPrediction(vocab_scores, vocab_tokens, attention_results, decoder_state)
class SnippetTokenPredictor(TokenPredictor):
""" Token predictor that also predicts snippets.
Attributes:
snippet_weights (dy.Parameter): Weights for scoring snippets against some
state.
"""
def __init__(
self,
model,
params,
vocabulary,
attention_key_size,
snippet_size):
TokenPredictor.__init__(self,
model,
params,
vocabulary,
attention_key_size)
if snippet_size <= 0:
raise ValueError("Snippet size must be greater than zero; was " \
+ str(snippet_size))
self.snippet_weights = du.add_params(model,
(params.decoder_state_size,
snippet_size),
"weights-snippet")
def _get_snippet_scorer(self, state):
return dy.transpose(du.linear_layer(dy.transpose(state),
self.snippet_weights))
def __call__(self,
prediction_input,
dropout_amount=0.):
decoder_state = prediction_input.decoder_state
input_hidden_states = prediction_input.input_hidden_states
snippets = prediction_input.snippets
attention_results = self.attention_module(decoder_state,
input_hidden_states)
state_and_attn = dy.concatenate(
[decoder_state, attention_results.vector])
intermediate_state = self._get_intermediate_state(
state_and_attn, dropout_amount=dropout_amount)
vocab_scores, vocab_tokens = self._score_vocabulary_tokens(
intermediate_state)
final_scores = vocab_scores
aligned_tokens = []
aligned_tokens.extend(vocab_tokens)
if snippets:
snippet_scores, snippet_tokens = score_snippets(
snippets,
self._get_snippet_scorer(intermediate_state))
final_scores = dy.concatenate([final_scores, snippet_scores])
aligned_tokens.extend(snippet_tokens)
return TokenPrediction(final_scores,
aligned_tokens,
attention_results,
decoder_state)
class AnonymizationTokenPredictor(TokenPredictor):
""" Token predictor that also predicts anonymization tokens.
Attributes:
anonymizer (Anonymizer): The anonymization object.
"""
def __init__(self,
model,
params,
vocabulary,
attention_key_size,
anonymizer):
TokenPredictor.__init__(self,
model,
params,
vocabulary,
attention_key_size)
if not anonymizer:
raise ValueError("Expected an anonymizer, but was None")
self.anonymizer = anonymizer
def _score_anonymized_tokens(self,
input_sequence,
attention_scores):
scores = []
tokens = []
for i, token in enumerate(input_sequence):
if self.anonymizer.is_anon_tok(token):
scores.append(attention_scores[i])
tokens.append(token)
if len(scores) > 0:
if len(scores) != len(tokens):
raise ValueError("Got " + str(len(scores)) + " scores for "
+ str(len(tokens)) + " anonymized tokens")
return dy.concatenate(scores), tokens
else:
return None, []
def __call__(self,
prediction_input,
dropout_amount=0.):
decoder_state = prediction_input.decoder_state
input_hidden_states = prediction_input.input_hidden_states
input_sequence = prediction_input.input_sequence
assert input_sequence
attention_results = self.attention_module(decoder_state,
input_hidden_states)
state_and_attn = dy.concatenate(
[decoder_state, attention_results.vector])
intermediate_state = self._get_intermediate_state(
state_and_attn, dropout_amount=dropout_amount)
vocab_scores, vocab_tokens = self._score_vocabulary_tokens(
intermediate_state)
final_scores = vocab_scores
aligned_tokens = []
aligned_tokens.extend(vocab_tokens)
anonymized_scores, anonymized_tokens = self._score_anonymized_tokens(
input_sequence,
attention_results.scores)
if anonymized_scores:
final_scores = dy.concatenate([final_scores, anonymized_scores])
aligned_tokens.extend(anonymized_tokens)
return TokenPrediction(final_scores,
aligned_tokens,
attention_results,
decoder_state)
class SnippetAnonymizationTokenPredictor(
SnippetTokenPredictor,
AnonymizationTokenPredictor):
""" Token predictor that both anonymizes and scores snippets."""
def __init__(self,
model,
params,
vocabulary,
attention_key_size,
snippet_size,
anonymizer):
SnippetTokenPredictor.__init__(self,
model,
params,
vocabulary,
attention_key_size,
snippet_size)
AnonymizationTokenPredictor.__init__(self,
model,
params,
vocabulary,
attention_key_size,
anonymizer)
def __call__(self,
prediction_input,
dropout_amount=0.):
decoder_state = prediction_input.decoder_state
assert prediction_input.input_sequence
snippets = prediction_input.snippets
attention_results = self.attention_module(decoder_state,
prediction_input.input_hidden_states)
intermediate_state = self._get_intermediate_state(
dy.concatenate([decoder_state, attention_results.vector]),
dropout_amount=dropout_amount)
# Vocabulary tokens
final_scores, vocab_tokens = self._score_vocabulary_tokens(
intermediate_state)
aligned_tokens = []
aligned_tokens.extend(vocab_tokens)
# Snippets
if snippets:
snippet_scores, snippet_tokens = score_snippets(
snippets,
self._get_snippet_scorer(intermediate_state))
final_scores = dy.concatenate([final_scores, snippet_scores])
aligned_tokens.extend(snippet_tokens)
# Anonymized tokens
anonymized_scores, anonymized_tokens = self._score_anonymized_tokens(
prediction_input.input_sequence,
attention_results.scores)
if anonymized_scores:
final_scores = dy.concatenate([final_scores, anonymized_scores])
aligned_tokens.extend(anonymized_tokens)
return TokenPrediction(final_scores,
aligned_tokens,
attention_results,
decoder_state)
def construct_token_predictor(parameter_collection,
params,
vocabulary,
attention_key_size,
snippet_size,
anonymizer=None):
""" Constructs a token predictor given the parameters.
Inputs:
parameter_collection (dy.ParameterCollection): Contains the parameters.
params (dictionary): Contains the command line parameters/hyperparameters.
vocabulary (Vocabulary): Vocabulary object for output generation.
attention_key_size (int): The size of the attention keys.
anonymizer (Anonymizer): An anonymization object.
"""
if params.use_snippets and anonymizer and not params.previous_decoder_snippet_encoding:
return SnippetAnonymizationTokenPredictor(parameter_collection,
params,
vocabulary,
attention_key_size,
snippet_size,
anonymizer)
elif params.use_snippets and not params.previous_decoder_snippet_encoding:
return SnippetTokenPredictor(parameter_collection,
params,
vocabulary,
attention_key_size,
snippet_size)
elif anonymizer:
return AnonymizationTokenPredictor(parameter_collection,
params,
vocabulary,
attention_key_size,
anonymizer)
else:
return TokenPredictor(parameter_collection,
params,
vocabulary,
attention_key_size)