-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_model.py
572 lines (463 loc) · 23.7 KB
/
evaluate_model.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import argparse
import pprint
from enum import Enum
import os
import evaluate
import torch
from datasets import load_dataset
from evaluate import QuestionAnsweringEvaluator
from transformers import (
AutoModelForQuestionAnswering,
AutoTokenizer,
QuestionAnsweringPipeline,
XLMRobertaAdapterModel,
)
from datasets import Dataset
from transformers.adapters.composition import Stack
import jieba
from M2QA_Metric.m2qa_metric import M2QAMetric
MODEL_NAME = "xlm-roberta-base"
PATHS = {
# Baseline XLM-R models
"path_fully_fine_tuned_model": "/home/leon/UKP/M2QA/m2qa/Experiments/Trained_model/Thesis_used/xlm_r_fully_finetuned/",
"paths_xlm_r_domain_adapted": {
"wiki": "/home/leon/UKP/M2QA/m2qa/Experiments/Trained_model/Thesis_used/domain_models/xlm-r-wiki-512-squad",
"creative_writing": "/home/leon/UKP/M2QA/m2qa/Experiments/Trained_model/Thesis_used/domain_models/xlm-r-books-512-squad",
"news": "/home/leon/UKP/M2QA/m2qa/Experiments/Trained_model/Thesis_used/domain_models/xlm-r-news-squad-512-64",
"product_reviews": "/home/leon/UKP/M2QA/m2qa/Experiments/Trained_model/Thesis_used/domain_models/xlm-r-reviews-yelp-squad-512-64",
},
# MAD-X+Domain
"mad-x-domain": {
"domains": {
"wiki": "AdapterHub/m2qa-xlm-roberta-base-mad-x-domain-wiki",
"creative_writing": "AdapterHub/m2qa-xlm-roberta-base-mad-x-domain-creative-writing",
"news": "AdapterHub/m2qa-xlm-roberta-base-mad-x-domain-news",
"product_reviews": "AdapterHub/m2qa-xlm-roberta-base-mad-x-domain-product-reviews",
},
"languages": {
# The original MAD-X language adapters
"english": "en/wiki@ukp",
"german": "de/wiki@ukp",
"chinese": "zh/wiki@ukp",
"turkish": "tr/wiki@ukp",
},
"qa_head": {
"name": "mad-x+domain qa_head",
"path": "AdapterHub/m2qa-xlm-roberta-base-mad-x-domain-qa-head",
},
},
# MAD-X²
"mad-x-2": {
"domains": {
"wiki": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-wiki",
"creative_writing": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-creative-writing",
"news": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-news",
"product_reviews": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-product-reviews",
},
"languages": {
"english": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-english",
"german": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-german",
"chinese": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-chinese",
"turkish": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-turkish",
},
"qa_head": {
"name": "mad-x-2-qa_adapter",
"path": "AdapterHub/m2qa-xlm-roberta-base-mad-x-2-qa-head",
},
},
}
##########################################################################################
# Other
M2QA_LANGUAGES_AND_DOMAINS_TO_EVALUATE = {
"german": [
"news",
"creative_writing",
"product_reviews",
],
"chinese": [
"news",
"creative_writing",
"product_reviews",
],
"turkish": [
"news",
"creative_writing",
"product_reviews",
],
}
XQUAD_LANGUAGE_MAPPING = {
"german": "de",
"english": "en",
"chinese": "zh",
"turkish": "tr",
}
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
qa_evaluator: QuestionAnsweringEvaluator = evaluate.evaluator("question-answering") # type: ignore
##########################################################################################
# Helper functions
class ExperimentSetup(str, Enum):
XLM_R = "xlm-r fine-tuned on SQuADv2"
XLM_R_DOMAIN_ADAPTED = "domain adapted xlm-r fine-tuned on SQuADv2"
# MAD-X+Domain
MAD_X_DOMAIN = "MAD-X+Domain: language adapter + domain adapter + squad head"
# MAD-X²
MAD_X_2 = "MAD-X²: language adapter + domain adapter + squad head"
# MAD-X+Domain intermediate combinations to see effect of language and domain adapters
MAD_X_DOMAIN_HEAD = "xlm-r + squad head + en adapter + wiki adapter"
MAD_X_DOMAIN_ONLY_LANGUAGE = "xlm-r + squad head + language adapter + wiki adapter"
MAD_X_DOMAIN_ONLY_DOMAIN = "xlm-r + squad head + en adapter + domain adapter"
def set_active_adapter_for_setup(model, experiment_setup: ExperimentSetup, language: str = None, domain: str = None):
match experiment_setup:
# XLM-R Baselines don't have adapters
case ExperimentSetup.XLM_R:
pass
case ExperimentSetup.XLM_R_DOMAIN_ADAPTED:
pass
# MAD-X+Domain
case ExperimentSetup.MAD_X_DOMAIN:
model.active_adapters = Stack(
f"mad-x+domain {language}", f"mad-x+domain {domain}", PATHS["mad-x-domain"]["qa_head"]["name"]
)
case ExperimentSetup.MAD_X_DOMAIN_HEAD:
model.active_adapters = Stack(
"mad-x+domain english", "mad-x+domain wiki", PATHS["mad-x-domain"]["qa_head"]["name"]
)
case ExperimentSetup.MAD_X_DOMAIN_ONLY_LANGUAGE:
model.active_adapters = Stack(
f"mad-x+domain {language}", "mad-x+domain wiki", PATHS["mad-x-domain"]["qa_head"]["name"]
)
case ExperimentSetup.MAD_X_DOMAIN_ONLY_DOMAIN:
model.active_adapters = Stack(
"mad-x+domain english", f"mad-x+domain {domain}", PATHS["mad-x-domain"]["qa_head"]["name"]
)
# MAD-X²
case ExperimentSetup.MAD_X_2:
model.active_adapters = Stack(
f"mad-x-2 {language}", f"mad-x-2 {domain}", PATHS["mad-x-2"]["qa_head"]["name"]
)
case _:
raise ValueError(f"Unknown experiment setup: {experiment_setup}")
if experiment_setup == ExperimentSetup.XLM_R or experiment_setup == ExperimentSetup.XLM_R_DOMAIN_ADAPTED:
print("Using fully finetuned model")
else:
print(f"Using model:\n{model.adapter_summary()}")
def load_adapters(model: XLMRobertaAdapterModel):
# 1. Load MAD-X+Domain adapters
model.load_adapter(PATHS["mad-x-domain"]["qa_head"]["path"], load_as=PATHS["mad-x-domain"]["qa_head"]["name"])
for language, adapter_path in PATHS["mad-x-domain"]["languages"].items():
model.load_adapter(adapter_path, load_as=f"mad-x+domain {language}")
for domain, adapter_path in PATHS["mad-x-domain"]["domains"].items():
model.load_adapter(adapter_path, load_as=f"mad-x+domain {domain}")
# 2. Load MAD-X² adapters
model.load_adapter(PATHS["mad-x-2"]["qa_head"]["path"], load_as=PATHS["mad-x-2"]["qa_head"]["name"])
for language, adapter_path in PATHS["mad-x-2"]["languages"].items():
model.load_adapter(adapter_path, load_as=f"mad-x-2 {language}")
for domain, adapter_path in PATHS["mad-x-2"]["domains"].items():
model.load_adapter(adapter_path, load_as=f"mad-x-2 {domain}")
def load_m2qa_dataset(args: argparse.Namespace):
m2qa_dataset = {}
for language in M2QA_LANGUAGES_AND_DOMAINS_TO_EVALUATE:
domains = M2QA_LANGUAGES_AND_DOMAINS_TO_EVALUATE[language]
m2qa_dataset[language] = load_dataset(
"json",
data_files={domain: f"../m2qa_dataset/{language}/{domain}.json" for domain in domains},
)
if args.only_unanswerable:
for language in m2qa_dataset:
for domain in m2qa_dataset[language]:
m2qa_dataset[language][domain] = m2qa_dataset[language][domain].filter(
lambda example: len(example["answers"]["text"]) == 0
)
if args.only_answerable:
for language in m2qa_dataset:
for domain in m2qa_dataset[language]:
m2qa_dataset[language][domain] = m2qa_dataset[language][domain].filter(
lambda example: len(example["answers"]["text"]) != 0
)
return m2qa_dataset
def print_intermediate_results(dataset: str, model_description: str, results: dict):
print(f" {dataset}: {model_description} ".center(150, "="))
print(f"F1 / EM: {results['f1']:.2f} / {results['exact']:.2f}")
print(f"{results}\n\n")
def load_domain_adapted_model(domain: str):
if domain not in PATHS["paths_xlm_r_domain_adapted"].keys():
raise ValueError(f"No domain-adapted model available for domain {domain}")
return AutoModelForQuestionAnswering.from_pretrained(PATHS["paths_xlm_r_domain_adapted"][domain])
def load_xlm_r_model():
return AutoModelForQuestionAnswering.from_pretrained(PATHS["path_fully_fine_tuned_model"])
def load_adapter_model():
model = XLMRobertaAdapterModel.from_pretrained(MODEL_NAME)
load_adapters(model)
return model
def add_spaces(text, language):
if language == "chinese":
# Add whitespaces between words
return " ".join(jieba.lcut(text))
else:
# Don't add whitespaces for non-Chinese texts
return text
def _evaluate(model, tokenizer, data, experiment_name: str, experiment_setup: ExperimentSetup, args, language, metric):
prepared_data = qa_evaluator.load_data(data=data, subset=None, split=None)
print(f"Prepared data for {language}: {prepared_data}")
if args.add_white_spaces_to_chinese and language == "chinese":
print(f"loop")
prepared_data = prepared_data.map(
lambda example: {
"id": example["id"],
"question": example["question"],
"context": add_spaces(example["context"], language),
"answers": example["answers"],
},
load_from_cache_file=False,
)
pipe = qa_evaluator.prepare_pipeline(
model_or_pipeline=model,
tokenizer=tokenizer,
device=(0 if torch.cuda.is_available() else -1),
)
qa_evaluator.PIPELINE_KWARGS["handle_impossible_answer"] = True
metric_inputs, pipe_inputs = qa_evaluator.prepare_data(
data=prepared_data,
question_column="question",
context_column="context",
id_column="id",
label_column="answers",
)
predictions, perf_results = qa_evaluator.call_pipeline(pipe, batch_size=16, **pipe_inputs)
predictions = qa_evaluator.predictions_processor(predictions, squad_v2_format=True, ids=prepared_data["id"])
# remove the white spaces from the predictions
if args.add_white_spaces_to_chinese and language == "chinese":
for prediction in predictions["predictions"]:
prediction["prediction_text"] = prediction["prediction_text"].replace(" ", "")
total_number_of_questions = 0
number_of_correctly_classified_questions = 0
total_of_questions_classified_as_unanswerable = 0
example_id_map = {example["id"]: i for i, example in enumerate(data)}
for prediction in predictions["predictions"]:
total_number_of_questions += 1
example = data[example_id_map[prediction["id"]]]
if len(prediction["prediction_text"]) == 0 and len(example["answers"]["text"]) == 0:
number_of_correctly_classified_questions += 1
if len(prediction["prediction_text"]) != 0 and len(example["answers"]["text"]) != 0:
number_of_correctly_classified_questions += 1
if len(prediction["prediction_text"]) == 0:
total_of_questions_classified_as_unanswerable += 1
metric_inputs.update(predictions)
# Compute metrics from references and predictions
# use default HuggingFace values
qa_evaluator.METRIC_KWARGS["language"] = language
full_result = qa_evaluator.compute_metric(
metric=metric,
metric_inputs=metric_inputs,
strategy="simple",
confidence_level="0.95",
n_resamples=9999,
random_state=None,
)
# print_intermediate_results(f"M2QA - {language} - {domain}: ", experiment_setup.value, full_result)
print_intermediate_results(experiment_name, experiment_setup.value, full_result)
print(f"Total number of questions: {total_number_of_questions}")
print(f"Number of correctly classified questions: {number_of_correctly_classified_questions}")
print(f"Total of questions classified as unanswerable: {total_of_questions_classified_as_unanswerable}")
print(
f"Correct classification accuracy: {number_of_correctly_classified_questions / total_number_of_questions * 100:.2f}%\n\n"
)
return full_result
##########################################################################################
# Evaluation functions: SQuADv2, XQuAD, M2QA
def _check_domains_to_evaluate(domains_to_evaluate, domains_list):
# If domains_to_evaluate is None, return domains_list
# If domains_to_evaluate is not None, check if all the domains are in domains_list
# If not, print a warning and return the intersection of domains_to_evaluate and domains_list
if domains_to_evaluate is None:
return domains_list
else:
domains_to_evaluate = set(domains_to_evaluate)
domains_list = set(domains_list)
if not domains_to_evaluate.issubset(domains_list):
print(
f"Warning: The domains to evaluate {domains_to_evaluate} are not a subset of the available domains {domains_list}."
)
return domains_to_evaluate.intersection(domains_list)
else:
return domains_to_evaluate
def evaluate_on_m2qa(
experiment_setup: ExperimentSetup,
model,
dataset,
all_results_dict: dict,
args,
metric,
domains_to_evaluate=None, # needed for the domain-adapted XLM-R models
):
all_results = {}
for language in M2QA_LANGUAGES_AND_DOMAINS_TO_EVALUATE.keys():
all_results[language] = {}
domains_list = M2QA_LANGUAGES_AND_DOMAINS_TO_EVALUATE[language]
domains = _check_domains_to_evaluate(domains_to_evaluate, domains_list)
print(f"Domains to evaluate: {domains} for {language}")
for domain in domains:
print(f"M2QA: Computing results for {language} - {domain}")
set_active_adapter_for_setup(model, experiment_setup, language, domain)
result = _evaluate(
model,
tokenizer,
dataset[language][domain],
f"M2QA - {language} - {domain}",
experiment_setup,
args,
language,
metric,
)
all_results[language][domain] = result
all_results_dict["m2qa"][experiment_setup.value] = all_results
def evaluate_on_xquad(
experiment_setup: ExperimentSetup, model, all_results_dict: dict, args: argparse.Namespace, metric
):
for language in ["english", "german", "chinese", "turkish"]:
set_active_adapter_for_setup(model, experiment_setup, language=language, domain="wiki")
xquad_dataset = load_dataset(
"xquad", f"xquad.{XQUAD_LANGUAGE_MAPPING[language]}", revision="8c2924a720ea543c2b6346284e21d3b85b1c2996"
)
if args.only_unanswerable:
print("XQuAD does not have unanswerable questions; skipping evaluation")
return
results = _evaluate(
model,
tokenizer,
xquad_dataset["validation"],
f"XQuAD - {language}",
experiment_setup,
args,
language,
metric,
)
all_results_dict["xquad"][language][experiment_setup.value] = results
def evaluate_on_squad(
experiment_setup: ExperimentSetup,
model,
all_results_dict: dict,
args: argparse.Namespace,
metric,
) -> None:
set_active_adapter_for_setup(model, experiment_setup, language="english", domain="wiki")
squad_v2_dataset = load_dataset("squad_v2")
if args.only_unanswerable:
squad_v2_dataset = squad_v2_dataset.filter(lambda example: len(example["answers"]["text"]) == 0)
if args.only_answerable:
squad_v2_dataset = squad_v2_dataset.filter(lambda example: len(example["answers"]["text"]) != 0)
qa_evaluator.METRIC_KWARGS["language"] = "english"
results = qa_evaluator.compute(
tokenizer=tokenizer,
model_or_pipeline=model,
data=squad_v2_dataset["validation"],
metric=metric,
squad_v2_format=True,
)
print_intermediate_results("SQuADv2 ", experiment_setup.value, results)
all_results_dict["squad_v2"][experiment_setup.value] = results
def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Evaluate the performance of different models on the SQuADv2, XQuAD, and M2QA datasets. You can choose which models to evaluate and on which datasets. You may use multiple flags to evaluate multiple models and datasets in one run.",
)
# Baseline XLM-R models
parser.add_argument("--evaluate_xlm_r", type=bool, default=False, const=True, nargs="?", help="Model: Evaluate XLM-R model") # fmt: skip
parser.add_argument("--evaluate_xlm_r_domain_adapted", type=bool, default=False, const=True, nargs="?", help="Model: Evaluate domain-adapted XLM-R model") # fmt: skip
# Adapter setups: MAD-X+Domain & MAD-X²
parser.add_argument("--evaluate_mad_x_domain", type=bool, default=False, const=True, nargs="?", help="Model: Evaluate MAD-X+Domain adapter setup") # fmt: skip
parser.add_argument("--evaluate_mad_x_2", type=bool, default=False, const=True, nargs="?", help="Model: Evaluate MAD-X² adapter setup") # fmt: skip
# Not used in the paper: Evaluate intermediate combinations of MAD-X+Domain, i.e. only head, head + language adapter and head + domain adapter
parser.add_argument("--evaluate_mad_x_domain_intermediate_combinations", type=bool, default=False, const=True, nargs="?", help="(not used in paper) Evaluate intermediate combinations of MAD-X+Domain adapter setup") # fmt: skip
# Which dataset(s) to evaluate on
parser.add_argument("--evaluate_squad", type=bool, default=False, const=True, nargs="?", help="Dataset: Evaluate on SQuAD dataset") # fmt: skip
parser.add_argument("--evaluate_xquad", type=bool, default=False, const=True, nargs="?", help="Dataset: Evaluate on XQuAD dataset") # fmt: skip
parser.add_argument("--evaluate_m2qa", type=bool, default=False, const=True, nargs="?", help="Dataset: Evaluate on M2QA dataset") # fmt: skip
# Choose how to filter the m2qa dataset
parser.add_argument("--only_answerable", type=bool, default=False, const=True, nargs="?", help="Filter: Evaluate only answerable questions in M2QA dataset") # fmt: skip
parser.add_argument("--only_unanswerable", type=bool, default=False, const=True, nargs="?", help="Filter: Evaluate only unanswerable questions in M2QA dataset") # fmt: skip
# Ablation Studies
parser.add_argument("--add_white_spaces_to_chinese", type=bool, default=False, const=True, nargs="?", help="Ablation study: Add white spaces to Chinese text") # fmt: skip
parser.add_argument("--use_m2qa_evaluation_metric", type=bool, default=False, const=True, nargs="?", help="Ablation study: Use M2QA evaluation metric") # fmt: skip
args = parser.parse_args()
if args.only_answerable and args.only_unanswerable:
raise ValueError("Evaluating only answerable and only unanswerable answers is exclusive")
return args
def main():
# 0. Parse parameters and prepare dicts to save the results.
args = parse_arguments()
all_results_dict = {}
model = None
if args.use_m2qa_evaluation_metric:
metric = M2QAMetric()
else:
metric = evaluate.load("squad_v2")
# 1. Evaluate SQuADv2
if args.evaluate_squad:
all_results_dict["squad_v2"] = {}
if args.evaluate_xlm_r:
model = load_xlm_r_model()
evaluate_on_squad(ExperimentSetup.XLM_R, model, all_results_dict, args, metric)
if args.evaluate_xlm_r_domain_adapted:
model = load_domain_adapted_model("wiki")
evaluate_on_squad(ExperimentSetup.XLM_R_DOMAIN_ADAPTED, model, all_results_dict, args, metric)
if args.evaluate_mad_x_domain:
model = load_adapter_model()
evaluate_on_squad(ExperimentSetup.MAD_X_DOMAIN, model, all_results_dict, args, metric)
if args.evaluate_mad_x_2:
model = load_adapter_model()
evaluate_on_squad(ExperimentSetup.MAD_X_2, model, all_results_dict, args, metric)
# 2. Evaluate XQuAD
if args.evaluate_xquad:
all_results_dict["xquad"] = {"german": {}, "english": {}, "chinese": {}, "turkish": {}}
if args.evaluate_xlm_r:
model = load_xlm_r_model()
evaluate_on_xquad(ExperimentSetup.XLM_R, model, all_results_dict, args, metric)
if args.evaluate_xlm_r_domain_adapted:
model = load_domain_adapted_model("wiki")
evaluate_on_xquad(ExperimentSetup.XLM_R_DOMAIN_ADAPTED, model, all_results_dict, args, metric)
if args.evaluate_mad_x_domain:
model = load_adapter_model()
evaluate_on_xquad(ExperimentSetup.MAD_X_DOMAIN, model, all_results_dict, args, metric)
if args.evaluate_mad_x_domain_intermediate_combinations:
model = load_adapter_model()
evaluate_on_xquad(ExperimentSetup.MAD_X_DOMAIN_HEAD, model, all_results_dict, args, metric)
if args.evaluate_mad_x_2:
model = load_adapter_model()
evaluate_on_xquad(ExperimentSetup.MAD_X_2, model, all_results_dict, args, metric)
# 4. Evaluate on M2QA dataset
if args.evaluate_m2qa:
all_results_dict["m2qa"] = {}
m2qa_dataset = load_m2qa_dataset(args)
if args.evaluate_xlm_r:
model = load_xlm_r_model()
evaluate_on_m2qa(ExperimentSetup.XLM_R, model, m2qa_dataset, all_results_dict, args, metric) # fmt: skip
if args.evaluate_xlm_r_domain_adapted:
print("==== Evaluate fully finetuned CREATIVE WRITING model ====")
model = load_domain_adapted_model("creative_writing")
evaluate_on_m2qa(ExperimentSetup.XLM_R_DOMAIN_ADAPTED, model, m2qa_dataset, all_results_dict, args, metric, domains_to_evaluate=["creative_writing"]) # fmt: skip
print("==== Evaluate fully finetuned PRODUCT REVIEWS model ====")
model = load_domain_adapted_model("product_reviews")
evaluate_on_m2qa(ExperimentSetup.XLM_R_DOMAIN_ADAPTED, model, m2qa_dataset, all_results_dict, args, metric, domains_to_evaluate=["product_reviews"]) # fmt: skip
print("==== Evaluate fully finetuned NEWS model ====")
model = load_domain_adapted_model("news")
evaluate_on_m2qa(ExperimentSetup.XLM_R_DOMAIN_ADAPTED, model, m2qa_dataset, all_results_dict, args, metric, domains_to_evaluate=["news"]) # fmt: skip
if args.evaluate_mad_x_domain:
model = load_adapter_model()
evaluate_on_m2qa(ExperimentSetup.MAD_X_DOMAIN, model, m2qa_dataset, all_results_dict, args, metric) # fmt: skip
if args.evaluate_mad_x_domain_intermediate_combinations:
model = load_adapter_model()
evaluate_on_m2qa(ExperimentSetup.MAD_X_DOMAIN_HEAD, model, m2qa_dataset, all_results_dict, args, metric) # fmt: skip
evaluate_on_m2qa(ExperimentSetup.MAD_X_DOMAIN_ONLY_LANGUAGE, model, m2qa_dataset, all_results_dict, args, metric) # fmt: skip
evaluate_on_m2qa(ExperimentSetup.MAD_X_DOMAIN_ONLY_DOMAIN, model, m2qa_dataset, all_results_dict, args, metric) # fmt: skip
if args.evaluate_mad_x_2:
model = load_adapter_model()
evaluate_on_m2qa(ExperimentSetup.MAD_X_2, model, m2qa_dataset, all_results_dict, args, metric) # fmt: skip
# Store results
all_results_file_name = "Evaluation/evaluation_results.txt"
# Create folder if it does not exist
os.makedirs(os.path.dirname(all_results_file_name), exist_ok=True)
with open(all_results_file_name, "w") as f:
pprint.pprint(all_results_dict, f, width=250)
print(f"Stored all results in {all_results_file_name}")
if __name__ == "__main__":
main()