forked from ShulinCao/OpenNRE-PyTorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
387 lines (318 loc) · 13.9 KB
/
Copy pathparse.py
File metadata and controls
387 lines (318 loc) · 13.9 KB
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
import functools
import dynet as dy
import numpy as np
import trees
START = "<START>"
STOP = "<STOP>"
UNK = "<UNK>"
def augment(scores, oracle_index):
assert isinstance(scores, dy.Expression)
shape = scores.dim()[0]
assert len(shape) == 1
increment = np.ones(shape)
increment[oracle_index] = 0
return scores + dy.inputVector(increment)
class Feedforward(object):
def __init__(self, model, input_dim, hidden_dims, output_dim):
self.spec = locals()
self.spec.pop("self")
self.spec.pop("model")
self.model = model.add_subcollection("Feedforward")
self.weights = []
self.biases = []
dims = [input_dim] + hidden_dims + [output_dim]
for prev_dim, next_dim in zip(dims, dims[1:]):
self.weights.append(self.model.add_parameters((next_dim, prev_dim)))
self.biases.append(self.model.add_parameters(next_dim))
def param_collection(self):
return self.model
@classmethod
def from_spec(cls, spec, model):
return cls(model, **spec)
def __call__(self, x):
for i, (weight, bias) in enumerate(zip(self.weights, self.biases)):
weight = dy.parameter(weight)
bias = dy.parameter(bias)
x = dy.affine_transform([bias, weight, x])
if i < len(self.weights) - 1:
x = dy.rectify(x)
return x
class TopDownParser(object):
def __init__(
self,
model,
tag_vocab,
word_vocab,
label_vocab,
tag_embedding_dim,
word_embedding_dim,
lstm_layers,
lstm_dim,
label_hidden_dim,
split_hidden_dim,
dropout,
):
self.spec = locals()
self.spec.pop("self")
self.spec.pop("model")
self.model = model.add_subcollection("Parser")
self.tag_vocab = tag_vocab
self.word_vocab = word_vocab
self.label_vocab = label_vocab
self.lstm_dim = lstm_dim
self.tag_embeddings = self.model.add_lookup_parameters(
(tag_vocab.size, tag_embedding_dim))
self.word_embeddings = self.model.add_lookup_parameters(
(word_vocab.size, word_embedding_dim))
self.lstm = dy.BiRNNBuilder(
lstm_layers,
tag_embedding_dim + word_embedding_dim,
2 * lstm_dim,
self.model,
dy.VanillaLSTMBuilder)
self.f_label = Feedforward(
self.model, 2 * lstm_dim, [label_hidden_dim], label_vocab.size)
self.f_split = Feedforward(
self.model, 2 * lstm_dim, [split_hidden_dim], 1)
self.dropout = dropout
def param_collection(self):
return self.model
@classmethod
def from_spec(cls, spec, model):
return cls(model, **spec)
def parts_parse(self, sentence, gold=None):
is_train = gold is not None
if is_train:
self.lstm.set_dropout(self.dropout)
else:
self.lstm.disable_dropout()
embeddings = []
for tag, word in [(START, START)] + sentence + [(STOP, STOP)]:
tag_embedding = self.tag_embeddings[self.tag_vocab.index(tag)]
if word not in (START, STOP):
count = self.word_vocab.count(word)
if not count or (is_train and np.random.rand() < 1 / (1 + count)):
word = UNK
word_embedding = self.word_embeddings[self.word_vocab.index(word)]
embeddings.append(dy.concatenate([tag_embedding, word_embedding]))
lstm_outputs = self.lstm.transduce(embeddings)
return lstm_outputs
def parse(self, sentence, gold=None, explore=True):
is_train = gold is not None
if is_train:
self.lstm.set_dropout(self.dropout)
else:
self.lstm.disable_dropout()
embeddings = []
for tag, word in [(START, START)] + sentence + [(STOP, STOP)]:
tag_embedding = self.tag_embeddings[self.tag_vocab.index(tag)]
if word not in (START, STOP):
count = self.word_vocab.count(word)
if not count or (is_train and np.random.rand() < 1 / (1 + count)):
word = UNK
word_embedding = self.word_embeddings[self.word_vocab.index(word)]
embeddings.append(dy.concatenate([tag_embedding, word_embedding]))
lstm_outputs = self.lstm.transduce(embeddings)
@functools.lru_cache(maxsize=None)
def get_span_encoding(left, right):
forward = (
lstm_outputs[right][:self.lstm_dim] -
lstm_outputs[left][:self.lstm_dim])
backward = (
lstm_outputs[left + 1][self.lstm_dim:] -
lstm_outputs[right + 1][self.lstm_dim:])
return dy.concatenate([forward, backward])
def helper(left, right):
assert 0 <= left < right <= len(sentence)
label_scores = self.f_label(get_span_encoding(left, right))
if is_train:
oracle_label = gold.oracle_label(left, right)
oracle_label_index = self.label_vocab.index(oracle_label)
label_scores = augment(label_scores, oracle_label_index)
label_scores_np = label_scores.npvalue()
argmax_label_index = int(
label_scores_np.argmax() if right - left < len(sentence) else
label_scores_np[1:].argmax() + 1)
argmax_label = self.label_vocab.value(argmax_label_index)
if is_train:
label = argmax_label if explore else oracle_label
label_loss = (
label_scores[argmax_label_index] -
label_scores[oracle_label_index]
if argmax_label != oracle_label else dy.zeros(1))
else:
label = argmax_label
label_loss = label_scores[argmax_label_index]
if right - left == 1:
tag, word = sentence[left]
tree = trees.LeafParseNode(left, tag, word)
if label:
tree = trees.InternalParseNode(label, [tree])
return [tree], label_loss
left_encodings = []
right_encodings = []
for split in range(left + 1, right):
left_encodings.append(get_span_encoding(left, split))
right_encodings.append(get_span_encoding(split, right))
left_scores = self.f_split(dy.concatenate_to_batch(left_encodings))
right_scores = self.f_split(dy.concatenate_to_batch(right_encodings))
split_scores = left_scores + right_scores
split_scores = dy.reshape(split_scores, (len(left_encodings),))
if is_train:
oracle_splits = gold.oracle_splits(left, right)
oracle_split = min(oracle_splits)
oracle_split_index = oracle_split - (left + 1)
split_scores = augment(split_scores, oracle_split_index)
split_scores_np = split_scores.npvalue()
argmax_split_index = int(split_scores_np.argmax())
argmax_split = argmax_split_index + (left + 1)
if is_train:
split = argmax_split if explore else oracle_split
split_loss = (
split_scores[argmax_split_index] -
split_scores[oracle_split_index]
if argmax_split != oracle_split else dy.zeros(1))
else:
split = argmax_split
split_loss = split_scores[argmax_split_index]
left_trees, left_loss = helper(left, split)
right_trees, right_loss = helper(split, right)
children = left_trees + right_trees
if label:
children = [trees.InternalParseNode(label, children)]
return children, label_loss + split_loss + left_loss + right_loss
children, loss = helper(0, len(sentence))
assert len(children) == 1
tree = children[0]
if is_train and not explore:
assert gold.convert().linearize() == tree.convert().linearize()
return tree, loss
class ChartParser(object):
def __init__(
self,
model,
tag_vocab,
word_vocab,
label_vocab,
tag_embedding_dim,
word_embedding_dim,
lstm_layers,
lstm_dim,
label_hidden_dim,
dropout,
):
self.spec = locals()
self.spec.pop("self")
self.spec.pop("model")
self.model = model.add_subcollection("Parser")
self.tag_vocab = tag_vocab
self.word_vocab = word_vocab
self.label_vocab = label_vocab
self.lstm_dim = lstm_dim
self.tag_embeddings = self.model.add_lookup_parameters(
(tag_vocab.size, tag_embedding_dim))
self.word_embeddings = self.model.add_lookup_parameters(
(word_vocab.size, word_embedding_dim))
self.lstm = dy.BiRNNBuilder(
lstm_layers,
tag_embedding_dim + word_embedding_dim,
2 * lstm_dim,
self.model,
dy.VanillaLSTMBuilder)
self.f_label = Feedforward(
self.model, 2 * lstm_dim, [label_hidden_dim], label_vocab.size - 1)
self.dropout = dropout
def param_collection(self):
return self.model
@classmethod
def from_spec(cls, spec, model):
return cls(model, **spec)
def parse(self, sentence, gold=None):
is_train = gold is not None
if is_train:
self.lstm.set_dropout(self.dropout)
else:
self.lstm.disable_dropout()
embeddings = []
for tag, word in [(START, START)] + sentence + [(STOP, STOP)]:
tag_embedding = self.tag_embeddings[self.tag_vocab.index(tag)]
if word not in (START, STOP):
count = self.word_vocab.count(word)
if not count or (is_train and np.random.rand() < 1 / (1 + count)):
word = UNK
word_embedding = self.word_embeddings[self.word_vocab.index(word)]
embeddings.append(dy.concatenate([tag_embedding, word_embedding]))
lstm_outputs = self.lstm.transduce(embeddings)
@functools.lru_cache(maxsize=None)
def get_span_encoding(left, right):
forward = (
lstm_outputs[right][:self.lstm_dim] -
lstm_outputs[left][:self.lstm_dim])
backward = (
lstm_outputs[left + 1][self.lstm_dim:] -
lstm_outputs[right + 1][self.lstm_dim:])
return dy.concatenate([forward, backward])
@functools.lru_cache(maxsize=None)
def get_label_scores(left, right):
non_empty_label_scores = self.f_label(get_span_encoding(left, right))
return dy.concatenate([dy.zeros(1), non_empty_label_scores])
def helper(force_gold):
if force_gold:
assert is_train
chart = {}
for length in range(1, len(sentence) + 1):
for left in range(0, len(sentence) + 1 - length):
right = left + length
label_scores = get_label_scores(left, right)
if is_train:
oracle_label = gold.oracle_label(left, right)
oracle_label_index = self.label_vocab.index(oracle_label)
if force_gold:
label = oracle_label
label_score = label_scores[oracle_label_index]
else:
if is_train:
label_scores = augment(label_scores, oracle_label_index)
label_scores_np = label_scores.npvalue()
argmax_label_index = int(
label_scores_np.argmax() if length < len(sentence) else
label_scores_np[1:].argmax() + 1)
argmax_label = self.label_vocab.value(argmax_label_index)
label = argmax_label
label_score = label_scores[argmax_label_index]
if length == 1:
tag, word = sentence[left]
tree = trees.LeafParseNode(left, tag, word)
if label:
tree = trees.InternalParseNode(label, [tree])
chart[left, right] = [tree], label_score
continue
if force_gold:
oracle_splits = gold.oracle_splits(left, right)
oracle_split = min(oracle_splits)
best_split = oracle_split
else:
best_split = max(
range(left + 1, right),
key=lambda split:
chart[left, split][1].value() +
chart[split, right][1].value())
left_trees, left_score = chart[left, best_split]
right_trees, right_score = chart[best_split, right]
children = left_trees + right_trees
if label:
children = [trees.InternalParseNode(label, children)]
chart[left, right] = (
children, label_score + left_score + right_score)
children, score = chart[0, len(sentence)]
assert len(children) == 1
return children[0], score
tree, score = helper(False)
if is_train:
oracle_tree, oracle_score = helper(True)
assert oracle_tree.convert().linearize() == gold.convert().linearize()
correct = tree.convert().linearize() == gold.convert().linearize()
loss = dy.zeros(1) if correct else score - oracle_score
return tree, loss
else:
return tree, score