-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfeatures_csv_reader.py
213 lines (167 loc) · 9.43 KB
/
features_csv_reader.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
# This code is for data reading
import os
import logging
import torch
import numpy as np
import linecache
from torch.utils.data import DataLoader, Dataset
from pathlib import Path
from tqdm import tqdm
logger = logging.getLogger(__name__)
class PregeneratedDataset(Dataset):
def __init__(self, data_path, cache_path, set_type, max_seq_length, num_examples):
logger.info('data_path: {}'.format(data_path))
self.seq_len = max_seq_length
self.set_type = set_type
self.num_samples = num_examples
self.working_dir = Path(cache_path)
input_ids = np.memmap(filename=self.working_dir/'input_ids.memmap',
mode='w+', dtype=np.int32, shape=(self.num_samples, self.seq_len))
input_masks = np.memmap(filename=self.working_dir/'input_masks.memmap',
shape=(self.num_samples, self.seq_len), mode='w+', dtype=np.int32)
segment_ids = np.memmap(filename=self.working_dir/'segment_ids.memmap',
shape=(self.num_samples, self.seq_len), mode='w+', dtype=np.int32)
if self.set_type != 'eval':
label_ids = np.memmap(filename=self.working_dir/'label_ids.memmap',
shape=(self.num_samples, ), mode='w+', dtype=np.int32)
label_ids[:] = -1
else:
label_ids = None
logging.info("Loading examples.")
with open(data_path, 'r') as f:
for i, line in enumerate(tqdm(f, total=self.num_samples, desc="Examples")):
tokens = line.strip().split(',')
guid = tokens[0]
input_ids[i] = [int(id) for id in tokens[1].split()]
input_masks[i] = [int(id) for id in tokens[2].split()]
segment_ids[i] = [int(id) for id in tokens[3].split()]
if self.set_type != 'eval':
label_ids[i] = int(tokens[4])
if label_ids[i] != 0 and label_ids[i] != 1:
print(i)
raise KeyError
if i < 1:
logger.info("*** Example ***")
logger.info("guid: %s" % guid)
logger.info("input_ids: %s" % " ".join([str(x) for x in input_ids[i]]))
logger.info("input_masks: %s" % " ".join([str(x) for x in input_masks[i]]))
logger.info("segment_ids: %s" % " ".join([str(x) for x in segment_ids[i]]))
if self.set_type != 'eval':
logger.info("label: %s" % str(label_ids[i]))
logging.info("Loading complete!")
self.input_ids = input_ids
self.input_masks = input_masks
self.segment_ids = segment_ids
if self.set_type != 'eval':
self.label_ids = label_ids
def __len__(self):
return self.num_samples
def __getitem__(self, item):
if self.set_type != 'eval':
return (torch.tensor(self.input_ids[item], dtype=torch.long),
torch.tensor(self.input_masks[item], dtype=torch.long),
torch.tensor(self.segment_ids[item], dtype=torch.long),
torch.tensor(self.label_ids[item], dtype=torch.long))
else:
return (torch.tensor(self.input_ids[item], dtype=torch.long),
torch.tensor(self.input_masks[item], dtype=torch.long),
torch.tensor(self.segment_ids[item], dtype=torch.long))
def eval_dataloader(args, sampler, batch_size=None):
file_dir = os.path.join(args.data_dir, args.eval_file_name)
num_examples = int(len(linecache.getlines(file_dir)))
print('number of examples: ', str(num_examples))
dataset = PregeneratedDataset(file_dir, args.cache_file_dir, 'eval', args.max_seq_length, num_examples)
dataloader = DataLoader(dataset, sampler=sampler(dataset), batch_size=batch_size)
return num_examples, dataloader
def train_dataloader(args, sampler, batch_size=None):
file_dir = os.path.join(args.data_dir, args.train_file_name)
num_examples = int(len(linecache.getlines(file_dir)))
print('number of examples: ', str(num_examples))
dataset = PregeneratedDataset(file_dir, args.cache_file_dir, 'train', args.max_seq_length, num_examples)
dataloader = DataLoader(dataset, sampler=sampler(dataset), batch_size=batch_size)
return num_examples, dataloader
class CrossPregeneratedDataset(Dataset):
def __init__(self, data_path, cache_path, set_type, max_seq_length, num_examples):
logger.info('data_path: {}'.format(data_path))
self.seq_len = max_seq_length
self.set_type = set_type
self.num_samples = num_examples
self.working_dir = Path(cache_path)
o_input_ids = np.memmap(filename=self.working_dir/'o_input_ids.memmap',
mode='w+', dtype=np.int32, shape=(self.num_samples, self.seq_len))
o_input_masks = np.memmap(filename=self.working_dir/'o_input_masks.memmap',
shape=(self.num_samples, self.seq_len), mode='w+', dtype=np.int32)
o_segment_ids = np.memmap(filename=self.working_dir/'o_segment_ids.memmap',
shape=(self.num_samples, self.seq_len), mode='w+', dtype=np.int32)
m_input_ids = np.memmap(filename=self.working_dir / 'm_input_ids.memmap',
mode='w+', dtype=np.int32, shape=(self.num_samples, self.seq_len))
m_input_masks = np.memmap(filename=self.working_dir / 'm_input_masks.memmap',
shape=(self.num_samples, self.seq_len), mode='w+', dtype=np.int32)
m_segment_ids = np.memmap(filename=self.working_dir / 'm_segment_ids.memmap',
shape=(self.num_samples, self.seq_len), mode='w+', dtype=np.int32)
if self.set_type != 'eval':
label_ids = np.memmap(filename=self.working_dir/'label_ids.memmap',
shape=(self.num_samples, ), mode='w+', dtype=np.int32)
label_ids[:] = -1
else:
label_ids = None
logging.info("Loading examples.")
with open(data_path, 'r') as f:
for i, line in enumerate(tqdm(f, total=self.num_samples, desc="Examples")):
tokens = line.strip().split(',')
o_guid = tokens[0]
o_input_ids[i] = [int(id) for id in tokens[1].split()]
o_input_masks[i] = [int(id) for id in tokens[2].split()]
o_segment_ids[i] = [int(id) for id in tokens[3].split()]
m_guid = tokens[5]
m_input_ids[i] = [int(id) for id in tokens[6].split()]
m_input_masks[i] = [int(id) for id in tokens[7].split()]
m_segment_ids[i] = [int(id) for id in tokens[8].split()]
assert o_guid in m_guid
if self.set_type != 'eval':
assert int(tokens[4]) == int(tokens[9])
label_ids[i] = int(tokens[4])
if label_ids[i] != 0 and label_ids[i] != 1:
print(i)
raise KeyError
if i < 1:
logger.info("*** Example ***")
logger.info("guid: %s // %s" % (o_guid, m_guid))
logger.info("o_input_ids: %s" % " ".join([str(x) for x in o_input_ids[i]]))
logger.info("o_input_masks: %s" % " ".join([str(x) for x in o_input_masks[i]]))
logger.info("o_segment_ids: %s" % " ".join([str(x) for x in o_segment_ids[i]]))
logger.info("m_input_ids: %s" % " ".join([str(x) for x in m_input_ids[i]]))
logger.info("m_input_masks: %s" % " ".join([str(x) for x in m_input_masks[i]]))
logger.info("m_segment_ids: %s" % " ".join([str(x) for x in m_segment_ids[i]]))
if self.set_type != 'eval':
logger.info("label: %s" % str(label_ids[i]))
logging.info("Loading complete!")
self.o_input_ids = o_input_ids
self.o_input_masks = o_input_masks
self.o_segment_ids = o_segment_ids
self.m_input_ids = m_input_ids
self.m_input_masks = m_input_masks
self.m_segment_ids = m_segment_ids
if self.set_type != 'eval':
self.label_ids = label_ids
def __len__(self):
return self.num_samples
def __getitem__(self, item):
if self.set_type == 'eval':
raise NotImplementedError
else:
return (torch.tensor(self.o_input_ids[item], dtype=torch.long),
torch.tensor(self.o_input_masks[item], dtype=torch.long),
torch.tensor(self.o_segment_ids[item], dtype=torch.long),
torch.tensor(self.m_input_ids[item], dtype=torch.long),
torch.tensor(self.m_input_masks[item], dtype=torch.long),
torch.tensor(self.m_segment_ids[item], dtype=torch.long),
torch.tensor(self.label_ids[item], dtype=torch.long)
)
def train_cross_dataloader(args, sampler, batch_size=None):
file_dir = os.path.join(args.data_dir, args.train_file_name)
num_examples = int(len(linecache.getlines(file_dir)))
print('number of examples: ', str(num_examples))
dataset = CrossPregeneratedDataset(file_dir, args.cache_file_dir, 'train', args.max_seq_length, num_examples)
dataloader = DataLoader(dataset, sampler=sampler(dataset), batch_size=batch_size)
return num_examples, dataloader