Repository navigation
Expand file tree
/
Copy pathsampling.py
More file actions
203 lines (162 loc) · 8.08 KB
/
Copy pathsampling.py
File metadata and controls
203 lines (162 loc) · 8.08 KB
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
import random
import torch
from spert import util
def create_train_sample(doc, neg_entity_count: int, neg_rel_count: int, max_span_size: int, rel_type_count: int):
encodings = doc.encoding
token_count = len(doc.tokens)
context_size = len(encodings)
# positive entities
pos_entity_spans, pos_entity_types, pos_entity_masks, pos_entity_sizes = [], [], [], []
for e in doc.entities:
if e._tokens == []:
continue
pos_entity_spans.append(e.span)
pos_entity_types.append(e.entity_type.index)
pos_entity_masks.append(create_entity_mask(*e.span, context_size))
pos_entity_sizes.append(len(e.tokens))
# positive relations
pos_rels, pos_rel_spans, pos_rel_types, pos_rel_masks = [], [], [], []
for rel in doc.relations:
s1, s2 = rel.head_entity.span, rel.tail_entity.span
pos_rels.append((pos_entity_spans.index(s1), pos_entity_spans.index(s2)))
pos_rel_spans.append((s1, s2))
pos_rel_types.append(rel.relation_type)
pos_rel_masks.append(create_rel_mask(s1, s2, context_size))
# negative entities
neg_entity_spans, neg_entity_sizes = [], []
for size in range(1, max_span_size + 1):
for i in range(0, (token_count - size) + 1):
span = doc.tokens[i:i + size].span
if span not in pos_entity_spans:
neg_entity_spans.append(span)
neg_entity_sizes.append(size)
# sample negative entities
neg_entity_samples = random.sample(list(zip(neg_entity_spans, neg_entity_sizes)),
min(len(neg_entity_spans), neg_entity_count))
neg_entity_spans, neg_entity_sizes = zip(*neg_entity_samples) if neg_entity_samples else ([], [])
neg_entity_masks = [create_entity_mask(*span, context_size) for span in neg_entity_spans]
neg_entity_types = [0] * len(neg_entity_spans)
# negative relations
# use only strong negative relations, i.e. pairs of actual (labeled) entities that are not related
neg_rel_spans = []
for i1, s1 in enumerate(pos_entity_spans):
for i2, s2 in enumerate(pos_entity_spans):
rev = (s2, s1)
rev_symmetric = rev in pos_rel_spans and pos_rel_types[pos_rel_spans.index(rev)].symmetric
# do not add as negative relation sample:
# neg. relations from an entity to itself
# entity pairs that are related according to gt
# entity pairs whose reverse exists as a symmetric relation in gt
if s1 != s2 and (s1, s2) not in pos_rel_spans and not rev_symmetric:
neg_rel_spans.append((s1, s2))
# sample negative relations
neg_rel_spans = random.sample(neg_rel_spans, min(len(neg_rel_spans), neg_rel_count))
neg_rels = [(pos_entity_spans.index(s1), pos_entity_spans.index(s2)) for s1, s2 in neg_rel_spans]
neg_rel_masks = [create_rel_mask(*spans, context_size) for spans in neg_rel_spans]
neg_rel_types = [0] * len(neg_rel_spans)
# merge
entity_types = pos_entity_types + neg_entity_types
entity_masks = pos_entity_masks + neg_entity_masks
entity_sizes = pos_entity_sizes + list(neg_entity_sizes)
rels = pos_rels + neg_rels
rel_types = [r.index for r in pos_rel_types] + neg_rel_types
rel_masks = pos_rel_masks + neg_rel_masks
assert len(entity_masks) == len(entity_sizes) == len(entity_types)
assert len(rels) == len(rel_masks) == len(rel_types)
# create tensors
# token indices
encodings = torch.tensor(encodings, dtype=torch.long)
# masking of tokens
context_masks = torch.ones(context_size, dtype=torch.bool)
# also create samples_masks:
# tensors to mask entity/relation samples of batch
# since samples are stacked into batches, "padding" entities/relations possibly must be created
# these are later masked during loss computation
if entity_masks:
entity_types = torch.tensor(entity_types, dtype=torch.long)
entity_masks = torch.stack(entity_masks)
entity_sizes = torch.tensor(entity_sizes, dtype=torch.long)
entity_sample_masks = torch.ones([entity_masks.shape[0]], dtype=torch.bool)
else:
# corner case handling (no pos/neg entities)
entity_types = torch.zeros([1], dtype=torch.long)
entity_masks = torch.zeros([1, context_size], dtype=torch.bool)
entity_sizes = torch.zeros([1], dtype=torch.long)
entity_sample_masks = torch.zeros([1], dtype=torch.bool)
if rels:
rels = torch.tensor(rels, dtype=torch.long)
rel_masks = torch.stack(rel_masks)
rel_types = torch.tensor(rel_types, dtype=torch.long)
rel_sample_masks = torch.ones([rels.shape[0]], dtype=torch.bool)
else:
# corner case handling (no pos/neg relations)
rels = torch.zeros([1, 2], dtype=torch.long)
rel_types = torch.zeros([1], dtype=torch.long)
rel_masks = torch.zeros([1, context_size], dtype=torch.bool)
rel_sample_masks = torch.zeros([1], dtype=torch.bool)
# relation types to one-hot encoding
rel_types_onehot = torch.zeros([rel_types.shape[0], rel_type_count], dtype=torch.float32)
rel_types_onehot.scatter_(1, rel_types.unsqueeze(1), 1)
rel_types_onehot = rel_types_onehot[:, 1:] # all zeros for 'none' relation
return dict(encodings=encodings, context_masks=context_masks, entity_masks=entity_masks,
entity_sizes=entity_sizes, entity_types=entity_types,
rels=rels, rel_masks=rel_masks, rel_types=rel_types_onehot,
entity_sample_masks=entity_sample_masks, rel_sample_masks=rel_sample_masks)
def create_eval_sample(doc, max_span_size: int):
encodings = doc.encoding
token_count = len(doc.tokens)
context_size = len(encodings)
# create entity candidates
entity_spans = []
entity_masks = []
entity_sizes = []
for size in range(1, max_span_size + 1):
for i in range(0, (token_count - size) + 1):
span = doc.tokens[i:i + size].span
entity_spans.append(span)
entity_masks.append(create_entity_mask(*span, context_size))
entity_sizes.append(size)
# create tensors
# token indices
_encoding = encodings
encodings = torch.zeros(context_size, dtype=torch.long)
encodings[:len(_encoding)] = torch.tensor(_encoding, dtype=torch.long)
# masking of tokens
context_masks = torch.zeros(context_size, dtype=torch.bool)
context_masks[:len(_encoding)] = 1
# entities
if entity_masks:
entity_masks = torch.stack(entity_masks)
entity_sizes = torch.tensor(entity_sizes, dtype=torch.long)
entity_spans = torch.tensor(entity_spans, dtype=torch.long)
# tensors to mask entity samples of batch
# since samples are stacked into batches, "padding" entities possibly must be created
# these are later masked during evaluation
entity_sample_masks = torch.tensor([1] * entity_masks.shape[0], dtype=torch.bool)
else:
# corner case handling (no entities)
entity_masks = torch.zeros([1, context_size], dtype=torch.bool)
entity_sizes = torch.zeros([1], dtype=torch.long)
entity_spans = torch.zeros([1, 2], dtype=torch.long)
entity_sample_masks = torch.zeros([1], dtype=torch.bool)
return dict(encodings=encodings, context_masks=context_masks, entity_masks=entity_masks,
entity_sizes=entity_sizes, entity_spans=entity_spans, entity_sample_masks=entity_sample_masks)
def create_entity_mask(start, end, context_size):
mask = torch.zeros(context_size, dtype=torch.bool)
mask[start:end] = 1
return mask
def create_rel_mask(s1, s2, context_size):
start = s1[1] if s1[1] < s2[0] else s2[1]
end = s2[0] if s1[1] < s2[0] else s1[0]
mask = create_entity_mask(start, end, context_size)
return mask
def collate_fn_padding(batch):
padded_batch = dict()
keys = batch[0].keys()
for key in keys:
samples = [s[key] for s in batch]
if not batch[0][key].shape:
padded_batch[key] = torch.stack(samples)
else:
padded_batch[key] = util.padded_stack([s[key] for s in batch])
return padded_batch