This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_parser.py
executable file
·488 lines (419 loc) · 15.2 KB
/
run_parser.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/env python
# Copyright (c) 2021 Kemal Kurniawan
from itertools import chain
from pathlib import Path
import os
import math
from einops import rearrange
from gensim.models.keyedvectors import KeyedVectors
from rnnr import Event, Runner
from rnnr.attachments import EpochTimer, LambdaReducer, ProgressBar, SumReducer
from rnnr.callbacks import maybe_stop_early
from sacred import Experiment
from sacred.observers import MongoObserver
from sacred.utils import apply_backspaces_and_linefeeds
from text2array import BucketIterator, ShuffleIterator, Vocab
import torch
from callbacks import (
batch2tensors,
compute_total_arc_type_scores,
evaluate_batch,
get_n_items,
log_grads,
log_stats,
predict_batch,
save_state_dict,
set_train_mode,
update_params,
)
from ingredients.corpus import ing as corpus_ing, read_samples
from models import SelfAttGraph
from serialization import dump, load
from utils import extend_word_embedding, print_accs
ex = Experiment("xduft-parser-testrun", ingredients=[corpus_ing])
ex.captured_out_filter = apply_backspaces_and_linefeeds
# Setup mongodb observer
mongo_url = os.getenv("SACRED_MONGO_URL")
db_name = os.getenv("SACRED_DB_NAME")
if None not in (mongo_url, db_name):
ex.observers.append(MongoObserver.create(url=mongo_url, db_name=db_name))
@ex.config
def default():
# directory to save training artifacts
artifacts_dir = "artifacts"
# whether to overwrite existing artifacts directory
overwrite = False
# discard train/dev/test samples with length greater than these numbers
max_length = {}
# path to word embedding in word2vec format
word_emb_path = "wiki.en.vec"
# size of POS tag embedding
tag_size = 50
# number of heads in transformer encoder
n_heads = 10
# number of layers in transformer encoder
n_layers = 6
# size of feedforward hidden layer in transformer encoder
ff_size = 2048
# size of keys and values in the transformer encoder
kv_size = 35
# word dropout rate
p_word = 0.5
# output dim dropout rate
p_out = 0.5
# size of dep arc representation
arc_size = 128
# size of dep type representation
type_size = 128
# batch size
batch_size = 16
# learning rate
lr = 1e-4
# max number of epochs
max_epoch = 1000
# device to run on [cpu, cuda]
device = "cuda" if torch.cuda.is_available() else "cpu"
# how many epochs to wait before early stopping
patience = 50
# whether to operate in the space of projective trees
projective = False
# whether to consider multi-root trees (otherwise only single-root trees)
multiroot = True
# load parameters from this file under artifacts directory (only for evaluate)
load_params = "model.pth"
# whether to do type-wise evaluation (only for evaluate)
type_wise = False
# load types vocabulary from this file
load_types_vocab_from = ""
@ex.named_config
def testrun():
seed = 12345
tag_size = 10
n_heads = 2
n_layers = 2
ff_size = 7
kv_size = 6
arc_size = 3
type_size = 3
max_epoch = 3
corpus = dict(portion=0.05)
@ex.named_config
def ahmadetal():
max_length = dict(train=100, dev=140, test=140)
tag_size = 50
n_heads = 8
n_layers = 6
ff_size = 512
kv_size = 64
p_word = 0.2
p_out = 0.2
arc_size = 512
type_size = 128
batch_size = 80
lr = 1e-4
corpus = dict(normalize_digits=True)
@ex.named_config
def heetal_eval_setup():
max_length = dict(dev=150, test=150)
@ex.capture
def make_model(
vocab,
_log,
word_emb_path="wiki.en.vec",
artifacts_dir="artifacts",
tag_size=50,
n_heads=10,
n_layers=6,
ff_size=2048,
kv_size=64,
p_word=0.5,
p_out=0.5,
arc_size=128,
type_size=128,
):
kv = KeyedVectors.load_word2vec_format(word_emb_path)
_log.info("Creating model")
model = SelfAttGraph(
len(vocab["words"]),
len(vocab["types"]),
len(vocab["tags"]),
word_size=kv.vector_size,
tag_size=tag_size,
n_heads=n_heads,
n_layers=n_layers,
ff_size=ff_size,
kv_size=kv_size,
word_dropout=p_word,
outdim_dropout=p_out,
arc_size=arc_size,
type_size=type_size,
)
_log.info("Model created with %d parameters", sum(p.numel() for p in model.parameters()))
weight = torch.randn(len(vocab["words"]), kv.vector_size)
cnt_pre, cnt_unk = 0, 0
for w in vocab["words"]:
wid = vocab["words"].index(w)
if w in kv:
weight[wid] = torch.from_numpy(kv[w])
cnt_pre += 1
elif w.lower() in kv:
weight[wid] = torch.from_numpy(kv[w.lower()])
cnt_pre += 1
else:
cnt_unk += 1
with torch.no_grad():
# freeze embedding to preserve alignment
model.word_emb = torch.nn.Embedding.from_pretrained(weight, freeze=True)
_log.info("Initialized %d words with pre-trained embedding", cnt_pre)
_log.info("Found %d unknown words", cnt_unk)
path = Path(artifacts_dir) / "model.yml"
_log.info("Saving model metadata to %s", path)
path.write_text(dump(model), encoding="utf8")
return model
@ex.capture
def run_eval(
model,
vocab,
samples,
device="cpu",
projective=False,
multiroot=True,
batch_size=32,
type_wise=False,
):
runner = Runner()
runner.on(
Event.BATCH,
[
batch2tensors(device, vocab),
set_train_mode(model, training=False),
compute_total_arc_type_scores(model, vocab),
predict_batch(projective, multiroot),
evaluate_batch(vocab["types"] if type_wise else None),
get_n_items(),
],
)
n_tokens = sum(len(s["words"]) for s in samples)
ProgressBar(leave=False, total=n_tokens, unit="tok").attach_on(runner)
SumReducer("counts").attach_on(runner)
if type_wise:
LambdaReducer(
"type2counts",
lambda o1, o2: {y: o1[y] + o2[y] for y in vocab["types"]},
value="tw_output",
).attach_on(runner)
with torch.no_grad():
runner.run(BucketIterator(samples, lambda s: len(s["words"]), batch_size))
return runner.state
@ex.command
def evaluate(
_log,
_run,
max_length=None,
artifacts_dir="artifacts",
load_params="model.pth",
word_emb_path="wiki.id.vec",
device="cpu",
):
"""Evaluate a trained self-attention graph-based parser."""
if max_length is None:
max_length = {}
artifacts_dir = Path(artifacts_dir)
samples = {}
try:
samples["dev"] = list(read_samples(which="dev", max_length=max_length.get("dev")))
except FileNotFoundError:
_log.info("Dev set is not found, skipping")
samples["test"] = list(read_samples(which="test", max_length=max_length.get("test")))
for wh in samples:
n_toks = sum(len(s["words"]) for s in samples[wh])
_log.info("Read %d %s samples and %d tokens", len(samples[wh]), wh, n_toks)
path = artifacts_dir / "vocab.yml"
_log.info("Loading source vocabulary from %s", path)
vocab = load(path.read_text(encoding="utf8"))
for name in vocab.keys():
_log.info("Found %d %s", len(vocab[name]), name)
_log.info("Extending vocab with target words")
old_n_words = len(vocab["words"])
vocab.extend(chain(*samples.values()), ["words"])
_log.info("Found %d words now", len(vocab["words"]))
samples = {wh: list(vocab.stoi(samples[wh])) for wh in samples}
path = artifacts_dir / "model.yml"
_log.info("Loading model from metadata %s", path)
model = load(path.read_text(encoding="utf8"))
path = artifacts_dir / load_params
_log.info("Loading model parameters from %s", path)
model.load_state_dict(torch.load(path, "cpu"))
if len(vocab["words"]) > old_n_words:
_log.info("Creating extended word embedding layer")
if word_emb_path:
kv = KeyedVectors.load_word2vec_format(word_emb_path)
assert model.word_emb.embedding_dim == kv.vector_size
else:
_log.warning(
"Word embedding file not specified; any extra target words will be treated as unks"
)
kv = None
with torch.no_grad():
model.word_emb = torch.nn.Embedding.from_pretrained(
extend_word_embedding(
model.word_emb.weight,
vocab["words"],
kv,
vocab["words"].index(vocab.UNK_TOKEN),
)
)
model.to(device)
dev_accs = {}
for wh in samples:
_log.info("Evaluating on %s", wh)
state = run_eval(model, vocab, samples[wh])
accs = state["counts"].accs
if wh == "dev":
dev_accs = accs
print_accs(accs, on=wh, run=_run)
if "type2counts" in state:
_log.info("Type-wise accuracies:")
for type_, c in state["type2counts"].items():
for key, acc in c.accs.items():
metric_name = f"{wh}_{type_}_{key}"
_log.info(f"{metric_name}: {acc:.2%}")
_run.log_scalar(metric_name, acc)
for suffix in ("", "_nopunct"):
metric_name = f"{wh}_{type_}_n_arcs{suffix}"
_log.info("%s: %d", metric_name, getattr(c, f"n_arcs{suffix}"))
_run.log_scalar(metric_name, getattr(c, f"n_arcs{suffix}"))
return dev_accs.get("las_nopunct")
@ex.automain
def train(
_log,
_run,
_rnd,
artifacts_dir="artifacts",
overwrite=False,
max_length=None,
load_types_vocab_from=None,
batch_size=16,
device="cpu",
lr=0.001,
patience=5,
max_epoch=1000,
):
"""Train a self-attention graph-based parser."""
if max_length is None:
max_length = {}
artifacts_dir = Path(artifacts_dir)
_log.info("Creating artifacts directory %s", artifacts_dir)
artifacts_dir.mkdir(exist_ok=overwrite)
samples = {
wh: list(read_samples(which=wh, max_length=max_length.get(wh)))
for wh in ["train", "dev", "test"]
}
for wh in samples:
n_toks = sum(len(s["words"]) for s in samples[wh])
_log.info("Read %d %s samples and %d tokens", len(samples[wh]), wh, n_toks)
_log.info("Creating vocabulary")
vocab = Vocab.from_samples(chain(*samples.values()))
if load_types_vocab_from:
path = Path(load_types_vocab_from)
_log.info("Loading types vocab from %s", path)
vocab["types"] = load(path.read_text(encoding="utf8"))["types"]
_log.info("Vocabulary created")
for name in vocab:
_log.info("Found %d %s", len(vocab[name]), name)
path = artifacts_dir / "vocab.yml"
_log.info("Saving vocabulary to %s", path)
path.write_text(dump(vocab), encoding="utf8")
samples = {wh: list(vocab.stoi(samples[wh])) for wh in samples}
model = make_model(vocab)
model.to(device)
_log.info("Creating optimizer")
opt = torch.optim.Adam(model.parameters(), lr=lr)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(opt, mode="max", factor=0.5)
trainer = Runner()
trainer.state.update({"dev_larcs_nopunct": -1, "dev_uarcs_nopunct": -1})
trainer.on(Event.BATCH, [batch2tensors(device, vocab), set_train_mode(model)])
@trainer.on(Event.BATCH)
def compute_loss(state):
bat = state["batch"]
words, tags, heads, types = bat["words"], bat["tags"], bat["heads"], bat["types"]
mask = bat["mask"]
arc_scores, type_scores = model(words, tags, mask, heads)
arc_scores = arc_scores.masked_fill(~mask.unsqueeze(2), -1e9) # mask padding heads
type_scores[..., vocab["types"].index(Vocab.PAD_TOKEN)] = -1e9
# remove root
arc_scores, type_scores = arc_scores[:, :, 1:], type_scores[:, 1:]
heads, types, mask = heads[:, 1:], types[:, 1:], mask[:, 1:]
arc_scores = rearrange(arc_scores, "bsz slen1 slen2 -> (bsz slen2) slen1")
heads = heads.reshape(-1)
arc_loss = torch.nn.functional.cross_entropy(arc_scores, heads, reduction="none")
type_scores = rearrange(type_scores, "bsz slen ntypes -> (bsz slen) ntypes")
types = types.reshape(-1)
type_loss = torch.nn.functional.cross_entropy(type_scores, types, reduction="none")
arc_loss = arc_loss.masked_select(mask.reshape(-1)).mean()
type_loss = type_loss.masked_select(mask.reshape(-1)).mean()
loss = arc_loss + type_loss
state["loss"] = loss
arc_loss, type_loss = arc_loss.item(), type_loss.item()
state["stats"] = {
"arc_ppl": math.exp(arc_loss),
"type_ppl": math.exp(type_loss),
}
state["extra_stats"] = {"arc_loss": arc_loss, "type_loss": type_loss}
state["n_items"] = bat["mask"].long().sum().item()
trainer.on(Event.BATCH, [update_params(opt), log_grads(_run, model), log_stats(_run)])
@trainer.on(Event.EPOCH_FINISHED)
def eval_on_dev(state):
_log.info("Evaluating on dev")
eval_state = run_eval(model, vocab, samples["dev"])
accs = eval_state["counts"].accs
print_accs(accs, run=_run, step=state["n_iters"])
scheduler.step(accs["las_nopunct"])
if eval_state["counts"].larcs_nopunct > state["dev_larcs_nopunct"]:
state["better"] = True
elif eval_state["counts"].larcs_nopunct < state["dev_larcs_nopunct"]:
state["better"] = False
elif eval_state["counts"].uarcs_nopunct > state["dev_uarcs_nopunct"]:
state["better"] = True
else:
state["better"] = False
if state["better"]:
_log.info("Found new best result on dev!")
state["dev_larcs_nopunct"] = eval_state["counts"].larcs_nopunct
state["dev_uarcs_nopunct"] = eval_state["counts"].uarcs_nopunct
state["dev_accs"] = accs
state["dev_epoch"] = state["epoch"]
else:
_log.info("Not better, the best so far is epoch %d:", state["dev_epoch"])
print_accs(state["dev_accs"])
print_accs(state["test_accs"], on="test")
@trainer.on(Event.EPOCH_FINISHED)
def maybe_eval_on_test(state):
if not state["better"]:
return
_log.info("Evaluating on test")
eval_state = run_eval(model, vocab, samples["test"])
state["test_accs"] = eval_state["counts"].accs
print_accs(state["test_accs"], on="test", run=_run, step=state["n_iters"])
trainer.on(
Event.EPOCH_FINISHED,
[
maybe_stop_early(patience=patience),
save_state_dict("model", model, under=artifacts_dir, when="better"),
],
)
EpochTimer().attach_on(trainer)
n_tokens = sum(len(s["words"]) for s in samples["train"])
ProgressBar(stats="stats", total=n_tokens, unit="tok").attach_on(trainer)
bucket_key = lambda s: (len(s["words"]) - 1) // 10
trn_iter = ShuffleIterator(
BucketIterator(samples["train"], bucket_key, batch_size, shuffle_bucket=True, rng=_rnd),
rng=_rnd,
)
_log.info("Starting training")
try:
trainer.run(trn_iter, max_epoch)
except KeyboardInterrupt:
_log.info("Interrupt detected, training will abort")
else:
return trainer.state["dev_accs"]["las_nopunct"]