lambada.py 2.27 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
23
24
25
26
27
28
29
30
31
32
_CITATION = """
@misc{
    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}, 
    title={The LAMBADA dataset},
    DOI={10.5281/zenodo.2630551},
    publisher={Zenodo},
    year={2016},
    month={Aug}
}
"""


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
56
    def doc_to_text(self, doc):
        return doc['text'].rsplit(' ', 1)[0]
sdtblck's avatar
sdtblck committed
57

Leo Gao's avatar
Leo Gao committed
58
59
    def doc_to_target(self, doc):
        return " " + doc['text'].rsplit(' ', 1)[1]
sdtblck's avatar
sdtblck committed
60

Leo Gao's avatar
Leo Gao committed
61
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
62
        ll, is_greedy = rf.loglikelihood(ctx, self.doc_to_target(doc))
63

Leo Gao's avatar
Leo Gao committed
64
        return ll, is_greedy
Leo Gao's avatar
Leo Gao committed
65
66
    
    def process_results(self, doc, results):
Leo Gao's avatar
Leo Gao committed
67
        ll, is_greedy = results
Leo Gao's avatar
Leo Gao committed
68

Leo Gao's avatar
Leo Gao committed
69
        return {
Leo Gao's avatar
Leo Gao committed
70
71
            'ppl': ll,
            'acc': int(is_greedy)
Leo Gao's avatar
Leo Gao committed
72
73
        }
        
Leo Gao's avatar
Leo Gao committed
74
    def aggregation(self):
Leo Gao's avatar
Leo Gao committed
75
        return {
Leo Gao's avatar
Leo Gao committed
76
77
            'ppl': perplexity,
            'acc': mean
Leo Gao's avatar
Leo Gao committed
78
        }
Leo Gao's avatar
Leo Gao committed
79
80

    def higher_is_better(self):
Leo Gao's avatar
Leo Gao committed
81
        return {
Leo Gao's avatar
Leo Gao committed
82
83
            'ppl': False,
            'acc': True
Leo Gao's avatar
Leo Gao committed
84
        }