-
Notifications
You must be signed in to change notification settings - Fork 2
/
training.py
481 lines (408 loc) · 15.2 KB
/
training.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
import json
import logging
import pickle
import random
from argparse import Namespace
from collections import defaultdict
from pathlib import Path
from typing import Dict, List
import numpy as np
import torch
import torch.nn as nn
from torch import Tensor
from tqdm import tqdm
from transformers.models.auto.tokenization_auto import AutoTokenizer
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.optimization import AdamW, get_linear_schedule_with_warmup
from configuration import ConfigurationParer
from data_process import RawPred, SparseCube, Data
from data_reader import DataReader, Dataset, RawTokenField, TokenField, Instance
from modeling import EntRelJointDecoder as TripletModel, CubeRE, Tagger
from nn_utils import get_n_trainable_parameters
from scoring import EntityScorer, QuintupletScorer, StrictScorer
from vocabulary import Vocabulary
logger = logging.getLogger(__name__)
def load_model(task: str, path: str = "", **kwargs):
model_class = dict(quintuplet=CubeRE, tagger=Tagger, triplet=TripletModel)[task]
if path:
return model_class.load(path)
else:
return model_class(**kwargs)
def run_eval(
path: str = "ckpt/quintuplet/best_model",
path_data="ckpt/quintuplet/dataset.pickle",
data_split: str = "dev",
task: str = "quintuplet",
path_in: str = "",
):
model = load_model(task, path)
dataset = Dataset.load(path_data)
cfg = model.cfg
evaluate(cfg, dataset, model, data_split, path_in=path_in)
def score_preds(path_pred: str, path_gold: str) -> dict:
preds = Data.load(path_pred).sents
sents = Data.load(path_gold).sents
results = {}
for scorer in [EntityScorer(), StrictScorer(), QuintupletScorer()]:
results[scorer.name] = scorer.run(preds, sents)
print(json.dumps(results, indent=2))
return results
def prepare_inputs(batch_inputs, device):
for k, v in batch_inputs.items():
device_id = device if device > -1 else None
if k in ["joint_label_matrix_mask", "quintuplet_matrix_mask"]:
batch_inputs[k] = torch.tensor(v, dtype=torch.bool, device=device_id)
if k in [
"tokens",
"joint_label_matrix",
"quintuplet_matrix",
"wordpiece_tokens",
"wordpiece_tokens_index",
"wordpiece_segment_ids",
]:
batch_inputs[k] = torch.tensor(v, dtype=torch.long, device=device_id)
return batch_inputs
def train(cfg, dataset, model):
logger.info("Training starting...")
for name, param in model.named_parameters():
logger.info(
"{!r}: size: {} requires_grad: {}.".format(
name, param.size(), param.requires_grad
)
)
logger.info(
"Trainable parameters size: {}.".format(get_n_trainable_parameters(model))
)
parameters = [
(name, param) for name, param in model.named_parameters() if param.requires_grad
]
no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
bert_layer_lr = {}
base_lr = cfg.bert_learning_rate
for i in range(11, -1, -1):
bert_layer_lr["." + str(i) + "."] = base_lr
base_lr *= cfg.lr_decay_rate
optimizer_grouped_parameters = []
for name, param in parameters:
params = {"params": [param], "lr": cfg.learning_rate}
if any(item in name for item in no_decay):
params["weight_decay_rate"] = 0.0
else:
if "bert" in name:
params["weight_decay_rate"] = cfg.adam_bert_weight_decay_rate
else:
params["weight_decay_rate"] = cfg.adam_weight_decay_rate
for bert_layer_name, lr in bert_layer_lr.items():
if bert_layer_name in name:
params["lr"] = lr
break
optimizer_grouped_parameters.append(params)
# noinspection PyTypeChecker
optimizer = AdamW(
optimizer_grouped_parameters,
betas=(cfg.adam_beta1, cfg.adam_beta2),
lr=cfg.learning_rate,
eps=cfg.adam_epsilon,
weight_decay=cfg.adam_weight_decay_rate,
correct_bias=False,
)
assert optimizer.step is not None
total_train_steps = (
(
dataset.get_dataset_size("train")
+ cfg.train_batch_size * cfg.gradient_accumulation_steps
- 1
)
/ (cfg.train_batch_size * cfg.gradient_accumulation_steps)
* cfg.epochs
)
num_warmup_steps = int(cfg.warmup_rate * total_train_steps) + 1
num_batches = cfg.epochs * dataset.get_dataset_size("train") // cfg.train_batch_size
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=num_warmup_steps,
num_training_steps=total_train_steps,
)
assert scheduler is not None
best_loss = 1e9
best_score = -1e9
accumulation_steps = 0
model.zero_grad()
seen_epochs = set()
epoch_info = dict(epoch=0.0)
for epoch, batch in tqdm(
dataset.get_batch("train", cfg.train_batch_size, None), total=num_batches
):
if epoch > cfg.epochs:
model.save(cfg.last_model_path)
break
if epoch not in seen_epochs:
seen_epochs.add(epoch)
if accumulation_steps != 0:
optimizer.step()
scheduler.step()
model.zero_grad()
dev_loss, dev_score = evaluate(cfg, dataset, model, data_split="dev")
epoch_info.update(
dev_loss=dev_loss,
best_loss=best_loss,
score=dev_score,
best_score=best_score,
)
if dev_loss < best_loss:
best_loss = dev_loss
if dev_score > best_score:
best_score = dev_score
logger.info(str(dict(save=cfg.best_model_path)))
model.save(cfg.best_model_path)
logger.info(str(epoch_info))
model.train()
batch["epoch"] = epoch - 1
outputs = model(prepare_inputs(batch, cfg.device))
loss = outputs["loss"]
for k, v in outputs.items():
if "loss" in k:
epoch_info[k] = round(v.cpu().item(), 4)
epoch_info.update(epoch=epoch)
if cfg.gradient_accumulation_steps > 1:
loss /= cfg.gradient_accumulation_steps
loss.backward()
accumulation_steps = (accumulation_steps + 1) % cfg.gradient_accumulation_steps
if accumulation_steps == 0:
nn.utils.clip_grad.clip_grad_norm_(
parameters=model.parameters(), max_norm=cfg.gradient_clipping
)
optimizer.step()
scheduler.step()
model.zero_grad()
def process_outputs(
batch_inputs: Dict[str, Tensor], batch_outputs: Dict[str, Tensor]
) -> List[dict]:
all_outputs = []
for i in range(len(batch_inputs["tokens_lens"])):
output = dict()
for k in set(batch_inputs.keys()).union(batch_outputs.keys()):
v = batch_inputs.get(k)
if v is None:
v = batch_outputs[k]
if k in ["quintuplet_preds"]:
output[k] = SparseCube.from_numpy(v[i].cpu().numpy()).dict()
if k in ["tokens", "joint_label_matrix", "joint_label_preds"]:
output[k] = v[i].cpu().numpy()
if k in [
"seq_len",
"all_separate_position_preds",
"all_ent_preds",
"all_rel_preds",
"all_q_preds",
]:
output[k] = v[i]
all_outputs.append(output)
return all_outputs
def evaluate(
cfg: Namespace,
dataset: Dataset,
model: nn.Module,
data_split: str,
path_in: str = "",
):
model.zero_grad()
losses = []
all_outputs = []
if path_in:
data_split = "pred"
max_len = {
"tokens": cfg.max_sent_len,
"wordpiece_tokens": cfg.max_wordpiece_len,
}
reader = DataReader(path_in, False, max_len)
fields = dataset.instance_dict["test"]["instance"].fields
instance = Instance(fields)
dataset.add_instance(
data_split, instance, reader, is_count=True, is_train=False
)
dataset.process_instance(data_split)
num_batches = dataset.get_dataset_size(data_split) // cfg.test_batch_size
for _, batch in tqdm(
dataset.get_batch(data_split, cfg.test_batch_size, None), total=num_batches
):
model.eval()
with torch.no_grad():
inputs = prepare_inputs(batch, cfg.device)
outputs = model(inputs)
losses.append(outputs["loss"].cpu().item())
all_outputs.extend(process_outputs(inputs, outputs))
# Save raw outputs
path = Path(cfg.save_dir) / f"raw_{data_split}.pkl"
print(dict(path=path))
with open(path, "wb") as f:
pickle.dump(all_outputs, f)
# Save processed sents
path = Path(cfg.save_dir) / f"{data_split}.json"
print(dict(path=path))
with open(path, "w") as f:
for r in all_outputs:
# noinspection Pydantic
sent = RawPred(**r).as_sentence(model.vocab)
f.write(sent.json() + "\n")
mapping = dict(train=cfg.train_file, dev=cfg.dev_file, test=cfg.test_file)
results = score_preds(path_pred=str(path), path_gold=path_in or mapping[data_split])
score = dict(
quintuplet=results["quintuplet"]["f1"],
triplet=results["entity"]["f1"] + results["strict triplet"]["f1"],
tagger=results["entity"]["f1"],
)[cfg.task]
return np.mean(losses), score
def main():
# config settings
parser = ConfigurationParer()
parser.add_save_cfgs()
parser.add_data_cfgs()
parser.add_model_cfgs()
parser.add_optimizer_cfgs()
parser.add_run_cfgs()
cfg = parser.parse_args()
logger.info(parser.format_values())
# set random seed
random.seed(cfg.seed)
torch.manual_seed(cfg.seed)
np.random.seed(cfg.seed)
if cfg.device > -1 and not torch.cuda.is_available():
logger.error("config conflicts: no gpu available, use cpu for training.")
cfg.device = -1
if cfg.device > -1:
torch.cuda.manual_seed(cfg.seed)
# define fields
tokens = TokenField("tokens", "tokens", "tokens", True)
joint_label_matrix = RawTokenField("joint_label_matrix", "joint_label_matrix")
quintuplet_shape = RawTokenField("quintuplet_shape", "quintuplet_shape")
quintuplet_entries = RawTokenField("quintuplet_entries", "quintuplet_entries")
wordpiece_tokens = TokenField(
"wordpiece_tokens", "wordpiece", "wordpiece_tokens", False
)
wordpiece_tokens_index = RawTokenField(
"wordpiece_tokens_index", "wordpiece_tokens_index"
)
wordpiece_segment_ids = RawTokenField(
"wordpiece_segment_ids", "wordpiece_segment_ids"
)
fields = [tokens, joint_label_matrix]
fields.extend([quintuplet_shape, quintuplet_entries])
if cfg.embedding_model in ["bert", "pretrained"]:
fields.extend([wordpiece_tokens, wordpiece_tokens_index, wordpiece_segment_ids])
# define counter and vocabulary
counter = defaultdict(lambda: defaultdict(int))
vocab = Vocabulary()
# define instance
train_instance = Instance(fields)
dev_instance = Instance(fields)
test_instance = Instance(fields)
# define dataset reader
max_len = {"tokens": cfg.max_sent_len, "wordpiece_tokens": cfg.max_wordpiece_len}
ent_rel_file = json.load(open(cfg.ent_rel_file, "r", encoding="utf-8"))
pretrained_vocab = {"ent_rel_id": ent_rel_file["id"]}
if cfg.embedding_model == "bert":
tokenizer = BertTokenizer.from_pretrained(cfg.bert_model_name)
logger.info("Load bert tokenizer successfully.")
pretrained_vocab["wordpiece"] = tokenizer.get_vocab()
elif cfg.embedding_model == "pretrained":
tokenizer = AutoTokenizer.from_pretrained(cfg.pretrained_model_name)
logger.info("Load {} tokenizer successfully.".format(cfg.pretrained_model_name))
pretrained_vocab["wordpiece"] = tokenizer.get_vocab()
else:
raise ValueError()
ace_train_reader = DataReader(cfg.train_file, False, max_len)
ace_dev_reader = DataReader(cfg.dev_file, False, max_len)
ace_test_reader = DataReader(cfg.test_file, False, max_len)
# define dataset
ace_dataset = Dataset("ACE2005")
ace_dataset.add_instance(
"train", train_instance, ace_train_reader, is_count=True, is_train=True
)
ace_dataset.add_instance(
"dev", dev_instance, ace_dev_reader, is_count=True, is_train=False
)
ace_dataset.add_instance(
"test", test_instance, ace_test_reader, is_count=True, is_train=False
)
min_count = {"tokens": 1}
no_pad_namespace = ["ent_rel_id"]
no_unk_namespace = ["ent_rel_id"]
contain_pad_namespace = {"wordpiece": tokenizer.pad_token}
contain_unk_namespace = {"wordpiece": tokenizer.unk_token}
ace_dataset.build_dataset(
vocab=vocab,
counter=counter,
min_count=min_count,
pretrained_vocab=pretrained_vocab,
no_pad_namespace=no_pad_namespace,
no_unk_namespace=no_unk_namespace,
contain_pad_namespace=contain_pad_namespace,
contain_unk_namespace=contain_unk_namespace,
)
wo_padding_namespace = []
ace_dataset.set_wo_padding_namespace(wo_padding_namespace=wo_padding_namespace)
if cfg.test:
vocab = Vocabulary.load(cfg.vocabulary_file)
else:
vocab.save(cfg.vocabulary_file)
# joint model
model = load_model(cfg.task, cfg=cfg, vocab=vocab, ent_rel_file=ent_rel_file)
if cfg.device > -1:
model.cuda(device=cfg.device)
if cfg.load_weight_path:
model.load_state_dict(CubeRE.load(cfg.load_weight_path).state_dict())
path_data = str(Path(cfg.save_dir) / "dataset.pickle")
ace_dataset.save(path_data)
train(cfg, ace_dataset, model)
run_eval(
path=cfg.best_model_path, path_data=path_data, data_split="test", task=cfg.task
)
"""
################################################################################
BERT-Large
p training.py \
--bert_model_name bert-large-uncased \
--save_dir ckpt/cube_prune_20_large_seed_0 \
--seed 0 \
--data_dir data/processed \
--prune_topk 20 \
--config_file config.yml
################################################################################
BERT-Base
p training.py \
--save_dir ckpt/cube_prune_20_seed_0 \
--seed 0 \
--data_dir data/processed \
--prune_topk 20 \
--config_file config.yml
p training.py \
--epochs 2 \
--save_dir ckpt/cube_prune_20_seed_0_epochs_2_copy \
--seed 0 \
--data_dir data/processed \
--prune_topk 20 \
--config_file config.yml
################################################################################
Triplet task (10)
p training.py \
--seed 0 \
--embedding_model pretrained \
--pretrained_model_name distilbert-base-uncased \
--save_dir ckpt/triplet_distilbert_seed_0 \
--data_dir data/processed \
--task triplet \
--config_file config.yml
################################################################################
Tagger task (10)
p training.py \
--seed 0 \
--embedding_model pretrained \
--pretrained_model_name distilbert-base-uncased \
--save_dir ckpt/tags_distilbert_seed_0 \
--data_dir data/processed_tags \
--task tagger \
--config_file config.yml
"""
if __name__ == "__main__":
main()