-
Notifications
You must be signed in to change notification settings - Fork 8
/
dataset.py
96 lines (72 loc) · 2.67 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
import transformers
from torch.utils.data import Dataset
import json
import logging
# PROMPT_DICT = {
# "prompt_input": (
# "Below is an instruction that describes a task, paired with an input that provides further context. "
# "Write a response that appropriately completes the request.\n\n"
# "### Instruction:\n{instruction}\n\n### Input:\n{input}\n\n### Response:"
# ),
# "prompt_no_input": (
# "Below is an instruction that describes a task. "
# "Write a response that appropriately completes the request.\n\n"
# "### Instruction:\n{instruction}\n\n### Response:"
# ),
# }
PROMPT_DICT = {
"prompt_input": (
"{instruction}\n\n {input}\n\n"
),
"prompt_no_input": (
"{instruction}\n\n"
),
}
class Seq2SeqDataset(Dataset):
def __init__(self, data_paths):
super(Seq2SeqDataset, self).__init__()
list_data_dict = []
for data_path in data_paths:
logging.warning(f"Loading data from {data_path}...")
with open(data_path, "r") as f:
list_data_dict.extend(json.load(f))
prompt_input, prompt_no_input = PROMPT_DICT["prompt_input"], PROMPT_DICT["prompt_no_input"]
logging.warning("Formatting data...")
sources = [
prompt_input.format_map(example) if example.get("input", "") != "" else prompt_no_input.format_map(example)
for example in list_data_dict
]
targets = [f"{example['output']}" if 'output' in example else f"{example['response']}" for example in list_data_dict]
self.sources = sources
self.targets = targets
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=40, output_length=160):
self.tokenizer = tokenizer
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
if __name__=="__main__":
dataset = Seq2SeqDataset(["./alpaca_data.json"])
print(dataset[0])