-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdataset.py
492 lines (426 loc) · 15 KB
/
dataset.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
from dataclasses import dataclass, field
from typing import Iterable, Optional, Mapping, Sequence, Tuple, List, Dict, Set
import abc
import math
import json
import collections
import random
import logging
import itertools
import importlib
import torch
import datasets
from utils import set_seed
DATASETS = ["agnews", "atis-intent", "cola", "cr", "dbpedia",
"dbpedia-content", "imdb", "mpqa", "mr", "snips-intent", "sst2",
"sst5", "subj", "trec6", "yahoo-answers", "yelp", "rt20"]
DATASET_VALID_SIZE = {
"agnews": 2000,
"dbpedia-content": 2000,
"yahoo-answers": 5000,
"imdb": 2000
}
DATASET_METATYPES = {
"sst2": {
"text_type": "movie review",
"label_type": "sentiment"
},
"agnews": {
"text_type": "news headline",
"label_type": "classification"
},
"dbpedia-content": {
"text_type": "description",
"label_type": "classification"
},
"yahoo-answers": {
"text_type": "question-answer pair",
"label_type": "question type"
},
"imdb": {
"text_type": "movie review",
"label_type": "sentiment"
},
"cola": {
"text_type": "text",
"label_type": "grammar"
},
"snips-intent": {
"text_type": "user request",
"label_type": "intent type"
},
"cr": {
"text_type": "customer review",
"label_type": "sentiment"
},
"atis-intent": {
"text_type": "user request",
"label_type": "intent type"
},
"yelp": {
"text_type": "place review",
"label_type": "sentiment"
},
"subj": {
"text_type": "text",
"label_type": "objective"
},
"trec6": {
"text_type": "question",
"label_type": "type"
},
"mpqa": {
"text_type": "phrase",
"label_type": "sentiment"
},
"rt20": {
"text_type": "movie review",
"label_type": "sentiment"
}
}
DATASET_LABEL_MAP = {
# Note: verbalizing label must correspond to single
# labels in GPT-3 vocabulary
"agnews": {
"Sci/Tech": "technology",
"World": "world",
"Sports": "sports",
"Business": "business"
},
"imdb": {
"pos": "positive",
"neg": "negative"
},
"cola": {
"0": "incorrect",
"1": "correct"
},
"snips-intent": {
"RateBook": "rate",
"BookRestaurant": "book",
"GetWeather": "weather",
"SearchCreativeWork": "creative",
"SearchScreeningEvent": "screening",
"PlayMusic": "music",
"AddToPlaylist": "playlist"
},
"cr": {
"pos": "positive",
"neg": "negative"
},
"atis-intent": {
"atis_flight": "flight",
"atis_quantity": "quantity",
"atis_city": "city",
"atis_ground_service#atis_ground_fare": "ground",
"atis_ground_service": "ground",
"atis_abbreviation": "abbreviation",
"atis_airfare": "cost",
"atis_flight#atis_airfare": "flight",
"atis_flight_time": "time",
},
"yelp": {
"positive": "positive",
"neutral": "neutral",
"negative": "negative",
"very positive": "positive",
"very negative": "negative"
},
"trec6": {
"ABBR": "abbreviation",
"LOC": "location",
"DESC": "description",
"NUM": "numeric",
"ENTY": "entity",
"HUM": "human"
},
"mpqa": {
"pos": "positive",
"neg": "negative"
},
"subj": {
"objective": "yes",
"subjective": "no"
},
"rt20": {
"positive": "positive",
"negative": "negative"
}
}
@dataclass
class Example:
text: str
label: str
probs: Optional[Mapping[str, float]] = None
info: Optional[Mapping[str, str]] = None
@property
def is_soft(self):
return self.probs is not None
def to_dict(self):
ret = {
"text": self.text,
"label": self.label
}
if self.probs is not None:
ret["probs"] = self.probs
if self.info is not None:
ret["info"] = self.info
return ret
@classmethod
def from_dict(cls, data):
return Example(
text=data["text"],
label=data["label"],
probs=data.get("probs"),
info=data.get("info")
)
def __str__(self):
if self.probs is None:
label_str = f"({self.label})"
else:
probs_str = " / ".join(itertools.starmap("{}: {:.1%}".format,
self.probs.items()))
label_str = "(" + probs_str + ")"
return f"{self.text} {label_str}"
class Dataset(abc.ABC, Sequence):
@abc.abstractmethod
def get(self, idx):
raise NotImplementedError
def get_multi(self, idxs):
return list(map(self.get, idxs))
@abc.abstractmethod
def __len__(self):
raise NotImplementedError
def __getitem__(self, item):
if isinstance(item, int):
return self.get(item)
elif isinstance(item, Sequence):
return self.get_multi(item)
else:
raise TypeError(f"unsupported index type: {type(item)}")
@dataclass
class Augmentation:
fake: Example
real: Optional[Sequence[Example]] = None
def to_dict(self):
return {
"real": [ex.to_dict() for ex in self.real] if self.real else None,
"fake": self.fake.to_dict()
}
@classmethod
def from_dict(cls, data):
return cls(
real=(list(map(Example.from_dict, data["real"]))
if "real" in data else None),
fake=Example.from_dict(data["fake"])
)
@dataclass
class AugmentedDataset(Dataset):
data: Sequence[Example]
augmenter: "Augmenter"
multiplier: int = 1
reuse: bool = False
save_path: str = None # jsonlines
num_classes: int = 5
num_examples: int = 2
sampling_strategy: str = "uniform"
_class_map: Mapping[str, list] = field(init=False, hash=False, repr=False)
_cache: List[Optional[Augmentation]] = \
field(init=False, hash=False, repr=False) # For caching augmentations
_cache_filled: int = 0
_pairwise_score: Dict[Tuple[int, int], float] = \
field(init=False, hash=False, repr=False)
def __post_init__(self):
self._cache = [None] * (len(self.data) * self.multiplier)
if self.sampling_strategy == "uniform":
self._sample_examples = self._sample_examples_uniform
elif self.sampling_strategy == "class-balanced":
self._sample_examples = self._sample_examples_balanced
elif self.sampling_strategy in {"furthest", "closest"}:
self._sample_examples = self._sample_examples_score
self._pairwise_score = dict()
scorer = importlib.import_module("bleurt.score").BleurtScorer()
for i, d1 in enumerate(self.data):
for j, d2 in enumerate(self.data):
score = scorer.score(references=[d1.text],
candidates=[d2.text])[0]
if self.sampling_strategy == "furthest":
score = -score
self._pairwise_score[(i, j)] = score
self._pairwise_score = \
{k: math.exp(v) for k, v in self._pairwise_score.items()}
total_score = sum(self._pairwise_score.values())
self._pairwise_score = \
{k: v / total_score for k, v in self._pairwise_score.items()}
else:
raise ValueError(f"unsupported strategy: {self.sampling_strategy}")
self._class_map = collections.defaultdict(list)
for ex in self.data:
self._class_map[ex.label].append(ex)
def _save_augs(self, augs: Sequence[Augmentation]):
if self.save_path is None:
return
with open(self.save_path, "a") as f:
for aug in augs:
f.write(json.dumps(aug.to_dict(), ensure_ascii=False) + "\n")
def _sample_examples_wrapper(self):
if not self.num_examples:
return []
if self.num_examples == len(self.data) and \
self.sampling_strategy != "paraphrase":
return self.data
if self.num_examples > len(self.data):
logging.warning(f"number of examples needed exceeds "
f"the data size:"
f" {self.num_examples} > {len(self.data)}")
return self._sample_examples(min(len(self.data), self.num_examples))
def _sample_examples_score(self, num_examples):
if num_examples == 1:
return random.choice(self.data)
anchor_idx = random.choice(range(len(self.data)))
anchor = self.data[anchor_idx]
pairs, probs = \
zip(*(((anchor, d), self._pairwise_score[(anchor_idx, d)])
for d in range(len(self.data)) if anchor_idx != d))
sampled_pairs = random.choices(pairs, weights=probs, k=num_examples)
return [anchor] + [self.data[b] for a, b in sampled_pairs]
def _sample_examples_uniform(self, num_examples):
return random.sample(self.data, num_examples)
def _sample_examples_balanced(self, num_examples):
num_classes = len(self._class_map)
if num_examples % num_classes:
raise ValueError(f"num_examples ({num_examples}) "
f"is not a multiple of number of "
f"classes ({num_classes})")
num_examples_per_class = num_examples // num_classes
ret = []
for cls, class_data in self._class_map.items():
if num_examples_per_class > len(class_data):
raise RuntimeError(
f"number of examples per class ({num_examples_per_class}) "
f"exceeds the available data in the class "
f"({cls}: {len(class_data)})")
ret.extend(random.sample(class_data, num_examples_per_class))
random.shuffle(ret)
return ret
def _augment(self, *examples):
augs = self.augmenter(*examples)
self._save_augs(augs)
return augs
def get(self, idx):
return self.get_multi([idx])[0]
def get_multi(self, idxs):
real_idxs = list(filter(lambda x: x < len(self.data), idxs))
aug_idxs = list(filter(lambda x: x >= len(self.data), idxs))
reals = list(map(self.data.__getitem__, real_idxs))
if not self.reuse:
augs = self._augment(*(self._sample_examples_wrapper()
for _ in aug_idxs))
else:
req_idxs = [idx for idx in aug_idxs
if self._cache[idx - len(self.data)] is None]
req_augs = self._augment(*(self._sample_examples_wrapper()
for _ in req_idxs))
for idx, aug in zip(req_idxs, req_augs):
if aug is not None:
self._cache_filled += 1
self._cache[idx - len(self.data)] = aug
augs = [self._cache[idx - len(self.data)] for idx in aug_idxs]
ret = []
for idx in idxs:
ret.append(reals.pop(0) if idx < len(self.data)
else augs.pop(0).fake)
if self.reuse and self.fill_rate < 1.0:
logging.info(f"{self.augmenter.__class__.__name__} "
f"current fill rate: {self.fill_rate * 100:.5f}%")
return ret
def __len__(self):
return len(self.data) * (self.multiplier + 1)
@property
def fill_rate(self):
"""Returns the ratio of augmentation examples filled in. Only
valid when the flag for reusing examples (`reuse`) is turned on.
When `reuse` is off, each augmentation example will be generated
on the fly, thus fill rate doesn't make sense in this case."""
if not self.reuse:
raise RuntimeError("fill rate cannot be reported for `reuse`=False")
return self._cache_filled / len(self._cache)
@dataclass
class SequenceDataset(Dataset):
data: Sequence
def get(self, idx):
return self.data[idx]
def __len__(self):
return len(self.data)
@dataclass
class DataSplits:
train: Dataset
valid: Dataset
test: Dataset
def to_dict(self):
return {
"train": self.train,
"valid": self.valid,
"test": self.test
}
def create_dataloader(data, **kwargs):
return torch.utils.data.DataLoader(
data, collate_fn=lambda x: x, **kwargs
)
def load_data(name: str, default_valid_size: int = 4000, valid_seed=42,
label_map: Mapping = None
) -> Tuple[Dict[str, List[Example]], Set[str], dict]:
datadict = datasets.load_dataset(name)
metatype = DATASET_METATYPES \
.get(name, {"text_type": "text", "label_type": "label"})
label_map = label_map or DATASET_LABEL_MAP.get(name)
dataset = {k: transform(v, label_map) for k, v in datadict.items()}
labels = set(datadict["train"].features["label"].names)
if label_map is not None:
labels = {label_map[label] for label in labels}
if "validation" not in datadict.keys():
logging.info(f"{name} dataset contains no "
f"pre-split validation set")
num_classes = datadict["train"].features["label"].num_classes
valid_size = DATASET_VALID_SIZE.get(name, default_valid_size)
samples_per_class = round(valid_size / num_classes)
set_seed(valid_seed, "sampling validation set")
logging.info(f"sampling {samples_per_class} per class "
f"(total {num_classes} classes) for "
f"the validation set")
valid, train = balanced_samples(dataset["train"], samples_per_class)
dataset["train"] = train
dataset["validation"] = valid
return dataset, labels, metatype
DataSequence = Sequence[Example]
def balanced_samples(data: DataSequence, size: int) -> Sequence[DataSequence]:
"""
Arguments:
data: sequence of examples
size: list of target number of samples per class
Returns:
list of samples and the rest
"""
cache = collections.defaultdict(list)
for d in data:
cache[d.label].append(d)
for l in cache.values():
random.shuffle(l)
sample_set = list()
for label, examples in cache.items():
if len(examples) < size:
logging.warning(f"number of target samples ({size}) more than "
f"the number of examples ({len(examples)}) "
f"in the class ({label})")
for _ in range(min(size, len(examples))):
sample_set.append(examples.pop())
return sample_set, sum(cache.values(), [])
def transform(dataset: datasets.Dataset, label_map=None) -> List[Example]:
int2str = dataset.features["label"].int2str
if label_map is not None:
def create_label(x):
return label_map[int2str(x)]
else:
create_label = int2str
return [Example(d['text'].replace("…", "..."), create_label(d['label']))
for d in dataset]