/
joint_metrics.py
311 lines (262 loc) · 10.4 KB
/
joint_metrics.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
# Custom Metrics for Joint NLU.
# last edited: 10.2.2021
# SP
import torch
import numpy as np
# Dataloading and Batching classes
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.dataloader import default_collate
# Training classes
from transformers import Trainer, TrainingArguments, DataCollatorForTokenClassification
# Tokenizers and Models
from transformers import (
AutoConfig,
AutoModel,
AutoTokenizer,
RobertaTokenizer,
XLMRobertaTokenizer,
)
# joint XLM-R NL
from joint_nlu_models import *
from typing import Dict, NamedTuple, Optional
from sklearn.metrics import classification_report, f1_score, accuracy_score
from preprocessing.conll_loader import ConLLLoader, intent_labels_list, slot_labels_list
from sklearn_crfsuite import metrics as seq_metrics
# Obsolete. Now uses the recommended solution using list comprehensions.
# def remove_padding(gold_slots, pred_slots):
# zipped = zip(gold_slots, pred_slots)
# sanitized_gold = []
# sanitized_pred = []
# for gold, pred in zipped:
# if gold != -100:
# sanitized_gold.append(gold)
# sanitized_pred.append(pred)
# return sanitized_gold, sanitized_pred
def show_align_labels(p,tokenized_utterances, intent_label_list, slot_label_list, ):
intent_predictions, slot_predictions = p.predictions
intent_labels, slot_labels = p.label_ids
slot_predictions = np.argmax(slot_predictions, axis=2)
intent_predictions = np.argmax(intent_predictions, axis=1)
slot_predictions_clean = [
[slot_label_list[p] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
slot_labels_clean = [
[slot_label_list[l] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
for pred, gold, itent_pred, intent_gold, utter in zip(slot_predictions_clean, slot_labels_clean,intent_predictions, intent_predictions,tokenized_utterances):
print()
print(intent_label_list[itent_pred],"\t",intent_label_list[intent_gold])
for tok_pred, tok_gold,real_tok in zip(pred,gold,utter):
print(real_tok,"\t", tok_pred,"\t", tok_gold)
def joint_classification_report(p, intent_label_list, slot_label_list, verbose=True):
intent_predictions, slot_predictions = p.predictions
intent_labels, slot_labels = p.label_ids
slot_predictions = np.argmax(slot_predictions, axis=2)
intent_predictions = np.argmax(intent_predictions, axis=1)
slot_predictions_clean = [
[p for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
slot_labels_clean = [
[l for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
labels_slot = list(range(len(slot_label_list)))
labels_intent = list(range(len(intent_label_list)))
seq_acc = seq_metrics.sequence_accuracy_score(
slot_labels_clean, slot_predictions_clean
)
if verbose:
print(
classification_report(
intent_labels,
intent_predictions,
target_names=intent_label_list,
labels=labels_intent,
digits = 4,
)
)
print(
seq_metrics.flat_classification_report(
slot_labels_clean,
slot_predictions_clean,
target_names=slot_label_list,
labels=labels_slot,
digits = 4,
)
)
print("sequence accuracy: ", seq_acc)
# In efficient
# can be done in one run and pretty print output reconstructed from dictionary
slot_res_dict = seq_metrics.flat_classification_report(
slot_labels_clean,
slot_predictions_clean,
target_names=slot_label_list,
labels=labels_slot,
output_dict=True,
digits = 5,
)
intent_res_dict = classification_report(
intent_labels,
intent_predictions,
target_names=intent_label_list,
labels=labels_intent,
output_dict=True,
digits = 5,
)
return {
"sequence_accuracy": seq_acc,
"slot_results": slot_res_dict,
"intent_results": intent_res_dict,
}
def exact_match(p):
intent_predictions, slot_predictions = p.predictions
intent_labels, slot_labels = p.label_ids
intent_predictions = np.argmax(intent_predictions, axis=1)
slot_predictions = np.argmax(slot_predictions, axis=2)
intent_matches = (intent_labels == intent_predictions)
slot_predictions_clean = [
[p for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
slot_labels_clean = [
[l for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
# for seq_lab, seq_pred in zip(slot_labels_clean, slot_predictions_clean):
# print(seq_lab, seq_pred)
seq_match = [
True if np.array_equal(yseq_true, yseq_pred) else False
for yseq_true, yseq_pred in zip(slot_labels_clean, slot_predictions_clean)
]
exact_matches = np.logical_and(intent_matches,seq_match)
#print(list(zip(intent_matches,seq_match)))
num_exact = np.sum(exact_matches)
total = len(intent_labels)
return num_exact/ float(total)
#print(seq_match)
def running_metrics(p):
intent_predictions, slot_predictions = p.predictions
intent_labels, slot_labels = p.label_ids
slot_predictions = np.argmax(slot_predictions, axis=2)
intent_predictions = np.argmax(intent_predictions, axis=1)
slot_predictions_clean = [
[p for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
slot_labels_clean = [
[l for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(slot_predictions, slot_labels)
]
intent_f1 = f1_score(intent_labels, intent_predictions, average="macro")
intent_accuracy = accuracy_score(intent_labels, intent_predictions)
flat_acc = seq_metrics.flat_accuracy_score(
slot_labels_clean, slot_predictions_clean
)
flat_f1 = seq_metrics.flat_f1_score(
slot_labels_clean, slot_predictions_clean, average="macro"
)
slt_f1_weighted = seq_metrics.flat_f1_score(
slot_labels_clean, slot_predictions_clean, average="weighted"
)
return {
"flat slot accuracy": flat_acc,
"flat slot f1": flat_f1,
"weighted slot f1": slt_f1_weighted,
"intent f1": intent_f1,
"intent accuracy": intent_accuracy,
}
# temporary predict function
# now works with integrated prediction_loop from Trainer class
# def predict(
# model, test_dataloader, intent_labels_list=None, slot_labels_list=None, report=True
# ):
# gold_intent = []
# gold_slots = []
# pred_intent = []
# pred_slots = []
# gold_slots_seq = []
# pred_slots_seq = []
# for example in test_loader:
# input_ids = example["input_ids"]
# attention_mask = example["attention_mask"]
# results = model(
# input_ids=input_ids.to(device), attention_mask=attention_mask.to(device)
# )
# intent = np.argmax(results["intents"].cpu().detach().numpy(), axis=1)
# slots = np.argmax(results["slots"].cpu().detach().numpy(), axis=2)
# slots = slots[0]
# real_intent = example["intent_label_ids"].tolist()
# real_slots = example["slot_labels_ids"]
# real_slots = [i.item() for i in real_slots]
# # print(intent,real_intent)
# pred_intent.extend(intent)
# pred_slots.extend(slots)
# gold_intent.extend(real_intent)
# gold_slots.extend(real_slots)
# gold_slots_seq.append(real_slots)
# pred_slots_seq.append(slots)
# sanitized_gold, sanitized_pred = remove_padding(gold_slots, pred_slots)
# if report:
# # TODO catch NONE parameters
# print(
# classification_report(
# gold_intent, pred_intent, target_names=intent_labels_list
# )
# )
# print(
# classification_report(
# sanitized_gold,
# sanitized_pred,
# labels=list(range(len(slot_labels_list))),
# target_names=slot_labels_list,
# )
# )
# # print(metrics.sequence_accuracy_score(gold_slots_seq, pred_slots_seq))
# return {"intents": pred_intent, "slots": pred_slots}
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
path2train_en = "/home/santi/BA/multilingual_task_oriented_dialog_slotfilling/en/train-en.conllu"
path2eval_en = (
"/home/santi/BA/multilingual_task_oriented_dialog_slotfilling/en/eval-en.conllu"
)
path2test_en = (
"/home/santi/BA/multilingual_task_oriented_dialog_slotfilling/en/eval-en.conllu"
)
pretrained_name = "xlm-roberta-base"
tokenizer = AutoTokenizer.from_pretrained(pretrained_name)
data_collator = DataCollatorForTokenClassification(tokenizer)
train_set = ConLLLoader(
path2train_en, tokenizer, intent_labels_list, slot_labels_list
)
val_set = ConLLLoader(path2eval_en, tokenizer, intent_labels_list, slot_labels_list)
test_set = ConLLLoader(
path2test_en, tokenizer, intent_labels_list, slot_labels_list
)
training_args = TrainingArguments(
output_dir="./results", # output directory
num_train_epochs=10, # total # of training epochs
per_device_train_batch_size=32, # batch size per device during training
warmup_steps=500, # number of warmup steps for learning rate scheduler
weight_decay=0.01, # strength of weight decay
logging_dir="./logs",
learning_rate=5e-5,
save_steps=2000,
label_names=["intent_label_ids", "slot_labels_ids"],
evaluation_strategy="epoch",
)
conf = config_init(pretrained_name)
model = JointClassifier(conf, num_intents=12, num_slots=31)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_set,
eval_dataset=val_set,
data_collator=data_collator,
compute_metrics=running_metrics,
)
res = trainer.evaluate()
print(res)
trainer.train()