sileod commited on
Commit
4a48898
1 Parent(s): 65d5cea

Create babi_nli.py

Browse files
Files changed (1) hide show
  1. babi_nli.py +211 -0
babi_nli.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # coding=utf-8
3
+ # Copyright 2020 The TensorFlow Datasets Authors and the HuggingFace Datasets Authors.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Lint as: python3
18
+ """bAbI_nli datasets"""
19
+
20
+ from __future__ import absolute_import, division, print_function
21
+
22
+ import csv
23
+ import os
24
+ import textwrap
25
+
26
+ import six
27
+
28
+ import datasets
29
+
30
+
31
+ bAbI_nli_CITATION = r"""@article{weston2015towards,
32
+ title={Towards ai-complete question answering: A set of prerequisite toy tasks},
33
+ author={Weston, Jason and Bordes, Antoine and Chopra, Sumit and Rush, Alexander M and Van Merri{\"e}nboer, Bart and Joulin, Armand and Mikolov, Tomas},
34
+ journal={arXiv preprint arXiv:1502.05698},
35
+ year={2015}
36
+ }
37
+
38
+ @inproceedings{sileo-moens-2022-analysis,
39
+ title = "Analysis and Prediction of {NLP} Models via Task Embeddings",
40
+ author = "Sileo, Damien and
41
+ Moens, Marie-Francine",
42
+ booktitle = "Proceedings of the Thirteenth Language Resources and Evaluation Conference",
43
+ month = jun,
44
+ year = "2022",
45
+ address = "Marseille, France",
46
+ publisher = "European Language Resources Association",
47
+ url = "https://aclanthology.org/2022.lrec-1.67",
48
+ pages = "633--647",
49
+ abstract = "Task embeddings are low-dimensional representations that are trained to capture task properties. In this paper, we propose MetaEval, a collection of 101 NLP tasks. We fit a single transformer to all MetaEval tasks jointly while conditioning it on learned embeddings. The resulting task embeddings enable a novel analysis of the space of tasks. We then show that task aspects can be mapped to task embeddings for new tasks without using any annotated examples. Predicted embeddings can modulate the encoder for zero-shot inference and outperform a zero-shot baseline on GLUE tasks. The provided multitask setup can function as a benchmark for future transfer learning research.",
50
+ }
51
+
52
+
53
+ """
54
+
55
+ _babi_nli_DESCRIPTION = """\
56
+ bAbi tasks recasted as natural language inference"""
57
+
58
+ DATA_URL = "https://www.dropbox.com/s/0b98tbrv2mej3cu/babi_nli.zip?dl=1"
59
+
60
+ LABELS=["not-entailed", "entailed"]
61
+
62
+ CONFIGS=['single-supporting-fact',
63
+ 'two-supporting-facts',
64
+ 'three-supporting-facts',
65
+ 'two-arg-relations',
66
+ 'three-arg-relations',
67
+ 'yes-no-questions',
68
+ 'counting',
69
+ 'lists-sets',
70
+ 'simple-negation',
71
+ 'indefinite-knowledge',
72
+ 'basic-coreference',
73
+ 'conjunction',
74
+ 'compound-coreference',
75
+ 'time-reasoning',
76
+ 'basic-deduction',
77
+ 'basic-induction',
78
+ 'positional-reasoning',
79
+ 'size-reasoning',
80
+ 'path-finding',
81
+ 'agents-motivations']
82
+
83
+ class bAbI_nli_Config(datasets.BuilderConfig):
84
+ """BuilderConfig for bAbI_nli."""
85
+
86
+ def __init__(
87
+ self,
88
+ text_features,
89
+ label_classes=None,
90
+ process_label=lambda x: x,
91
+ **kwargs,
92
+ ):
93
+ """BuilderConfig for bAbI_nli.
94
+ Args:
95
+ text_features: `dict[string, string]`, map from the name of the feature
96
+ dict for each text field to the name of the column in the tsv file
97
+ label_column: `string`, name of the column in the tsv file corresponding
98
+ to the label
99
+ data_url: `string`, url to download the zip file from
100
+ data_dir: `string`, the path to the folder containing the tsv files in the
101
+ downloaded zip
102
+ citation: `string`, citation for the data set
103
+ url: `string`, url for information about the data set
104
+ label_classes: `list[string]`, the list of classes if the label is
105
+ categorical. If not provided, then the label will be of type
106
+ `datasets.Value('float32')`.
107
+ process_label: `Function[string, any]`, function taking in the raw value
108
+ of the label and processing it to the form required by the label feature
109
+ **kwargs: keyword arguments forwarded to super.
110
+ """
111
+
112
+ super(bAbI_nli_Config, self).__init__(
113
+ version=datasets.Version("1.0.0", ""), **kwargs
114
+ )
115
+
116
+ self.text_features = text_features
117
+ self.label_column = "label"
118
+ self.label_classes = LABELS
119
+ self.data_url = DATA_URL
120
+ self.data_dir = "babi_nli"#os.path.join("babi_nli", self.name)
121
+ self.citation = textwrap.dedent(bAbI_nli_CITATION)
122
+ self.process_label = lambda x: str(x)
123
+ self.description = ""
124
+ self.url = ""
125
+
126
+
127
+ class bAbI_nli(datasets.GeneratorBasedBuilder):
128
+
129
+ """The General Language Understanding Evaluation (bAbI_nli) benchmark."""
130
+
131
+ BUILDER_CONFIG_CLASS = bAbI_nli_Config
132
+
133
+ BUILDER_CONFIGS = [
134
+ bAbI_nli_Config(
135
+ name=name,
136
+ text_features={"premise": "premise", "hypothesis": "hypothesis"},
137
+ ) for name in CONFIGS
138
+ ]
139
+
140
+ def _info(self):
141
+ features = {
142
+ text_feature: datasets.Value("string")
143
+ for text_feature in six.iterkeys(self.config.text_features)
144
+ }
145
+ if self.config.label_classes:
146
+ features["label"] = datasets.features.ClassLabel(
147
+ names=self.config.label_classes
148
+ )
149
+ else:
150
+ features["label"] = datasets.Value("float32")
151
+ features["idx"] = datasets.Value("int32")
152
+ return datasets.DatasetInfo(
153
+ description=_babi_nli_DESCRIPTION,
154
+ features=datasets.Features(features),
155
+ homepage=self.config.url,
156
+ citation=self.config.citation + "\n" + bAbI_nli_CITATION,
157
+ )
158
+
159
+ def _split_generators(self, dl_manager):
160
+ dl_dir = dl_manager.download_and_extract(self.config.data_url)
161
+ data_dir = os.path.join(dl_dir, self.config.data_dir)
162
+
163
+ return [
164
+ datasets.SplitGenerator(
165
+ name=datasets.Split.TRAIN,
166
+ gen_kwargs={
167
+ "data_file": os.path.join(data_dir or "", "train.tsv"),
168
+ "split": "train",
169
+ },
170
+ ),
171
+ datasets.SplitGenerator(
172
+ name=datasets.Split.VALIDATION,
173
+ gen_kwargs={
174
+ "data_file": os.path.join(data_dir or "", "validation.tsv"),
175
+ "split": "validation",
176
+ },
177
+ ),
178
+ datasets.SplitGenerator(
179
+ name=datasets.Split.TEST,
180
+ gen_kwargs={
181
+ "data_file": os.path.join(data_dir or "", "test.tsv"),
182
+ "split": "test",
183
+ },
184
+ ),
185
+ ]
186
+
187
+ def _generate_examples(self, data_file, split):
188
+
189
+ process_label = self.config.process_label
190
+ label_classes = self.config.label_classes
191
+
192
+ with open(data_file, encoding="utf8") as f:
193
+ reader = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
194
+
195
+ for n, row in enumerate(reader):
196
+
197
+ example = {
198
+ feat: row[col]
199
+ for feat, col in six.iteritems(self.config.text_features)
200
+ }
201
+ example["idx"] = n
202
+
203
+ if self.config.label_column in row:
204
+ label = row[self.config.label_column]
205
+ if label_classes and label not in label_classes:
206
+ label = int(label) if label else None
207
+ example["label"] = process_label(label)
208
+ else:
209
+ example["label"] = process_label(-1)
210
+ yield example["idx"], example
211
+