-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_inference.py
285 lines (214 loc) · 11.8 KB
/
gpt_inference.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import pandas as pd
import numpy as np
import tqdm
from functools import partial
import torch
from torch.utils.data import Dataset
from datasets import load_dataset
from datasets import Value, ClassLabel, Features, DatasetDict
from datasets import load_metric
import transformers
from transformers import AutoTokenizer, AutoModel, AutoModelForMaskedLM, AutoModelForSequenceClassification
from transformers import GPT2Tokenizer, GPTNeoForSequenceClassification
from transformers import DataCollatorWithPadding
from transformers import logging
from transformers import TrainingArguments, Trainer
from transformers import pipeline
from transformers import EvalPrediction
from omegaconf import DictConfig, OmegaConf
import hydra
from preprocessing.cleaning_utils import *
from train_utils.metrics import *
logging.set_verbosity_warning()
@hydra.main(config_path="conf/", config_name="config")
def main(cfg: DictConfig) -> None:
print(OmegaConf.to_yaml(cfg))
if cfg.hardware.gpu:
print(f"Has cuda: {torch.cuda.is_available()}")
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
print(device)
print(f"Using huggingface transformer model: {cfg.model.model_name}")
# Define file paths
if cfg.model.model_type == "bert":
tokenizer = AutoTokenizer.from_pretrained(cfg.model.model_name)
model = AutoModelForSequenceClassification.from_pretrained(cfg.model.model_name, num_labels=4)
elif cfg.model.model_type == "gptneo":
tokenizer = GPT2Tokenizer.from_pretrained(cfg.model.model_name)
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
model = GPTNeoForSequenceClassification.from_pretrained(cfg.model.model_name, num_labels=4,
problem_type="single_label_classification",
pad_token_id=tokenizer.convert_tokens_to_ids("[PAD]"),
)
model.resize_token_embeddings(len(tokenizer))
else:
raise ValueError(f"Model type isn't bert or gpt-neo, it's {cfg.model.model_type}")
# create hf Dataset
classes = ['Not Relevant', 'Neither', 'Indirect', 'Direct']
# instead we will use the raw text for now
features = Features({
'ROW ID':Value("int64"),
'HADM ID':Value("int64"),
'Assessment':Value("string"),
'Plan Subsection':Value("string"),
"Relation":Value("string"),
"S":Value("string"),
"O":Value("string")
})
dataset = load_dataset("csv", data_files={
"train":cfg.data.n2c2_data_dir + "train_so.csv",
"valid":cfg.data.n2c2_data_dir + "dev_so.csv",
},
features=features)
test_features = Features({
'ROW ID':Value("int64"),
'HADM ID':Value("int64"),
'Assessment':Value("string"),
'Plan Subsection':Value("string"),
"Relation":Value("string"),
"S":Value("string"),
"O":Value("string")
})
test_dataset = load_dataset("csv", data_files={
# "test":cfg.data.n2c2_data_dir + "n2c2_test_noLabel.csv",
"test":cfg.data.n2c2_data_dir + "n2c2_track3_test_so.csv",
},
features=test_features)
if cfg.train.fast_dev_run:
dataset['train'] = dataset['train'].shard(num_shards=1000, index=0)
dataset['valid'] = dataset['valid'].shard(num_shards=50, index=0)
# create encoded class labels and rename
dataset = dataset.class_encode_column("Relation")
label2id = {'Not Relevant':3, 'Neither':2, 'Indirect':1, 'Direct':0}
id2label = {v:k for k,v in label2id.items()}
dataset = dataset.align_labels_with_mapping(label2id, "Relation")
dataset = dataset.rename_column("Relation", "label")
test_dataset = test_dataset.class_encode_column("Relation")
test_dataset = test_dataset.align_labels_with_mapping(label2id, "Relation")
test_dataset = test_dataset.rename_column("Relation", "label")
# drop symptom list at beginning of some assessments
# dataset['train'] = dataset['train'].map(split_leading_symptom_list)
# dataset['valid'] = dataset['valid'].map(split_leading_symptom_list)
if cfg.train.so_sections:
dataset = dataset.map(partial(add_SO_sections))
test_dataset = test_dataset.map(partial(add_SO_sections))
print("AFTER TRAIN _SO sections")
print(dataset['valid'][0])
print(test_dataset['test'][0])
if cfg.train.add_ner:
nlp_assessment = spacy.load(cfg.pretrained.spacy_assessment, exclude="parser")
nlp_plan = spacy.load(cfg.pretrained.spacy_plan, exclude="parser")
# add the named entities
dataset['train'] = dataset['train'].map(partial(add_ner_assessment, nlp=nlp_assessment))
dataset['train'] = dataset['train'].map(partial(add_ner_plan, nlp=nlp_plan))
dataset['valid'] = dataset['valid'].map(partial(add_ner_assessment, nlp=nlp_assessment))
dataset['valid'] = dataset['valid'].map(partial(add_ner_plan, nlp=nlp_plan))
test_dataset['test'] = test_dataset['test'].map(partial(add_ner_assessment, nlp=nlp_assessment))
test_dataset['test'] = test_dataset['test'].map(partial(add_ner_plan, nlp=nlp_plan))
# we ASSUME that the ner labels we want are lowercase, UNLIKE the standard ones in the model
spans = [x for x in nlp_plan.get_pipe("ner").labels if x.islower()] + [x for x in nlp_assessment.get_pipe("ner").labels if x.islower()]
tokens = []
for span in spans:
tokens.append("<" + span + ">")
tokens.append("</" + span + ">")
# add the span tags to the vocab
_ = tokenizer.add_tokens(tokens)
model.resize_token_embeddings(len(tokenizer))
elif cfg.train.add_ner_end:
nlp_assessment = spacy.load(cfg.pretrained.spacy_assessment, exclude="parser")
nlp_plan = spacy.load(cfg.pretrained.spacy_plan, exclude="parser")
# add the named entities
dataset['train'] = dataset['train'].map(partial(add_ner_assessment_end, nlp=nlp_assessment))
dataset['train'] = dataset['train'].map(partial(add_ner_plan_end, nlp=nlp_plan))
dataset['valid'] = dataset['valid'].map(partial(add_ner_assessment_end, nlp=nlp_assessment))
dataset['valid'] = dataset['valid'].map(partial(add_ner_plan_end, nlp=nlp_plan))
test_dataset['test'] = test_dataset['test'].map(partial(add_ner_assessment_end, nlp=nlp_assessment))
test_dataset['test'] = test_dataset['test'].map(partial(add_ner_plan_end, nlp=nlp_plan))
# we ASSUME that the ner labels we want are lowercase, UNLIKE the standard ones in the model
spans = [x for x in nlp_plan.get_pipe("ner").labels if x.islower()] + [x for x in nlp_assessment.get_pipe("ner").labels if x.islower()]
tokens = []
for span in spans:
tokens.append("</" + span + ">")
# add the span tags to the vocab
_ = tokenizer.add_tokens(tokens)
model.resize_token_embeddings(len(tokenizer))
if cfg.train.drop_mimic_deid:
dataset = dataset.map(remove_mimic_deid)
test_dataset = test_dataset.map(remove_mimic_deid)
if cfg.train.expand_abbvs:
abbv_nlp = spacy.load("en_core_sci_lg")
abbreviations = pd.read_csv(cfg.data.abbreviation_inventory, sep="|", na_filter=False)
med_abbvs = abbreviations[abbreviations['Source'].isin(["Vanderbilt Clinic Notes", "Vanderbilt Discharge Sums", "Berman", "Stetson",
"Columbia"])]
med_abbvs = med_abbvs[~med_abbvs['SF'].isin(abbv_nlp.Defaults.stop_words)]
med_abbvs = med_abbvs[~med_abbvs['SF'].isin(["man", "woman", "old", "Mr.", "Ms.", "Mrs", "M", "F"])]
med_abbvs = med_abbvs.astype({"Source":"category"})
sorter = ["Vanderbilt Discharge Sums", "Vanderbilt Clinic Notes", "Stetson", "Columbia", "Berman"]
med_abbvs.Source.cat.set_categories(sorter, inplace=True)
med_abbvs = med_abbvs.sort_values(['Source'])
unq_sfs = med_abbvs['SF'].unique()
dataset = dataset.map(partial(expand_abbreviations, spacy_pip=abbv_nlp, abbv_map=med_abbvs, unq_sfs=unq_sfs))
test_dataset = test_dataset.map(partial(expand_abbreviations, spacy_pip=abbv_nlp, abbv_map=med_abbvs, unq_sfs=unq_sfs))
# create training args and Trainer
training_args = TrainingArguments(output_dir="test_trainer",
evaluation_strategy="epoch",
num_train_epochs=cfg.train.epochs,
per_device_train_batch_size=4,
)
# metrics to track
acc = load_metric("accuracy")
macrof1 = load_metric("f1")
# create metric_dict for compute_metrics
metric_dict = {}
metric_dict['accuracy'] = {"metric":acc}
metric_dict['f1-macro'] = {"metric":macrof1, "average":"macro"}
print("Creating GPT prompts...")
# create GPT prompts
prompts = []
labels = []
for data in dataset['valid']:
question = 'To which category does the text belong?: "Direct", "Indirect", "Neither", "Not Relevant"'
prompt_str = question + '\nAssessment: ' + data['Assessment'] + '\nPlan: ' + data['Plan Subsection'] + '\nLabel: ' #\
# + str(data['label'])
labels.append(data['label'])
prompts.append(prompt_str)
test_prompts = []
test_labels = []
for data in test_dataset['test']:
question = 'To which category does the text belong?: "Direct", "Indirect", "Neither", "Not Relevant"'
prompt_str = question + '\nAssessment: ' + data['Assessment'] + '\nPlan: ' + data['Plan Subsection'] + '\nLabel: ' #\
# + str(data['label'])
test_labels.append(data['label'])
test_prompts.append(prompt_str)
class ListDataset(Dataset):
def __init__(self, original_list):
self.original_list = original_list
def __len__(self):
return len(self.original_list)
def __getitem__(self, i):
return self.original_list[i]
print("Creating Dataset obj...")
mydataset = ListDataset(prompts)
print("Creating Dataset obj...")
my_testdataset = ListDataset(test_prompts)
print("Defining pipeline...")
rel_classifier = pipeline(task="text-classification", model=model, tokenizer=tokenizer,
return_all_scores=True, device=0, padding="max_length", max_length=512,
truncation=True)
logits = np.zeros(shape=(len(prompts),4))
labels = np.array(labels)
test_logits = np.zeros(shape=(len(test_prompts),4))
test_labels = np.array(test_labels)
print("Inference...")
for idx, pred in enumerate(tqdm.tqdm(rel_classifier(mydataset))):
pred_logits = [x['score'] for x in pred]
logits[idx, :] = pred_logits
for idx, pred in enumerate(tqdm.tqdm(rel_classifier(my_testdataset))):
pred_logits = [x['score'] for x in pred]
test_logits[idx, :] = pred_logits
compute_metrics_func=partial(compute_metrics, metric_dict=metric_dict)
metrics = compute_metrics_func(EvalPrediction(logits, labels))
print("Eval Metrics:", metrics)
metrics = compute_metrics_func(EvalPrediction(test_logits, test_labels))
print("Test Metrics:", metrics)
if __name__ == "__main__":
main()