-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Fever NLI task and data downloader (#1215)
* Implement task and add downloader for Fever NLI Co-authored-by: jeswan <57466294+jeswan@users.noreply.github.com>
- Loading branch information
1 parent
0cc8cbb
commit e7eefc6
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
} | ||
OTHER_DOWNLOAD_TASKS = { | ||
"abductive_nli", | ||
"fever_nli", | ||
"swag", | ||
"qamr", | ||
"qasrl", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import numpy as np | ||
import torch | ||
from dataclasses import dataclass | ||
from typing import List | ||
|
||
from jiant.tasks.core import ( | ||
BaseExample, | ||
BaseTokenizedExample, | ||
BaseDataRow, | ||
BatchMixin, | ||
Task, | ||
TaskTypes, | ||
) | ||
from jiant.tasks.lib.templates.shared import double_sentence_featurize, labels_to_bimap | ||
from jiant.utils.python.io import read_jsonl | ||
|
||
|
||
@dataclass | ||
class Example(BaseExample): | ||
guid: str | ||
premise: str | ||
hypothesis: str | ||
label: str | ||
|
||
def tokenize(self, tokenizer): | ||
return TokenizedExample( | ||
guid=self.guid, | ||
premise=tokenizer.tokenize(self.premise), | ||
hypothesis=tokenizer.tokenize(self.hypothesis), | ||
label_id=FeverNliTask.LABEL_TO_ID[self.label], | ||
) | ||
|
||
|
||
@dataclass | ||
class TokenizedExample(BaseTokenizedExample): | ||
guid: str | ||
premise: List | ||
hypothesis: List | ||
label_id: int | ||
|
||
def featurize(self, tokenizer, feat_spec): | ||
return double_sentence_featurize( | ||
guid=self.guid, | ||
input_tokens_a=self.premise, | ||
input_tokens_b=self.hypothesis, | ||
label_id=self.label_id, | ||
tokenizer=tokenizer, | ||
feat_spec=feat_spec, | ||
data_row_class=DataRow, | ||
) | ||
|
||
|
||
@dataclass | ||
class DataRow(BaseDataRow): | ||
guid: str | ||
input_ids: np.ndarray | ||
input_mask: np.ndarray | ||
segment_ids: np.ndarray | ||
label_id: int | ||
tokens: list | ||
|
||
|
||
@dataclass | ||
class Batch(BatchMixin): | ||
input_ids: torch.LongTensor | ||
input_mask: torch.LongTensor | ||
segment_ids: torch.LongTensor | ||
label_id: torch.LongTensor | ||
tokens: list | ||
|
||
|
||
class FeverNliTask(Task): | ||
Example = Example | ||
TokenizedExample = Example | ||
DataRow = DataRow | ||
Batch = Batch | ||
|
||
TASK_TYPE = TaskTypes.CLASSIFICATION | ||
LABELS = ["REFUTES", "SUPPORTS", "NOT ENOUGH INFO"] | ||
LABEL_TO_ID, ID_TO_LABEL = labels_to_bimap(LABELS) | ||
|
||
def get_train_examples(self): | ||
return self._create_examples(lines=read_jsonl(self.train_path), set_type="train") | ||
|
||
def get_val_examples(self): | ||
return self._create_examples(lines=read_jsonl(self.val_path), set_type="val") | ||
|
||
def get_test_examples(self): | ||
return self._create_examples(lines=read_jsonl(self.test_path), set_type="test") | ||
|
||
@classmethod | ||
def _create_examples(cls, lines, set_type): | ||
# noinspection DuplicatedCode | ||
examples = [] | ||
for (i, line) in enumerate(lines): | ||
examples.append( | ||
Example( | ||
guid="%s-%s" % (set_type, i), | ||
premise=line["context"], | ||
hypothesis=line["query"], | ||
label=line["label"] if set_type != "test" else cls.LABELS[-1], | ||
) | ||
) | ||
return examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters