-
Notifications
You must be signed in to change notification settings - Fork 4
/
dataloader.py
381 lines (311 loc) · 14.7 KB
/
dataloader.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import collections
import random
import numpy as np
import torch
class ContextFreeTokenizer():
def __init__(self, mapping):
self.mapping = mapping
self.pad = mapping['<pad>']
self.unk = mapping['<unk>']
def __call__(self, item):
tokens = item['token']
anchor_index = item['anchor_index']
# Crop
start_crop = 0
end_crop = len(tokens)
if end_crop - anchor_index > 15:
end_crop = anchor_index + 16
if anchor_index > 15:
start_crop = anchor_index - 15
anchor_index = 15
crop_tokens = tokens[start_crop:end_crop]
indices = []
for token in crop_tokens:
if token in self.mapping:
indices.append(self.mapping[token])
else:
indices.append(self.unk)
l = len(crop_tokens)
# Make position embedding
dist = [0 for x in range(l)]
for i in range(anchor_index):
dist[i] = i - anchor_index + 15
for i in range(anchor_index + 1, l):
dist[i] = i - anchor_index + 15
item['length'] = len(indices)
item['indices'] = indices + [self.pad for _ in range(31 - len(indices))]
item['anchor_index'] = anchor_index
item['dist'] = dist + [self.pad for _ in range(31 - len(dist))]
return item
def load_mapping_vector_tokenizer(embedding):
import utils
assert embedding in ['word2vec', 'glove', 'debug']
if embedding == 'debug':
mapping, vectors = utils.load_text_vec(utils.GLOVE50)
if embedding == 'word2vec':
mapping, vectors = utils.load_text_vec(utils.WORD2VEC)
if embedding == 'glove':
mapping, vectors = utils.load_text_vec(utils.GLOVE)
tokenizer = ContextFreeTokenizer(mapping)
return mapping, vectors, tokenizer
def load_ace_dataset(options):
import utils
test_type = options.test_type
data, label2idx = utils.read_ace_data(utils.ACE)
train = data['train']
valid = data['dev']
test = data['test']
other = data['other']
data = train + valid + test
# Filter test from train:
train = [x for x in data if not x['label'].startswith(test_type)]
rest = [x for x in data if x['label'].startswith(test_type)]
valid = []
test = []
for label, idx in label2idx.items():
samples = [x for x in train if x['target'] == idx]
if len(samples) > 0:
for x in range(30 // (len(samples))):
train += samples
counter = collections.Counter()
counter.update([x['target'] for x in rest])
accepted_target_classes = [k for k, v in counter.items() if v > 20]
print(accepted_target_classes)
for t in accepted_target_classes:
samples = [x for x in rest if x['target'] == t]
valid += samples[:len(samples) // 2]
test += samples[len(samples) // 2:]
l = len(other) // 3
train_other = other[:l]
valid_other = other[l:2 * l]
test_other = other[2 * l:]
return train, valid, test, train_other, valid_other, test_other
def load_tac_dataset(options):
import utils
test_type = options.test_type
data, label2idx = utils.read_tac_from_pickle()
other = data['other']
train = data['train']
test = data['test']
_data = train + test
# Filter test from train:
train = [x for x in _data if not x['label'].startswith(test_type)]
rest = [x for x in _data if x['label'].startswith(test_type)]
valid = []
test = []
for label, idx in label2idx.items():
samples = [x for x in train if x['target'] == idx]
if len(samples) > 0:
for x in range(30 // (len(samples))):
train += samples
counter = collections.Counter()
counter.update([x['target'] for x in rest])
accepted_target_classes = [k for k, v in counter.items() if v > 20]
# print(accepted_target_classes)
for t in accepted_target_classes:
samples = [x for x in rest if x['target'] == t]
valid += samples[:len(samples) // 2]
test += samples[len(samples) // 2:]
# utils.print_label_distribution(train, valid, test)
l = len(other) // 3
train_other = other[:l]
valid_other = other[l:2 * l]
test_other = other[2 * l:]
return train, valid, test, train_other, valid_other, test_other
class Fewshot(object):
def __init__(self, data, tokenizer, N=5, K=5, Q=4, O=0, other=None, noise=0.0):
self.positive_length = len(data)
self.negative_length = len(data)
self.max_length = 31
self.tokenizer = tokenizer
self.data = data
self.other = other
self.noise = noise
self.N = N
self.K = K
self.Q = Q
self.O = O
self.data_word2vec = [None for _ in range(self.positive_length)]
self.data_dist = [None for _ in range(self.positive_length)]
self.data_length = [None for _ in range(self.positive_length)]
self.data_anchor_index = [None for _ in range(self.positive_length)]
self.data_mask = [None for _ in range(self.positive_length)]
self.other_word2vec = [None for _ in range(self.negative_length)]
self.other_dist = [None for _ in range(self.negative_length)]
self.other_length = [None for _ in range(self.negative_length)]
self.other_anchor_index = [None for _ in range(self.negative_length)]
self.other_mask = [None for _ in range(self.negative_length)]
self.data_cached = set()
self.other_cached = set()
self.event2indices = {'other': [x for x in range(self.negative_length)]}
targets = {x['target'] for x in data}
for t in targets:
indices = [idx for idx, x in enumerate(data) if x['target'] == t]
self.event2indices[t] = indices
def __len__(self):
return 10000000
def get_positive(self, indices):
word2vec, dist, length, anchor_index, mask = [], [], [], [], []
for idx in indices:
if idx in self.data_cached:
word2vec.append(self.data_word2vec[idx])
dist.append(self.data_dist[idx])
length.append(self.data_length[idx])
anchor_index.append(self.data_anchor_index[idx])
mask.append(self.data_mask[idx])
else:
item = self.tokenizer(self.data[idx])
word2vec.append(item['indices'])
self.data_word2vec[idx] = item['indices']
dist.append(item['dist'])
length.append(item['length'])
anchor_index.append(item['anchor_index'])
l = item['length']
m = [1.0 for _ in range(l)] + [0.0 for _ in range(31 - l)]
mask.append(m)
self.data_dist[idx] = item['dist']
self.data_length[idx] = item['length']
self.data_anchor_index[idx] = item['anchor_index']
self.data_mask[idx] = m
self.data_cached.add(idx)
return word2vec, dist, length, anchor_index, mask
def get_negative(self):
scope = self.event2indices['other']
indices = random.sample(scope, self.O)
word2vec, dist, length, anchor_index, mask = [], [], [], [], []
for idx in indices:
if idx in self.other_cached:
word2vec.append(self.other_word2vec[idx])
dist.append(self.other_dist[idx])
length.append(self.other_length[idx])
anchor_index.append(self.other_anchor_index[idx])
mask.append(self.other_mask[idx])
else:
item = self.tokenizer(self.other[idx])
word2vec.append(item['indices'])
self.other_word2vec[idx] = item['indices']
dist.append(item['dist'])
length.append(item['length'])
anchor_index.append(item['anchor_index'])
l = item['length']
m = [1.0 for _ in range(l)] + [0.0 for _ in range(31 - l)]
mask.append(m)
self.other_dist[idx] = item['dist']
self.other_length[idx] = item['length']
self.other_anchor_index[idx] = item['anchor_index']
self.other_mask[idx] = m
self.other_cached.add(idx)
negative = {'word2vec': word2vec, 'dist': dist, 'length': length, 'anchor_index': anchor_index, 'mask': mask}
return negative
def __getitem__(self, item):
N, K, Q = self.N, self.K, self.Q
target_classes = random.sample(self.event2indices.keys(), N)
noise_classes = []
for class_name in self.event2indices.keys():
if not (class_name in target_classes):
noise_classes.append(class_name)
support_set = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
query_set = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
query_label = []
for i, class_name in enumerate(target_classes): # N way
scope = self.event2indices[class_name]
indices = random.sample(scope, K + Q)
word2vec, dist, length, anchor_index, mask = self.get_positive(indices)
support_word2vec, query_word2vec = word2vec[:K], word2vec[K:]
support_dist, query_dist = dist[:K], dist[K:]
support_length, query_length = length[:K], length[K:]
support_anchor_index, query_anchor_index = anchor_index[:K], anchor_index[K:]
support_mask, query_mask = mask[:K], mask[K:]
if self.noise > 0.0:
for j in range(K):
prob = np.random.rand()
if prob < self.noise:
noise_class_name = noise_classes[np.random.randint(0, len(noise_classes))]
scope = self.event2indices[noise_class_name]
indices = random.sample(scope, 1)
word2vec, dist, length, anchor_index, mask = self.get_positive(indices)
support_word2vec[j] = word2vec[0]
support_dist[j] = dist[0]
support_length[j] = length[0]
support_anchor_index[j] = anchor_index[0]
support_mask[j] = mask[0]
support_set['word2vec'].append(support_word2vec)
support_set['dist'].append(support_dist)
support_set['length'].append(support_length)
support_set['anchor_index'].append(support_anchor_index)
support_set['mask'].append(support_mask)
query_set['word2vec'].append(query_word2vec)
query_set['dist'].append(query_dist)
query_set['length'].append(query_length)
query_set['anchor_index'].append(query_anchor_index)
query_set['mask'].append(query_mask)
query_label += [i] * Q
negative = self.get_negative() if self.O > 0 else None
return support_set, query_set, negative, query_label
def fewshot_fn(items):
support = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
query = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
label = []
for current_support, current_query, negative, current_label in items:
support['word2vec'].append(current_support['word2vec'])
support['dist'].append(current_support['dist'])
support['length'].append(current_support['length'])
support['anchor_index'].append(current_support['anchor_index'])
support['mask'].append(current_support['mask'])
query['word2vec'].append(current_query['word2vec'])
query['dist'].append(current_query['dist'])
query['length'].append(current_query['length'])
query['anchor_index'].append(current_query['anchor_index'])
query['mask'].append(current_query['mask'])
label.append(current_label)
support_ts = {'word2vec': torch.Tensor(support['word2vec']).long(),
'dist': torch.Tensor(support['dist']).long(),
'length': torch.Tensor(support['length']).long(),
'mask': torch.Tensor(support['mask']).float(),
'anchor_index': torch.Tensor(support['anchor_index']).long()}
query_ts = {'word2vec': torch.Tensor(query['word2vec']).long(),
'dist': torch.Tensor(query['dist']).long(),
'length': torch.Tensor(query['length']).long(),
'mask': torch.Tensor(query['mask']).float(),
'anchor_index': torch.Tensor(query['anchor_index']).long()}
label_ts = torch.Tensor(label).long()
return support_ts, query_ts, None, label_ts
def fewshot_negative_fn(items):
support = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
query = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
negative = {'word2vec': [], 'dist': [], 'length': [], 'anchor_index': [], 'mask': []}
label = []
for s, q, o, l in items:
support['word2vec'].append(s['word2vec'])
support['dist'].append(s['dist'])
support['length'].append(s['length'])
support['anchor_index'].append(s['anchor_index'])
support['mask'].append(s['mask'])
query['word2vec'].append(q['word2vec'])
query['dist'].append(q['dist'])
query['length'].append(q['length'])
query['anchor_index'].append(q['anchor_index'])
query['mask'].append(q['mask'])
negative['word2vec'].append(o['word2vec'])
negative['dist'].append(o['dist'])
negative['length'].append(o['length'])
negative['anchor_index'].append(o['anchor_index'])
negative['mask'].append(o['mask'])
label.append(l)
support_ts = {'word2vec': torch.Tensor(support['word2vec']).long(),
'dist': torch.Tensor(support['dist']).long(),
'length': torch.Tensor(support['length']).long(),
'mask': torch.Tensor(support['mask']).float(),
'anchor_index': torch.Tensor(support['anchor_index']).long()}
query_ts = {'word2vec': torch.Tensor(query['word2vec']).long(),
'dist': torch.Tensor(query['dist']).long(),
'length': torch.Tensor(query['length']).long(),
'mask': torch.Tensor(query['mask']).float(),
'anchor_index': torch.Tensor(query['anchor_index']).long()}
negative_ts = {'word2vec': torch.Tensor(negative['word2vec']).long(),
'dist': torch.Tensor(negative['dist']).long(),
'length': torch.Tensor(negative['length']).long(),
'mask': torch.Tensor(negative['mask']).float(),
'anchor_index': torch.Tensor(negative['anchor_index']).long()}
label_ts = torch.Tensor(label).long()
return support_ts, query_ts, negative_ts, label_ts