-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
80 lines (57 loc) · 1.71 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
import transformers
from torch.utils.data import Dataset
import json
import logging
import torch
from tqdm import tqdm
import pdb
PROMPT_DICT = {
"prompt_input": (
"{instruction}\n\n {input}\n\n"
),
"prompt_no_input": (
"{instruction}\n\n"
),
}
class Seq2SeqDataset(Dataset):
def __init__(self, dataset):
super(Seq2SeqDataset, self).__init__()
sources = []
targets = []
for data in tqdm(dataset):
words = data['text'].split()
sources.append(" ".join(words[:-20]))
targets.append(" ".join(words[-20:]))
print(sources[0])
print(targets[0])
self.sources = sources[:10000]
self.targets = targets[:10000]
def __len__(self):
return len(self.sources)
def __getitem__(self, item):
return self.sources[item], self.targets[item]
class Seq2SeqCollator(object):
def __init__(self, tokenizer, intruction_length=160, output_length=40):
self.tokenizer = tokenizer
self.tokenizer.pad_token_id = 0
self.intruction_length = intruction_length
self.output_length = output_length
def __call__(self, batch):
sources = [ex[0] for ex in batch]
targets = [ex[1] for ex in batch]
inputs = self.tokenizer(
sources,
max_length=self.intruction_length,
return_tensors='pt',
padding=True,
truncation=True
)
labels = self.tokenizer(
targets,
max_length=self.output_length,
return_tensors='pt',
padding=True,
truncation=True
).input_ids
inputs['labels'] = labels
return inputs