-
Notifications
You must be signed in to change notification settings - Fork 7
/
loader.py
147 lines (124 loc) · 6.31 KB
/
loader.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
from registry import register
from functools import partial
import torch
import random
from torch.utils.data import DataLoader
from utils import load_dataset, TextData, repre_word, load_neg_samples
from attacks import get_random_attack
registry = {}
register = partial(register, registry=registry)
@register('simple')
class SimpleLoader():
def __init__(self, args, TOKENIZER):
self.batch_size = args.batch_size
self.shuffle = args.shuffle
self.dim = args.emb_dim
self.input_type = args.input_type
self.lowercase = args.lowercase
self.tokenizer = TOKENIZER
def collate_fn(self, batch_data, pad=0):
batch_words, batch_oririn_repre = list(zip(*batch_data))
aug_words, aug_repre, aug_ids = list(), list(), list()
for index in range(len(batch_words)):
aug_word = batch_words[index]
repre, repre_ids = repre_word(aug_word, self.tokenizer, rtype=self.input_type)
aug_words.append(aug_word)
aug_repre.append(repre)
aug_ids.append(repre_ids)
batch_words = list(batch_words) + aug_words
batch_oririn_repre = torch.FloatTensor(batch_oririn_repre)
max_len = max([len(seq) for seq in aug_ids])
batch_aug_repre_ids = [char + [pad] * (max_len - len(char)) for char in aug_ids]
batch_aug_repre_ids = torch.LongTensor(batch_aug_repre_ids)
mask = torch.ne(batch_aug_repre_ids, pad).unsqueeze(2)
return batch_words, batch_oririn_repre, batch_aug_repre_ids, mask
def __call__(self, data_path, neg_sample_path=''):
dataset, _ = load_dataset(path=data_path, DIM=self.dim, lower=self.lowercase)
dataset = TextData(dataset)
train_iterator = DataLoader(dataset=dataset, batch_size=self.batch_size // 2, shuffle=self.shuffle,
collate_fn=self.collate_fn)
return train_iterator
@register('aug')
class SimpleLoader():
def __init__(self, args, TOKENIZER):
self.batch_size = args.batch_size
self.shuffle = args.shuffle
self.dim = args.emb_dim
self.input_type = args.input_type
self.lowercase = args.lowercase
self.tokenizer = TOKENIZER
def collate_fn(self, batch_data, pad=0):
batch_words, batch_oririn_repre = list(zip(*batch_data))
aug_words, aug_repre, aug_ids = list(), list(), list()
for index in range(len(batch_words)):
aug_word = get_random_attack(batch_words[index])
repre, repre_ids = repre_word(aug_word, self.tokenizer, rtype=self.input_type)
aug_words.append(aug_word)
aug_repre.append(repre)
aug_ids.append(repre_ids)
batch_words = list(batch_words) + aug_words
batch_oririn_repre = torch.FloatTensor(batch_oririn_repre)
max_len = max([len(seq) for seq in aug_ids])
batch_aug_repre_ids = [char + [pad] * (max_len - len(char)) for char in aug_ids]
batch_aug_repre_ids = torch.LongTensor(batch_aug_repre_ids)
mask = torch.ne(batch_aug_repre_ids, pad).unsqueeze(2)
return batch_words, batch_oririn_repre, batch_aug_repre_ids, mask
def __call__(self, data_path):
dataset, _ = load_dataset(path=data_path, DIM=self.dim, lower=self.lowercase)
dataset = TextData(dataset)
train_iterator = DataLoader(dataset=dataset, batch_size=self.batch_size // 2, shuffle=self.shuffle,
collate_fn=self.collate_fn)
return train_iterator
@register('hard')
class SimpleLoader():
def __init__(self, args, TOKENIZER):
self.batch_size = args.batch_size
self.shuffle = args.shuffle
self.hard_neg_numbers = args.hard_neg_numbers
self.dim = args.emb_dim
self.neg_samples = load_neg_samples(args.hard_neg_path)
self.input_type = args.input_type
self.lowercase = args.lowercase
self.all_words = list(self.neg_samples.keys())
self.tokenizer = TOKENIZER
# to load in the call function
self.emb = None
def collate_fn(self, batch_data, pad=0):
batch_words, batch_oririn_repre = list(zip(*batch_data))
batch_words_with_hards, batch_repre_with_hards = list(), list()
for word in batch_words:
if word in self.neg_samples:
neg_words = self.neg_samples[word]
else:
neg_words = []
if len(neg_words) >= self.hard_neg_numbers:
batch_hards = list(random.sample(neg_words, self.hard_neg_numbers))
else:
sum_words = list(random.sample(self.all_words, self.hard_neg_numbers - len(neg_words)))
batch_hards = list(neg_words + sum_words)
batch_words_with_hards.append(word)
batch_words_with_hards.extend(batch_hards)
batch_repre_with_hards.append(self.emb[word])
for w in batch_hards:
if w not in self.emb:
print('this word {a} does not in vocab'.format(a=w))
batch_repre_with_hards.extend([self.emb[w] if w in self.emb else self.emb['<unk>'] for w in batch_hards])
aug_words, aug_repre, aug_ids = list(), list(), list()
for index in range(len(batch_words_with_hards)):
aug_word = get_random_attack(batch_words_with_hards[index])
repre, repre_ids = repre_word(aug_word, self.tokenizer, id_mapping=None, rtype=self.input_type)
aug_words.append(aug_word)
aug_repre.append(repre)
aug_ids.append(repre_ids)
batch_words = batch_words_with_hards + aug_words
batch_repre_with_hards = torch.FloatTensor(batch_repre_with_hards)
max_len = max([len(seq) for seq in aug_ids])
batch_aug_repre_ids = [char + [pad] * (max_len - len(char)) for char in aug_ids]
batch_aug_repre_ids = torch.LongTensor(batch_aug_repre_ids)
mask = torch.ne(batch_aug_repre_ids, pad).unsqueeze(2)
return batch_words, batch_repre_with_hards, batch_aug_repre_ids, mask
def __call__(self, data_path):
dataset, self.emb = load_dataset(path=data_path, DIM=self.dim, lower=self.lowercase)
dataset = TextData(dataset)
train_iterator = DataLoader(dataset=dataset, batch_size=self.batch_size // (2 * (self.hard_neg_numbers+1)), shuffle=self.shuffle, collate_fn=self.collate_fn)
return train_iterator