-
Notifications
You must be signed in to change notification settings - Fork 6
/
dataset.py
223 lines (184 loc) · 7.58 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
from typing import List
import torch
import numpy as np
import pandas as pd
from transformers import PreTrainedTokenizer
from torch.utils.data import Dataset, DataLoader
import pytorch_lightning as pl
import pickle
from Tokenizers import NaiveTokenizer
import os
class TextOnlyDataset(Dataset):
""" PyTorch Dataset class """
def __init__(
self,
data: pd.DataFrame,
tokenizer: PreTrainedTokenizer,
source_max_token_len: int = 512,
target_max_token_len: int = 512,
pad_to_the_longest: bool = False
):
"""
initiates a PyTorch Dataset Module for input data
Args:
data (pd.DataFrame): input pandas dataframe. Dataframe must have 2 column --> "source_text" and "target_text"
tokenizer (PreTrainedTokenizer): a PreTrainedTokenizer (T5Tokenizer, MT5Tokenizer, or ByT5Tokenizer)
source_max_token_len (int, optional): max token length of source text. Defaults to 512.
target_max_token_len (int, optional): max token length of target text. Defaults to 512.
"""
self.tokenizer = tokenizer
self.data = data
self.source_max_token_len = source_max_token_len
self.target_max_token_len = target_max_token_len
if pad_to_the_longest:
self.kwargs = dict(padding='longest')
else:
self.kwargs = dict(
padding='max_length',
truncation=True,
)
def __len__(self):
""" returns length of data """
return len(self.data)
def __getitem__(self, index: int):
""" returns dictionary of input tensors to feed into T5/MT5 model"""
data_row = self.data.iloc[index]
key = data_row["key"]
source_text = data_row["source_text"]
source_text_encoding = self.tokenizer(
source_text,
return_attention_mask=True,
add_special_tokens=False,
return_tensors="pt",
max_length=self.source_max_token_len,
**self.kwargs
)
target_text_encoding = self.tokenizer(
data_row["target_text"],
return_attention_mask=True,
add_special_tokens=True,
return_tensors="pt",
max_length=self.target_max_token_len,
**self.kwargs
)
labels = target_text_encoding["input_ids"]
labels[
labels == 0
] = -100 # to make sure we have correct labels for T5 text generation
return dict(
source_text=source_text,
target_text=data_row["target_text"],
source_text_input_ids=source_text_encoding["input_ids"].flatten(),
source_text_attention_mask=source_text_encoding["attention_mask"].flatten(),
labels=labels.flatten(),
labels_attention_mask=target_text_encoding["attention_mask"].flatten(),
key=key,
)
class EmbsLoader(object):
def __init__(
self,
relevant_info_paths: List[str],
embs_path: str,
topk: int,
dim: int,
concat: float=False,
):
self.relevant_info = [pickle.load(open(path, 'rb')) for path in relevant_info_paths]
self.embs = pickle.load(open(embs_path, 'rb'))
self.topk = topk
self.dim = dim
self.concat = concat
def get_embs_based_on_the_key(self, key):
relevant_embs_list = []
for info in self.relevant_info:
this_embs = np.zeros((self.topk, self.dim))
if key in info:
relevant_list = info[key]
relevant_list = relevant_list[:self.topk]
for i, relevant_key in enumerate(relevant_list):
this_embs[i, :] = self.embs[relevant_key]
relevant_embs_list.append(torch.FloatTensor(this_embs))
if self.concat:
relevant_embs_list = [torch.cat(relevant_embs_list, dim=0)] # [(topk * n_info, dim)]
return dict(relevant_embs_list=relevant_embs_list)
class Dataset(TextOnlyDataset):
def __init__(self, args, tokenizer, type: str = 'joint', mode: str = 'train'):
assert type in ['joint', 'text'], type
assert mode in ['train', 'val', 'test']
self.type = type
self.mode = mode
data = load_csv(getattr(args, f'{mode}_csv_path'))
if hasattr(args, 'subtask_file'):
print('- Loading the subtask file from', args.subtask_file)
keys = set(open(args.subtask_file, 'r').read().strip().split('\n'))
frames = []
for i in range(len(data)):
if data.iloc[i]['key'] in keys:
frames.append(data.iloc[i:i+1])
data = pd.concat(frames, ignore_index=True)
print(mode, data.shape)
TextOnlyDataset.__init__(
self,
data=data,
tokenizer=tokenizer,
source_max_token_len=args.source_max_token_len,
target_max_token_len=args.target_max_token_len,
pad_to_the_longest=getattr(args, 'pad_to_the_longest', False)
)
if self.type == 'joint':
self.embs_loader = EmbsLoader(
relevant_info_paths=args.relevant_info_paths,
embs_path=args.embs_path,
topk=args.relevant_topk,
dim=768 * int(args.embs_path.split('_')[-2]),
concat=getattr(args, 'relevant_concat', False),
)
def __getitem__(self, index: int):
item = super().__getitem__(index)
if self.type == 'joint':
item.update(self.embs_loader.get_embs_based_on_the_key(item['key']))
return item
class LightningDataModule(pl.LightningDataModule):
""" PyTorch Lightning data class """
def __init__(self, args, tokenizer, mode=None):
super().__init__()
self.args = args
self.tokenizer = tokenizer
self.num_workers = args.num_workers
self.mode = mode
def setup(self, stage=None):
this_type = 'joint' if getattr(self.args, 'use_prior_experience', False) else 'text'
if self.mode is None or self.mode == 'train':
self.train_dataset = Dataset(self.args, self.tokenizer, mode='train', type=this_type)
if self.mode is None or self.mode == 'val':
self.val_dataset = Dataset(self.args, self.tokenizer, mode='val', type=this_type)
if self.mode is None or self.mode == 'test':
self.test_dataset = Dataset(self.args, self.tokenizer, mode='test', type=this_type)
def train_dataloader(self):
""" training dataloader """
return DataLoader(
self.train_dataset, batch_size=self.args.batch_size, shuffle=True,
num_workers=self.args.num_workers, persistent_workers=True
)
def test_dataloader(self):
""" test dataloader """
return DataLoader(
self.test_dataset, batch_size=self.args.batch_size, shuffle=False,
num_workers=self.args.num_workers, persistent_workers=True
)
def val_dataloader(self):
""" validation dataloader """
return DataLoader(
self.val_dataset, batch_size=self.args.batch_size, shuffle=False,
num_workers=self.args.num_workers, persistent_workers=True
)
def load_csv(
path,
columns={"discharge_instruction": "target_text", "discharge_summary": "source_text"},
extract_columns=['key', 'source_text', 'target_text'],
):
import pandas as pd
df = pd.read_csv(path)
df = df.rename(columns=columns)
df = df[extract_columns]
return df