-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern_verbalizer.py
executable file
·176 lines (146 loc) · 6.33 KB
/
pattern_verbalizer.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
from collections.abc import Mapping
from dataclasses import dataclass
from typing import List, Optional, Dict, Any, Union
import torch
from transformers import PreTrainedTokenizerBase
from transformers.data.data_collator import DataCollatorMixin, _torch_collate_batch
ANSWER_TOKEN = "[ANSWER]"
@dataclass
class DataCollatorForClozeTask(DataCollatorMixin):
tokenizer: PreTrainedTokenizerBase
pad_to_multiple_of: Optional[int] = None
def __call__(self, examples: List[Union[List[int], Any, Dict[str, Any]]]) -> Dict[str, Any]:
# Handle dict or lists with proper padding and conversion to tensor.
if isinstance(examples[0], Mapping):
batch = self.tokenizer.pad(examples, return_tensors="pt", pad_to_multiple_of=self.pad_to_multiple_of)
else:
batch = {
"input_ids": _torch_collate_batch(examples, self.tokenizer, pad_to_multiple_of=self.pad_to_multiple_of)
}
# If special token mask has been preprocessed, pop it from the dict.
special_tokens_mask = batch.pop("special_tokens_mask", None)
labels = torch.empty_like(batch["input_ids"]).fill_(-100)
answer_token_id = self.tokenizer.convert_tokens_to_ids(ANSWER_TOKEN)
index = (torch.tensor(batch["input_ids"]) == answer_token_id).nonzero()
batch["input_ids"][range(len(labels)), index[:, 1]] = \
self.tokenizer.convert_tokens_to_ids(self.tokenizer.mask_token)
labels[range(len(labels)), index[:, 1]] = batch.pop("label")
batch["labels"] = labels
return batch
def rte_pv_fn(sent1s, sent2s, labels=None):
sent1_format = "{}?"
sent2_format = "{}, {}"
formatted_sent1s, formatted_sent2s = [], []
verbalizer = {
0: "No",
1: "Yes",
-1: "dummy", # test data
}
formatted_labels = []
for sent1, sent2, label in zip(sent1s, sent2s, labels):
assert ANSWER_TOKEN not in sent1 and ANSWER_TOKEN not in sent2
formatted_sent1s.append(sent1_format.format(sent1))
formatted_sent2s.append(sent2_format.format(ANSWER_TOKEN, sent2))
formatted_labels.append(verbalizer[label])
return formatted_sent1s, formatted_sent2s, formatted_labels
def sst2_pv_fn(sent1s, sent2s=None, labels=None):
sent1_format = "It was {}. {}"
formatted_sent1s = []
verbalizer = {
0: "bad",
1: "good",
-1: "dummy", # test data
}
formatted_labels = []
for sent1, label in zip(sent1s, labels):
assert ANSWER_TOKEN not in sent1
formatted_sent1s.append(sent1_format.format(ANSWER_TOKEN, sent1))
formatted_labels.append(verbalizer[label])
return formatted_sent1s, None, formatted_labels
def cola_pv_fn(sent1s, sent2s=None, labels=None):
sent1_format = "Is the sentence a grammatical sentence? {}. Sentence: {}"
formatted_sent1s = []
verbalizer = {
0: "No",
1: "Yes",
-1: "dummy", # test data
}
formatted_labels = []
for sent1, label in zip(sent1s, labels):
assert ANSWER_TOKEN not in sent1
formatted_sent1s.append(sent1_format.format(ANSWER_TOKEN, sent1))
formatted_labels.append(verbalizer[label])
return formatted_sent1s, None, formatted_labels
def qqp_pv_fn(sent1s, sent2s, labels=None):
sent1_format = "Are the following sentences paraphrases? {}. {}"
sent2_format = "{}"
formatted_sent1s, formatted_sent2s = [], []
verbalizer = {
0: "No",
1: "Yes",
-1: "dummy", # test data
}
formatted_labels = []
for sent1, sent2, label in zip(sent1s, sent2s, labels):
assert ANSWER_TOKEN not in sent1 and ANSWER_TOKEN not in sent2
formatted_sent1s.append(sent1_format.format(ANSWER_TOKEN, sent1))
formatted_sent2s.append(sent2_format.format(sent2))
formatted_labels.append(verbalizer[label])
return formatted_sent1s, formatted_sent2s, formatted_labels
def qnli_pv_fn(sent1s, sent2s, labels=None):
sent1_format = "Does the context contain answer to the question? {}. Question: {}"
sent2_format = "Context: {}"
formatted_sent1s, formatted_sent2s = [], []
verbalizer = {
0: "No",
1: "Yes",
-1: "dummy", # test data
}
verbalizer_for_newsqa = {
"not_entailment": "No",
"entailment": "Yes",
}
formatted_labels = []
for sent1, sent2, label in zip(sent1s, sent2s, labels):
assert ANSWER_TOKEN not in sent1 and ANSWER_TOKEN not in sent2
formatted_sent1s.append(sent1_format.format(ANSWER_TOKEN, sent1))
formatted_sent2s.append(sent2_format.format(sent2))
try:
formatted_labels.append(verbalizer[label])
except:
formatted_labels.append(verbalizer_for_newsqa[label])
return formatted_sent1s, formatted_sent2s, formatted_labels
def mnli_pv_fn_1(sent1s, sent2s, labels=None):
sent1_format = "{}?"
sent2_format = "{}, {}"
formatted_sent1s, formatted_sent2s = [], []
verbalizer = {
0: "Yes",
1: "?",
2: "No",
-1: "dummy", # test data
}
formatted_labels = []
for sent1, sent2, label in zip(sent1s, sent2s, labels):
assert ANSWER_TOKEN not in sent1 and ANSWER_TOKEN not in sent2
formatted_sent1s.append(sent1_format.format(sent1))
formatted_sent2s.append(sent2_format.format(ANSWER_TOKEN, sent2))
formatted_labels.append(verbalizer[label])
return formatted_sent1s, formatted_sent2s, formatted_labels
def mnli_pv_fn_2(sent1s, sent2s, labels=None):
sent1_format = "Does the premise entail the hypothesis? {}. Premise: {}"
sent2_format = "Hypothesis: {}"
formatted_sent1s, formatted_sent2s = [], []
verbalizer = {
0: "Yes",
1: "Neutral", # FIXME: doesn't really make sense
2: "No",
-1: "dummy", # test data
}
formatted_labels = []
for sent1, sent2, label in zip(sent1s, sent2s, labels):
assert ANSWER_TOKEN not in sent1 and ANSWER_TOKEN not in sent2
formatted_sent1s.append(sent1_format.format(ANSWER_TOKEN, sent1))
formatted_sent2s.append(sent2_format.format(sent2))
formatted_labels.append(verbalizer[label])
return formatted_sent1s, formatted_sent2s, formatted_labels