-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata_helper.py
206 lines (169 loc) · 8.88 KB
/
data_helper.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
import torch
import numpy as np
import string
import copy
import multiprocessing as mp
from tqdm import tqdm
import json
import pickle as pkl
class LSIDataset(torch.utils.data.Dataset):
def __init__(self, jsonl_file=None, data_list=None):
super().__init__()
self.annotated = False
self.sent_vectorized = False
if data_list is not None:
self.dataset = copy.deepcopy(data_list)
for instance in tqdm(self.dataset, desc="Loading data from list"):
instance['text'] = instance['text']
if 'labels' in instance:
self.annotated = True
instance['labels'] = np.array(instance['labels'])
elif jsonl_file is not None:
self.dataset = []
with open(jsonl_file) as fr:
for line in tqdm(fr, desc="Loading data from file"):
doc = json.loads(line)
text = np.array([sent for sent in doc['text']])
newdoc = {'id': doc['id'], 'text': text}
if 'labels' in doc:
self.annotated = True
labels = np.array(doc['labels'])
newdoc['labels'] = labels
self.dataset.append(newdoc)
def __len__(self):
return len(self.dataset)
def __getitem__(self, index):
return self.dataset[index]
def save_data(self, data_file):
with open(data_file, 'wb') as fw:
pkl.dump(self, fw)
def load_data(data_file):
with open(data_file, 'rb') as fr:
return pkl.load(fr)
# remove puncutations and empty sentences
def preprocess(self):
for i, instance in enumerate(tqdm(self.dataset, desc="Preprocessing")):
text = []
for j, sent in enumerate(instance['text']):
ppsent = sent.strip().lower().translate(str.maketrans('', '', string.punctuation))
if len(ppsent.split()) > 1:
text.append(ppsent)
instance['text'] = np.array(text)
# break each sentence string into word tokens
def tokenize(self):
for i, instance in enumerate(tqdm(self.dataset, desc="Tokenizing")):
text = []
for j, sent in enumerate(instance['text']):
toksent = np.array(sent.strip().split())
text.append(toksent)
instance['text'] = np.array(text, dtype=object)
# generate a vector for each sentence using Sent2Vec
def sent_vectorize(self, sent2vec_model):
for i, instance in enumerate(tqdm(self.dataset, desc="Embedding sentences")):
esents = sent2vec_model.embed_sentences(instance['text'])
instance['text'] = np.delete(esents, np.where(esents.sum(axis=1) == 0)[0], axis=0)
self.sent_vectorized = True
# unified code for generating mini batches of data for both facts and sections during train / dev / test / inference
class MiniBatch:
def __init__(self, examples, vocab=None, label_vocab=None, schemas=None, type_map=None, node_vocab=None, edge_vocab=None, adjacency=None, hidden_size=200, max_segments=4, max_segment_size=8, num_mpath_samples=2):
# provide vocab if not sent vectorized, None otherwise
self.sent_vectorized = True if vocab is None else False
# provide label_vocab if annotated, None otherwise
self.annotated = True if label_vocab is not None else False
# provide graph data if struct encoder is to be used on these examples, None otherwise
self.sample_metapaths = True if schemas is not None else False
self.max_segments = max_segments
if not self.sent_vectorized:
self.vocab = vocab
self.max_segment_size = max_segment_size
else:
self.sent_hidden_size = hidden_size
if self.annotated:
self.label_vocab = label_vocab
if self.sample_metapaths:
self.schemas = schemas
self.type_map = type_map
self.node_vocab = node_vocab
self.edge_vocab = edge_vocab
self.adjacency = adjacency
self.num_mpath_samples = num_mpath_samples
max_len = max([len(d['text']) for d in examples])
max_segments = min(self.max_segments, max_len)
# expected shape of text tensors
if not self.sent_vectorized:
max_segment_size = min(self.max_segment_size, max([len(s) for d in examples for s in d['text']]))
self.tokens = torch.zeros(len(examples), max_segments, max_segment_size, dtype=torch.long) # [D, S, W]
else:
self.doc_inputs = torch.zeros(len(examples), max_segments, self.sent_hidden_size) # [D, S, H]
self.example_ids = []
if self.annotated:
# expected shape of true label indicator tensors
self.labels = torch.zeros(len(examples), len(self.label_vocab)) # [D, C]
for i, instance in enumerate(examples):
if not self.sent_vectorized:
for j, sent in enumerate(instance['text']):
# fill up the j-th sentence of i-th example with word tokens
self.tokens[i, j, :len(sent)] = torch.from_numpy(np.array([self.vocab[w] for w in sent]))
else:
# fill up the i-th example with sentence embeddings
self.doc_inputs[i, :len(instance['text']), :] = torch.from_numpy(instance['text'])[:max_segments]
self.example_ids.append(instance['id'])
if self.annotated:
label_list = torch.from_numpy(np.array([self.label_vocab[l] for l in instance['labels']]))
self.labels[i].scatter_(0, label_list, 1.)
if not self.sent_vectorized:
self.mask = (self.tokens != 0).float() # [D, S, W]
else:
self.mask = (self.doc_inputs != 0).any(dim=2).float() # [D, S]
if self.sample_metapaths:
trg_node_tokens = torch.tensor([self.node_vocab[self.type_map[x]][x] for x in self.example_ids])
self.node_tokens, self.edge_tokens = self.generate_metapaths(trg_node_tokens, self.schemas, self.adjacency, self.edge_vocab, num_samples=self.num_mpath_samples) # N * [M, D, L+1], N * [M, D, L]
# sample metapaths using adjacency matrices
def generate_metapaths(self, indices, schemas, adjacency, edge_vocab, num_samples=2): # [D,]
indices = indices.repeat(num_samples) # [M*D,]
tokens, edge_tokens = [], []
# repeat over all schemas
for i in range(len(schemas)):
ins_tokens, ins_edge_tokens = [indices], []
for keys in schemas[i]:
neighbours = adjacency[keys].sample(num_neighbors=1, subset=ins_tokens[-1]).squeeze(1) # [M*D,]
relations = torch.full(neighbours.shape, edge_vocab[keys[1]], dtype=torch.long) # [M*D,]
ins_tokens.append(neighbours)
ins_edge_tokens.append(relations)
ins_tokens = torch.stack(ins_tokens, dim=1)
ins_tokens = ins_tokens.view(num_samples, -1, ins_tokens.size(1)) # [M, D, L+1]
ins_edge_tokens = torch.stack(ins_edge_tokens, dim=1)
ins_edge_tokens = ins_edge_tokens.view(num_samples, -1, ins_edge_tokens.size(1)) # [M, D, L]
tokens.append(ins_tokens)
edge_tokens.append(ins_edge_tokens)
return tokens, edge_tokens
# automatic memory pinning for faster cpu to cuda transfer
def pin_memory(self):
if not self.sent_vectorized:
self.tokens.pin_memory()
else:
self.doc_inputs.pin_memory()
self.mask.pin_memory()
if self.annotated:
self.labels.pin_memory()
if self.sample_metapaths:
for i in range(len(self.node_tokens)):
self.node_tokens[i].pin_memory()
self.edge_tokens[i].pin_memory()
return self
# transfer pinned cpu tensors to cuda
def cuda(self, dev='cuda'):
if not self.sent_vectorized:
self.tokens = self.tokens.cuda(dev, non_blocking=True)
else:
self.doc_inputs = self.doc_inputs.cuda(dev, non_blocking=True)
self.mask = self.mask.cuda(dev, non_blocking=True)
if self.annotated:
self.labels = self.labels.cuda(dev, non_blocking=True)
if self.sample_metapaths:
for i in range(len(self.node_tokens)):
self.node_tokens[i] = self.node_tokens[i].cuda(dev, non_blocking=True)
self.edge_tokens[i] = self.edge_tokens[i].cuda(dev, non_blocking=True)
return self
def collate_func(examples, **kwargs):
return MiniBatch(examples, **kwargs)