-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
161 lines (124 loc) · 5.17 KB
/
base.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
import random
from collections import defaultdict
import torch
from torch import nn
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import DataLoader
class InstructBase(nn.Module):
"""
Base class for preprocessing and dataloader
"""
def __init__(self, base_config):
super().__init__()
# classes: label name [eg. PERS, ORG, ...]
self.max_width = base_config.max_width
self.base_config = base_config
def get_dict(self, spans):
dict_tag = defaultdict(int)
for span in spans:
dict_tag[(span[0], span[1])] = 1
return dict_tag
def preprocess_spans(self, tokens, ner, rel):
max_len = self.base_config.max_len
if len(tokens) > max_len:
length = max_len
tokens = tokens[:max_len]
else:
length = len(tokens)
spans_idx = []
for i in range(length):
spans_idx.extend([(i, i + j) for j in range(self.max_width)])
dict_lab = self.get_dict(ner) if ner else defaultdict(int)
# 0 for null labels
span_label = torch.LongTensor([dict_lab[i] for i in spans_idx])
spans_idx = torch.LongTensor(spans_idx)
# mask for valid spans
valid_span_mask = spans_idx[:, 1] > length - 1
# mask invalid positions
span_label = span_label.masked_fill(valid_span_mask, -1)
return {
'tokens': tokens,
'span_idx': spans_idx,
'span_label': span_label,
'seq_length': length,
'entities': ner,
'relations': rel,
}
def collate_fn(self, batch_list, relation_types=None):
# batch = [self.preprocess_spans(['tokenized_text'], b['spans'], b['relations'])
# for b in batch_list]
if relation_types is None:
negs = self.get_negatives(batch_list, 100)
rel_to_id = []
id_to_rel = []
for b in batch_list:
random.shuffle(negs)
# negs = negs[:sampled_neg]
max_neg_type_ratio = int(self.base_config.max_neg_type_ratio)
if max_neg_type_ratio == 0:
# no negatives
neg_type_ratio = 0
else:
neg_type_ratio = random.randint(0, max_neg_type_ratio)
if neg_type_ratio == 0:
# no negatives
negs_i = []
else:
negs_i = negs[:len(b['relations']) * neg_type_ratio]
# this is the list of all possible entity types (positive and negative)
types = list(set([el[-1] for el in b['relations']] + negs_i))
# shuffle (every epoch)
random.shuffle(types)
if len(types) != 0:
# prob of higher number shoul
# random drop
if self.base_config.random_drop:
num_ents = random.randint(1, len(types))
types = types[:num_ents]
# maximum number of entities types
types = types[:int(self.base_config.max_types)]
# supervised training
if "label" in b:
types = sorted(b["label"])
class_to_id = {k: v for v, k in enumerate(types, start=1)}
id_to_class = {k: v for v, k in class_to_id.items()}
rel_to_id.append(class_to_id)
id_to_rel.append(id_to_class)
batch = [
self.preprocess_spans(b["tokenized_text"], b["spans"], b["relations"]) for i, b in enumerate(batch_list)
]
else:
rel_to_id = {k: v for v, k in enumerate(relation_types, start=1)}
id_to_rel = {k: v for v, k in rel_to_id.items()}
batch = [
self.preprocess_spans(b["tokenized_text"], b["spans"], b["relations"]) for b in batch_list
]
span_idx = pad_sequence(
[b['span_idx'] for b in batch], batch_first=True, padding_value=0
)
span_label = pad_sequence(
[el['span_label'] for el in batch], batch_first=True, padding_value=-1
)
return {
'seq_length': torch.LongTensor([el['seq_length'] for el in batch]),
'span_idx': span_idx,
'tokens': [el['tokens'] for el in batch],
'span_mask': span_label != -1,
'span_label': span_label,
'entities': [el['entities'] for el in batch],
'relations': [el['relations'] for el in batch],
'classes_to_id': rel_to_id,
'id_to_classes': id_to_rel,
}
@staticmethod
def get_negatives(batch_list, sampled_neg=50):
rel_types = []
for b in batch_list:
types = set([el[-1] for el in b['relations']])
rel_types.extend(list(types))
ent_types = list(set(rel_types))
# sample negatives
random.shuffle(ent_types)
return ent_types[:sampled_neg]
def create_dataloader(self, data, relation_types=None, **kwargs):
return DataLoader(data, collate_fn=lambda x: self.collate_fn(x, relation_types), **kwargs)