-
Notifications
You must be signed in to change notification settings - Fork 36
/
dataset.py
49 lines (36 loc) · 1.16 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
from typing import List, Dict
from torch.utils.data import Dataset
from utils import Vocab
class SeqClsDataset(Dataset):
def __init__(
self,
data: List[Dict],
vocab: Vocab,
label_mapping: Dict[str, int],
max_len: int,
):
self.data = data
self.vocab = vocab
self.label_mapping = label_mapping
self._idx2label = {idx: intent for intent, idx in self.label_mapping.items()}
self.max_len = max_len
def __len__(self) -> int:
return len(self.data)
def __getitem__(self, index) -> Dict:
instance = self.data[index]
return instance
@property
def num_classes(self) -> int:
return len(self.label_mapping)
def collate_fn(self, samples: List[Dict]) -> Dict:
# TODO: implement collate_fn
raise NotImplementedError
def label2idx(self, label: str):
return self.label_mapping[label]
def idx2label(self, idx: int):
return self._idx2label[idx]
class SeqTaggingClsDataset(SeqClsDataset):
ignore_idx = -100
def collate_fn(self, samples):
# TODO: implement collate_fn
raise NotImplementedError