-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
51 lines (41 loc) · 1.83 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
import numpy as np
import pandas as pd
from torch.utils.data import Dataset
class KoBARTQGDataset(Dataset):
def __init__(self, file, tokenizer, max_len = 512, ignore_index=-100):
super().__init__()
self.tokenizer = tokenizer
self.max_len = max_len
self.docs = pd.read_csv(file, sep='\t', encoding='cp949')
self.len = self.docs.shape[0]
self.pad_index = self.tokenizer.pad_token_id
self.ignore_index = ignore_index
def add_padding_data(self, inputs):
if len(inputs) < self.max_len:
pad = np.array([self.pad_index] * (self.max_len - len(inputs)))
inputs = np.concatenate([inputs, pad])
else:
inputs = inputs[:self.max_len]
return inputs
def add_ignored_data(self, inputs):
if len(inputs) < self.max_len:
pad = np.array([self.ignore_index] * (self.max_len - len(inputs)))
inputs = np.concatenate([inputs, pad])
else:
inputs = inputs[:self.max_len]
return inputs
def __getitem__(self, idx):
instance = self.docs.iloc[idx]
input_ids = self.tokenizer.encode(instance['content'])
input_ids = self.add_padding_data(input_ids)
label_ids = self.tokenizer.encode(instance['question'])
label_ids.append(self.tokenizer.eos_token_id)
dec_input_ids = [self.tokenizer.eos_token_id]
dec_input_ids += label_ids[:-1]
dec_input_ids = self.add_padding_data(dec_input_ids)
label_ids = self.add_ignored_data(label_ids)
return {'input_ids': np.array(input_ids, dtype=np.int_),
'decoder_input_ids': np.array(dec_input_ids, dtype=np.int_),
'labels': np.array(label_ids, dtype=np.int_)}
def __len__(self):
return self.len