lambada.py 2.29 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""
The LAMBADA dataset: Word prediction requiring a broad discourse context∗
https://arxiv.org/pdf/1606.06031.pdf

LAMBADA is a dataset to evaluate the capabilities of computational models for text
understanding by means of a word prediction task. LAMBADA is a collection of narrative
passages sharing the characteristic that human subjects are able to guess their last
word if they are exposed to the whole passage, but not if they only see the last
sentence preceding the target word. To succeed on LAMBADA, computational models
cannot simply rely on local context, but must be able to keep track of information
in the broader discourse.

Homepage: https://zenodo.org/record/2630551#.X4Xzn5NKjUI
"""
Jonathan Tow's avatar
Jonathan Tow committed
15
16
import inspect
import lm_eval.datasets.lambada.lambada
&'s avatar
& committed
17
18
from lm_eval.base import Task, rf
from lm_eval.metrics import mean, perplexity
sdtblck's avatar
sdtblck committed
19
20


21
22
_CITATION = """
@misc{
Fabrizio Milo's avatar
Fabrizio Milo committed
23
    author={Paperno, Denis and Kruszewski, Germán and Lazaridou, Angeliki and Pham, Quan Ngoc and Bernardi, Raffaella and Pezzelle, Sandro and Baroni, Marco and Boleda, Gemma and Fernández, Raquel},
24
25
26
27
28
29
30
    title={The LAMBADA dataset},
    DOI={10.5281/zenodo.2630551},
    publisher={Zenodo},
    year={2016},
    month={Aug}
}
"""
sdtblck's avatar
sdtblck committed
31
32


33
class LAMBADA(Task):
Leo Gao's avatar
Leo Gao committed
34
    VERSION = 0
Jonathan Tow's avatar
Jonathan Tow committed
35
    DATASET_PATH = inspect.getfile(lm_eval.datasets.lambada.lambada)
sdtblck's avatar
sdtblck committed
36
37
38
39
40

    def has_training_docs(self):
        return False

    def has_validation_docs(self):
Leo Gao's avatar
Leo Gao committed
41
        return True
sdtblck's avatar
sdtblck committed
42
43

    def has_test_docs(self):
Leo Gao's avatar
Leo Gao committed
44
        return False
sdtblck's avatar
sdtblck committed
45
46
47
48
49

    def training_docs(self):
        pass

    def validation_docs(self):
Jonathan Tow's avatar
Jonathan Tow committed
50
        return self.dataset["validation"]
Leo Gao's avatar
Leo Gao committed
51

Leo Gao's avatar
Leo Gao committed
52
53
54
    def test_docs(self):
        pass

Leo Gao's avatar
Leo Gao committed
55
    def doc_to_text(self, doc):
Fabrizio Milo's avatar
Fabrizio Milo committed
56
        return doc["text"].rsplit(" ", 1)[0]
sdtblck's avatar
sdtblck committed
57

58
59
60
61
    def should_decontaminate(self):
        return True

    def doc_to_decontamination_query(self, doc):
Fabrizio Milo's avatar
Fabrizio Milo committed
62
        return doc["text"]
63

Leo Gao's avatar
Leo Gao committed
64
    def doc_to_target(self, doc):
Fabrizio Milo's avatar
Fabrizio Milo committed
65
        return " " + doc["text"].rsplit(" ", 1)[1]
sdtblck's avatar
sdtblck committed
66

Leo Gao's avatar
Leo Gao committed
67
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
68
        ll, is_greedy = rf.loglikelihood(ctx, self.doc_to_target(doc))
69

Leo Gao's avatar
Leo Gao committed
70
        return ll, is_greedy
Fabrizio Milo's avatar
Fabrizio Milo committed
71

Leo Gao's avatar
Leo Gao committed
72
    def process_results(self, doc, results):
Leo Gao's avatar
Leo Gao committed
73
        ll, is_greedy = results
Leo Gao's avatar
Leo Gao committed
74

Fabrizio Milo's avatar
Fabrizio Milo committed
75
76
        return {"ppl": ll, "acc": int(is_greedy)}

Leo Gao's avatar
Leo Gao committed
77
    def aggregation(self):
Fabrizio Milo's avatar
Fabrizio Milo committed
78
        return {"ppl": perplexity, "acc": mean}
Leo Gao's avatar
Leo Gao committed
79
80

    def higher_is_better(self):
Fabrizio Milo's avatar
Fabrizio Milo committed
81
        return {"ppl": False, "acc": True}