-
Notifications
You must be signed in to change notification settings - Fork 3
/
train.py
executable file
·184 lines (155 loc) · 5.76 KB
/
train.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
"""Train File."""
## Imports
import argparse
# import itertools
import copy
import os
import numpy as np
from omegaconf import OmegaConf
import torch
import torch.nn as nn
from copy import deepcopy
from datasets import load_metric
from evaluation.semeval2021 import f1
from sklearn.metrics import f1_score
from transformers import (
AutoTokenizer,
AutoModelForSequenceClassification,
DataCollatorForTokenClassification,
default_data_collator,
TrainingArguments,
Trainer,
)
from sklearn.metrics import f1_score
from src.utils.configuration import Config
from src.datasets import *
from src.models import *
from src.modules.preprocessors import *
from src.utils.mapper import configmapper
import os
import gc
def compute_metrics_token(p):
predictions, labels = p
predictions = np.argmax(predictions, axis=2) ## batch_size, seq_length
offset_wise_scores = []
# print(len(predictions))
for i, prediction in enumerate(predictions):
## Batch Wise
# print(len(prediction))
ground_spans = eval(validation_spans[i])
predicted_spans = []
for j, tokenwise_prediction in enumerate(
prediction[: len(validation_offsets_mapping[i])]
):
if tokenwise_prediction == 1:
predicted_spans += list(
range(
validation_offsets_mapping[i][j][0],
validation_offsets_mapping[i][j][1],
)
)
offset_wise_scores.append(f1(predicted_spans, ground_spans))
results_offset = np.mean(offset_wise_scores)
true_predictions = [
[p for (p, l) in zip(pred, label) if l != -100]
for pred, label in zip(predictions, labels)
]
true_labels = [
[l for (p, l) in zip(pred, label) if l != -100]
for pred, label in zip(predictions, labels)
]
results = np.mean(
[
f1_score(true_label, true_preds)
for true_label, true_preds in zip(true_labels, true_predictions)
]
)
return {"Token-Wise F1": results, "Offset-Wise F1": results_offset}
dirname = os.path.dirname(__file__) ## For Paths Relative to Current File
## Config
parser = argparse.ArgumentParser(prog="train.py", description="Train a model.")
parser.add_argument(
"--train",
type=str,
action="store",
help="The configuration for model training/evaluation",
)
parser.add_argument(
"--data",
type=str,
action="store",
help="The configuration for data",
)
args = parser.parse_args()
# print(vars(args))
train_config = OmegaConf.load(args.train)
data_config = OmegaConf.load(args.data)
print(data_config.train_files)
dataset = configmapper.get_object("datasets", data_config.name)(data_config)
untokenized_train_dataset = dataset.dataset
tokenized_train_dataset = dataset.tokenized_inputs
tokenized_test_dataset = dataset.test_tokenized_inputs
model_class = configmapper.get_object("models", train_config.model_name)
if "toxic-bert" in train_config.pretrained_args.pretrained_model_name_or_path:
toxicbert_model = AutoModelForSequenceClassification.from_pretrained(
train_config.pretrained_args.pretrained_model_name_or_path
)
train_config.pretrained_args.pretrained_model_name_or_path = "bert-base-uncased"
model = model_class.from_pretrained(**train_config.pretrained_args)
model.bert = deepcopy(toxicbert_model.bert)
gc.collect()
elif "toxic-roberta" in train_config.pretrained_args.pretrained_model_name_or_path:
toxicroberta_model = AutoModelForSequenceClassification.from_pretrained(
train_config.pretrained_args.pretrained_model_name_or_path
)
train_config.pretrained_args.pretrained_model_name_or_path = "roberta-base"
model = model_class.from_pretrained(**train_config.pretrained_args)
model.roberta = deepcopy(toxicroberta_model.roberta)
gc.collect()
else:
model = model_class.from_pretrained(**train_config.pretrained_args)
tokenizer = AutoTokenizer.from_pretrained(data_config.model_checkpoint_name)
if "crf" in train_config.model_name:
data_collator = DataCollatorForTokenClassification(tokenizer)
compute_metrics = None
elif not "spans" in train_config.model_name:
validation_spans = untokenized_train_dataset["validation"]["spans"]
validation_offsets_mapping = tokenized_train_dataset["validation"]["offset_mapping"]
data_collator = DataCollatorForTokenClassification(tokenizer)
compute_metrics = compute_metrics_token
else:
data_collator = default_data_collator
compute_metrics = None
## Need to place data_collator
if "multi" in train_config.model_name:
args = TrainingArguments(
label_names=["start_positions", "end_positions"], **train_config.args
)
else:
args = TrainingArguments(**train_config.args)
if not os.path.exists(train_config.args.output_dir):
os.makedirs(train_config.args.output_dir)
checkpoints = sorted(
os.listdir(train_config.args.output_dir), key=lambda x: int(x.split("-")[1])
)
if len(checkpoints) != 0:
print("Found Checkpoints:")
print(checkpoints)
trainer = Trainer(
model=model,
args=args,
train_dataset=tokenized_train_dataset["train"],
eval_dataset=tokenized_train_dataset["validation"],
data_collator=data_collator,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
if len(checkpoints) != 0:
trainer.train(
os.path.join(train_config.args.output_dir, checkpoints[-1])
) ## Load from checkpoint
else:
trainer.train()
if not os.path.exists(train_config.save_model_path):
os.makedirs(train_config.save_model_path)
trainer.save_model(train_config.save_model_path)