-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnlp_models.py
More file actions
192 lines (144 loc) · 6.28 KB
/
nlp_models.py
File metadata and controls
192 lines (144 loc) · 6.28 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
import torch
from torch import nn
import numpy as np
from transformers import AutoTokenizer, AutoModel, AutoConfig
"""
Different types of NLP models for the harmonizer.
LR and MLP are based on the pre-computed embedding for the input.
BERT models take the token ids as input.
"""
# Base models built on precomputed embeddings
class LR(nn.Module):
def __init__(self, input_size, n_class) -> None:
super(LR, self).__init__()
if n_class <= 2:
n_class = 1
self.linear = nn.Linear(input_size, n_class)
def forward(self, x):
return self.linear(x)
class MLP(nn.Module):
def __init__(self, input_size, n_class, hidden_size=100) -> None:
super(MLP, self).__init__()
if n_class <= 2:
n_class = 1
self.mlp = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, n_class))
def forward(self, x):
return self.mlp(x)
def get_token_ids(batch_text, bert_model):
"""
Get token IDs for batch text input to ensure equal length.
"""
tokenizer = AutoTokenizer.from_pretrained(bert_model)
return tokenizer.batch_encode_plus(batch_text, padding='longest', truncation=True)['input_ids']
def get_entity_masks(batch_examples, bert_model):
"""
Get token IDs and entity masks for batch examples of relation extraction.
Each example contains the raw text input and the character spans for the two entities.
Output: batch of token IDs and entity masks for the spans, all of same length.
"""
tokenizer = AutoTokenizer.from_pretrained(bert_model)
input_ids = []
e1_mask = []
e2_mask = []
for ex in batch_examples:
text = ex['text']
s1 = ex['span1']
s2 = ex['span2']
ids, m1, m2 = tokenize_with_breakpoints(text, s1, s2,tokenizer)
input_ids.append(ids)
e1_mask.append(m1)
e2_mask.append(m2)
# pad to same length
max_len = max([len(ids) for ids in input_ids])
for i in range(len(input_ids)):
l = len(input_ids[i])
input_ids[i] += [0] * (max_len - l)
e1_mask[i] += [0] * (max_len - l)
e2_mask[i] += [0] * (max_len - l)
return input_ids, e1_mask, e2_mask
def tokenize_with_breakpoints(text, span1, span2, tokenizer):
'''
Helper function to tokenize the text with the given spans as breakpoints
Return the token IDs and span masks for the single input example.
'''
switched = False
if span1[0] > span2[0]:
# span position switched
span1, span2 = span2[:], span1[:]
switched = True
tokens1 = [tokenizer.cls_token] + tokenizer.tokenize(text[:span1[0]])
entity1 = tokenizer.tokenize(text[span1[0]:span1[1]])
tokens2 = tokenizer.tokenize(text[span1[1]:span2[0]])
entity2 = tokenizer.tokenize(text[span2[0]:span2[1]])
tokens3 = tokenizer.tokenize(text[span2[1]:]) + [tokenizer.sep_token]
if len(entity1)==0 or len(entity2)==0:
print(text, span1, span2)
input_ids = tokenizer.convert_tokens_to_ids(tokens1+entity1+tokens2+entity2+tokens3)
mask1 = [0]*len(tokens1) + [1/len(entity1)]*len(entity1) + [0]*len(tokens2+entity2+tokens3)
mask2 = [0]*len(tokens1+entity1+tokens2) + [1/len(entity2)]*len(entity2) + [0]*len(tokens3)
# truncate the length
input_ids = input_ids[:512]
mask1 = mask1[:512]
mask2 = mask2[:512]
if not switched:
return input_ids, mask1, mask2
else:
return input_ids, mask2, mask1
# BERT models from token IDs
class BERTclf(nn.Module):
"""
BERT model for text classification. The input should be token ids.
"""
def __init__(self, n_class, bert_model) -> None:
# bert_model is the model name string
# for biomed tasks, use microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract
super(BERTclf, self).__init__()
if n_class <= 2:
n_class = 1
self.tokenizer = AutoTokenizer.from_pretrained(bert_model)
self.model = AutoModel.from_pretrained(bert_model)
self.config = AutoConfig.from_pretrained(bert_model)
self.linear = nn.Linear(self.config.hidden_size, n_class)
def forward(self, input_ids):
return self.linear(self.get_embedding(input_ids))
def get_embedding(self, input_ids):
"""
Embedding before the last classification layer.
Use the embedding of the [CLS] token.
Useful for precomputing features for LR and MLP.
"""
return self.model(input_ids).last_hidden_state[:,0,:]
def get_token_ids(self, text):
input = self.tokenizer(text, truncation=True)
return input['input_ids']
class BERTre(nn.Module):
"""
BERT model for entity relation classification. The input should be token ids and entity masks.
"""
def __init__(self, n_class, bert_model) -> None:
# bert_model is the model name string
# for biomed tasks, use microsoft/BiomedNLP-PubMedBERT-base-uncased-abstract
super(BERTre, self).__init__()
if n_class <= 2:
n_class = 1
self.tokenizer = AutoTokenizer.from_pretrained(bert_model)
self.model = AutoModel.from_pretrained(bert_model)
self.config = AutoConfig.from_pretrained(bert_model)
self.linear = nn.Linear(3*self.config.hidden_size, n_class)
def forward(self, input_ids, e1_mask, e2_mask):
return self.linear(self.get_embedding(input_ids, e1_mask, e2_mask))
def get_embedding(self, input_ids, e1_mask, e2_mask):
"""
Embedding before the last classification layer.
The relation extraction embedding is computed by concatenating
the [CLS] token embedding, and the mean-pooled embeddings for the two entity spans.
Useful for precomputing features for LR and MLP.
"""
output = self.model(torch.as_tensor(input_ids)).last_hidden_state
e0 = output[:,0,:]
e1 = torch.bmm(e1_mask.unsqueeze(1).float(), output).squeeze(1)
e2 = torch.bmm(e2_mask.unsqueeze(1).float(), output).squeeze(1)
return torch.cat((e0, e1, e2), dim=-1)