-
Notifications
You must be signed in to change notification settings - Fork 0
/
nobi_classifier.py
253 lines (211 loc) · 9.67 KB
/
nobi_classifier.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
import os
os.environ["CUDA_VISIBLE_DEVICES"]='0'
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["WANDB_DISABLED"] = "true"
import torch
torch.manual_seed(3407)
import random
random.seed(3407)
import numpy as np
np.random.seed(3407)
from transformers import XLMRobertaTokenizerFast
from transformers import XLMRobertaForTokenClassification
from transformers import AutoTokenizer, AutoModelForTokenClassification
from transformers import Trainer, TrainingArguments
from transformers import EarlyStoppingCallback
from transformers import logging
from huggingface_hub import HfApi, Repository
import torch
import json
import argparse
import pickle as pkl
from utils import *
logging.set_verbosity_warning()
def convert_type(df):
for i in range(len(df)):
df['words'].iloc[i] = eval(df['words'].iloc[i])
df['labels'].iloc[i] = eval(df['labels'].iloc[i])
return df
def get_data(trainings_data, val_data, test_data):
#train
train_tags = trainings_data['labels'].tolist()
train_texts= trainings_data['words'].tolist()
#val
val_tags = val_data['labels'].tolist()
val_texts= val_data['words'].tolist()
#test
test_tags = test_data['labels'].tolist()
test_texts= test_data['words'].tolist()
return train_tags, train_texts, val_tags, val_texts, test_tags, test_texts
def tokenize_and_align_labels(texts, tags):
# lowercase
texts = [[x.lower() for x in l] for l in texts]
tokenized_inputs = tokenizer(
texts,
padding=True,
truncation=True,
# We use this argument because the texts in our dataset are lists of words (with a label for each word).
is_split_into_words=True,
)
labels = []
for i, label in enumerate(tags):
word_ids = tokenized_inputs.word_ids(batch_index=i)
previous_word_idx = None
label_ids = []
for word_idx in word_ids:
# Special tokens have a word id that is None. We set the label to -100 so they are automatically
# ignored in the loss function.
if word_idx is None:
label_ids.append(-100)
# We set the label for the first token of each word.
elif word_idx != previous_word_idx:
label_ids.append(label_to_id[label[word_idx]])
# For the other tokens in a word, we set the label to either the current label or -100, depending on
# the label_all_tokens flag.
else:
label_ids.append(-100)
previous_word_idx = word_idx
labels.append(label_ids)
tokenized_inputs["labels"] = labels
return tokenized_inputs
# create dataset that can be used for training with the huggingface trainer
class OurDataset(torch.utils.data.Dataset):
def __init__(self, encodings, labels):
self.encodings = encodings
self.labels = labels
def __getitem__(self, idx):
item = {key: torch.tensor(val[idx]) for key, val in self.encodings.items()}
item['labels'] = torch.tensor(self.labels[idx])
return item
def __len__(self):
return len(self.labels)
#compute the metrics TermEval style for Trainer
def compute_metrics(p):
predictions, labels = p
predictions = np.argmax(predictions, axis=2)
# Remove ignored index (special tokens)
true_predictions = [
[label_list[p] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
extracted_terms = extract_terms(true_predictions, val) # ??????
extracted_terms = set([item.lower() for item in extracted_terms])
gold_set=gold_validation # ??????
# print(extracted_terms)
true_pos=extracted_terms.intersection(gold_set)
# print("True pos", true_pos)
recall=len(true_pos)/len(gold_set)
precision=len(true_pos)/len(extracted_terms)
f1 = 2*(precision*recall)/(precision+recall) if precision + recall != 0 else 0
return {
"precision": precision,
"recall": recall,
"f1": f1,
}
def extract_terms(token_predictions, val_texts):
extracted_terms = set()
# go over all predictions
for i in range(len(token_predictions)):
pred = token_predictions[i]
txt = val_texts[i]
# print(len(pred), len(txt))
for j in range(len(pred)):
if pred[j]=="BN-Term" or pred[j]=="IN-Term":
extracted_terms.add(txt[j])
# if right tag build term and add it to the set otherwise just continue
if pred[j]=="B-Term" or pred[j]=="BN-Term":
# print(pred[j], txt[j])
term=txt[j]
for k in range(j+1,len(pred)):
if pred[k]=="I-Term" or pred[k]=="IN-Term":
term+=" "+txt[k]
# print(pred[k], txt[k], term)
else: break
extracted_terms.add(term)
return extracted_terms
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--train1", default="train1", type=str, required=False)
parser.add_argument("--train2", default="train2", type=str, required=False)
parser.add_argument("--val", default="val", type=str, required=False)
parser.add_argument("--test", default="test", type=str, required=False)
parser.add_argument("--gold_val", default="gold_val", type=str, required=False)
parser.add_argument("--gold_test", default="gold_test", type=str, required=False)
parser.add_argument("--output_dir", default="output_dir", type=str, required=False)
parser.add_argument("--log_dir", default="log", type=str, required=False)
parser.add_argument("--metric_path", default="metric", type=str, required=False)
parser.add_argument("--prediction_path", default="prediction", type=str, required=False)
parser.add_argument("--hf_repo_id", type=str, required=True, help="The ID of the Hugging Face Hub repository to push to.")
args = parser.parse_args()
train1 = pd.read_csv(args.train1)
train2 = pd.read_csv(args.train2)
trainings_data = convert_type(pd.concat([train1, train2]))
val_data = convert_type(pd.read_csv(args.val))
test_data = convert_type(pd.read_csv(args.test))
gold_set_for_validation= pd.read_csv(args.gold_val, header=None, delimiter='\t')[0].tolist()
gold_set_for_test = pd.read_csv(args.gold_test, header=None, delimiter='\t')[0].tolist()
train_tags, train_texts, val_tags, val_texts, test_tags, test_texts = get_data(trainings_data, val_data, test_data)
tokenizer = XLMRobertaTokenizerFast.from_pretrained("xlm-roberta-base")
#align labels with tokenization from XLM-R
label_list=['O', 'B-Term', 'BN-Term', 'IN-Term', 'I-Term']
label_to_id = {l: i for i, l in enumerate(label_list)}
num_labels=len(label_list)
train_input_and_labels = tokenize_and_align_labels(train_texts, train_tags)
val_input_and_labels = tokenize_and_align_labels(val_texts, val_tags)
test_input_and_labels = tokenize_and_align_labels(test_texts, test_tags)
train_dataset = OurDataset(train_input_and_labels, train_input_and_labels["labels"])
val_dataset = OurDataset(val_input_and_labels, val_input_and_labels["labels"])
test_dataset = OurDataset(test_input_and_labels, test_input_and_labels["labels"])
start = timeit.default_timer()
training_args = TrainingArguments(
output_dir= args.output_dir, # output directory
num_train_epochs=20, # total # of training epochs
per_device_train_batch_size=32, # batch size per device during training
per_device_eval_batch_size=32, # batch size for evaluation
learning_rate=2e-5,
logging_dir= args.log_dir, # directory for storing logs
evaluation_strategy= 'steps', # or use epoch here
eval_steps = 500,
load_best_model_at_end=True, #loads the model with the best evaluation score
metric_for_best_model="f1",
greater_is_better=True,
push_to_hub=True, # enable pushing to the hub
hub_model_id=args.hf_repo_id, # specify the Hugging Face repo ID
hub_strategy="end", # push at the end of training
)
# initialize model
model = XLMRobertaForTokenClassification.from_pretrained("xlm-roberta-base", num_labels=num_labels)
val = val_texts
gold_validation = gold_set_for_validation
# initialize huggingface trainer
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
callbacks = [EarlyStoppingCallback(early_stopping_patience=5)]
)
# train
trainer.train()
# Save model to Hugging Face Hub
trainer.push_to_hub()
stop = timeit.default_timer()
print('Time: ', stop - start)
val = test_texts
gold_validation = gold_set_for_test
#test
test_predictions, test_labels, test_metrics = trainer.predict(test_dataset)
test_predictions = np.argmax(test_predictions, axis=2)
# Remove ignored index (special tokens)
true_test_predictions = [
[label_list[p] for (p, l) in zip(test_prediction, test_label) if l != -100]
for test_prediction, test_label in zip(test_predictions, test_labels)
]
test_extracted_terms = extract_terms(true_test_predictions, test_texts)
extracted, gold, true_pos, precision, recall, fscore = computeTermEvalMetrics(test_extracted_terms, set(gold_set_for_test))
with open(args.metric_path, 'w') as f:
f. write(json.dumps([[extracted, gold, true_pos, precision, recall, fscore]]))
with open(args.prediction_path, 'w') as f:
f. write(json.dumps(list(test_extracted_terms)))