-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunllama_train_vaner.py
477 lines (407 loc) · 17 KB
/
unllama_train_vaner.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# -*- coding: utf-8 -*-
import sys
import json
import numpy as np
import evaluate
from datasets import load_dataset, Dataset, DatasetDict
from transformers import AutoTokenizer
from transformers import DataCollatorForTokenClassification
from transformers import TrainingArguments, Trainer
from peft import get_peft_model, LoraConfig, TaskType
import ipdb
from modeling_llama import UnmaskingLlamaForTokenClassification
from seqeval.metrics import precision_score, recall_score, f1_score
from utils_vaner import *
###
def vis(ds, idx):
print(' '.join(ds['train'][idx]['tokens']))
print(ds['train'][idx]['ner_tags'])
def find(ds):
for idx, ele in enumerate(ds['train']):
if 'linnaeus' in ele['tokens'][0]:
if 1 in ele['ner_tags']:
print(idx)
ipdb.set_trace()
def load_unidev_kgmix2(kg_type):
ret = {}
for split_name in ['train']:
cnt = 0
data = []
if kg_type == 'mt':
for cohort in ["BC2GM","BC4CHEMD","BC5CDR-chem","BC5CDR-disease","JNLPBA","linnaeus","s800","ncbi"]:
with open(f'./data/vaner_datacohort/{cohort}/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
if cohort == "BC2GM":
items = parse_mt(json.loads(line), split_name, cohort, 'Gene')
data.extend(items)
elif cohort == "BC4CHEMD":
items = parse_mt(json.loads(line), split_name, cohort, 'Chemical')
data.extend(items)
elif cohort == "BC5CDR-chem":
items = parse_mt(json.loads(line), split_name, cohort, 'Chemical')
data.extend(items)
elif cohort == "BC5CDR-disease":
items = parse_mt(json.loads(line), split_name, cohort, 'Disease')
data.extend(items)
elif cohort == "JNLPBA":
items = parse_mt(json.loads(line), split_name, cohort, 'Gene')
data.extend(items)
elif cohort == "linnaeus":
items = parse_mt(json.loads(line), split_name, cohort, 'Species')
data.extend(items)
elif cohort == "s800":
items = parse_mt(json.loads(line), split_name, cohort, 'Species')
data.extend(items)
elif cohort == "ncbi":
items = parse_mt(json.loads(line), split_name, cohort, 'Disease')
data.extend(items)
else:
continue
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
for split_name in ['test']:
cnt = 0
data = []
if kg_type == 'mt':
for cohort in ["ncbi"]:
with open(f'./data/vaner_datacohort/{cohort}/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
if cohort == "BC2GM":
items = parse_mt(json.loads(line), split_name, cohort, 'Gene')
data.extend(items)
elif cohort == "BC4CHEMD":
items = parse_mt(json.loads(line), split_name)
data.extend(items)
elif cohort == "BC5CDR-chem":
items = parse_mt(json.loads(line), split_name)
data.extend(items)
elif cohort == "BC5CDR-disease":
items = parse_mt(json.loads(line), split_name)
data.extend(items)
elif cohort == "JNLPBA":
items = parse_mt(json.loads(line), split_name)
data.extend(items)
elif cohort == "linnaeus":
items = parse_mt(json.loads(line), split_name)
data.extend(items)
elif cohort == "s800":
items = parse_mt(json.loads(line), split_name)
data.extend(items)
elif cohort == "ncbi":
items = parse_mt(json.loads(line), split_name, cohort, 'Disease')
data.extend(items)
else:
continue
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
# ipdb.set_trace()
return DatasetDict(ret)
def load_BC2GM(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/BC2GM/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'BC2GM', 'Gene')
data.extend(items)
else:
with open(f'./data/BC2GM/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_BC4CHEMD(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/BC4CHEMD/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'BC4CHEMD', 'Chemical')
data.extend(items)
else:
with open(f'./data/BC4CHEMD/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_BC5CDR_chem(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/BC5CDR-chem/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'BC5CDR-chem', 'Chemical')
data.extend(items)
else:
with open(f'./data/BC5CDR-chem/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_BC5CDR_disease(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/BC5CDR-disease/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'BC5CDR-disease', 'Disease')
data.extend(items)
else:
with open(f'./data/BC5CDR-disease/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_JNLPBA(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/JNLPBA/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'JNLPBA', 'Gene')
data.extend(items)
else:
with open(f'./data/JNLPBA/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_linnaeus(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/linnaeus/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'linnaeus', 'Species')
data.extend(items)
else:
with open(f'./data/linnaeus/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_s800(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/s800/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 's800', 'Species')
data.extend(items)
else:
with open(f'./data/s800/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
def load_ncbi(kg_type):
ret = {}
for split_name in ['train', 'test']:
cnt = 0
data = []
if kg_type == 'mt':
with open(f'./data/vaner_datacohort/ncbi/{split_name}_df_mix2.jsonl', 'r') as reader:
for line in reader:
# items = parse_mt_species(json.loads(line), split_name)
items = parse_mt(json.loads(line), split_name, 'ncbi', 'Disease')
data.extend(items)
else:
with open(f'./data/ncbi/{split_name}.jsonl', 'r') as reader:
for line in reader:
data.append(json.loads(line))
ret[split_name] = Dataset.from_list(data)
# print(cnt)
print(len(data))
return DatasetDict(ret)
# def load_test():
# ret = {}
# for split_name in ['train', 'dev', 'test']:
# data = []
# with open(f'./data/test/test.jsonl', 'r') as reader:
# for line in reader:
# data.append(json.loads(line))
# ret[split_name] = Dataset.from_list(data)
# return DatasetDict(ret)
task, max_length, kgtype, align_mode = sys.argv[1], int(sys.argv[2]), sys.argv[3], sys.argv[4]
print(f'handling task {task}')
epochs = 20
batch_size = 4
learning_rate = 1e-4
lora_r = 12
model_id = './Llama-2-7b-hf'
tokenizer = AutoTokenizer.from_pretrained(model_id)
if task == 'ncbi':
ds = load_ncbi(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'BC2GM':
ds = load_BC2GM(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'BC4CHEMD':
ds = load_BC4CHEMD(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'BC5CDR-chem':
ds = load_BC5CDR_chem(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'BC5CDR-disease':
ds = load_BC5CDR_disease(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'JNLPBA':
ds = load_JNLPBA(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'linnaeus':
ds = load_linnaeus(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 's800':
ds = load_s800(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
elif task == 'unidev_kgmix2':
ds = load_unidev_kgmix2(kgtype)
label2id = {'O': 0, 'B-biomedical': 1, 'I-biomedical': 2}
else:
raise NotImplementedError
id2label = {v: k for k, v in label2id.items()}
label_list = list(label2id.keys()) # ds["train"].features[f"ner_tags"].feature.names
model = UnmaskingLlamaForTokenClassification.from_pretrained(
model_id, num_labels=len(label2id), id2label=id2label, label2id=label2id
).bfloat16()
peft_config = LoraConfig(task_type=TaskType.TOKEN_CLS, inference_mode=False, r=lora_r, lora_alpha=32, lora_dropout=0.1)
model = get_peft_model(model, peft_config)
model.print_trainable_parameters()
# ipdb.set_trace()
def tokenize_and_align_labels_try1(examples):
tokenized_inputs = tokenizer(examples["tokens"], is_split_into_words=True, padding='longest', max_length=max_length, truncation=True)
labels = []
for i, label in enumerate(examples[f"ner_tags"]):
word_ids = tokenized_inputs.word_ids(batch_index=i) # Map tokens to their respective word.
previous_word_idx = None
label_ids = []
# # print("word_ids",word_ids)
word_ids_adjust=[]
for word_idx in word_ids: # Set the special tokens to -100.
if word_idx is None:
word_ids_adjust.append(None)
elif word_idx ==0: # Only label the first token of a given word.
word_ids_adjust.append(0)
else:
word_ids_adjust.append(word_idx-1)
for word_idx in word_ids_adjust: # Set the special tokens to -100.
if word_idx is None:
label_ids.append(-100)
elif word_idx != previous_word_idx: # Only label the first token of a given word.
label_ids.append(label[word_idx])
else:
label_ids.append(-100)
previous_word_idx = word_idx
labels.append(label_ids)
tokenized_inputs["labels"] = labels
return tokenized_inputs
def tokenize_and_align_labels_vaner(examples):
tokenized_inputs = tokenizer(examples["tokens"], is_split_into_words=True, padding='longest', max_length=max_length, truncation=True)
labels = []
for i, label in enumerate(examples[f"ner_tags"]):
word_ids = tokenized_inputs.word_ids(batch_index=i) # Map tokens to their respective word.
previous_word_idx = None
label_ids = []
prompt_len = 1
for word_idx in word_ids: # Set the special tokens to -100.
if word_idx is None:
label_ids.append(-100)
elif word_idx != previous_word_idx: # Only label the first token of a given word.
if word_idx < prompt_len:
label_ids.append(-100)
else:
label_ids.append(label[word_idx - prompt_len])
else:
label_ids.append(-100)
previous_word_idx = word_idx
labels.append(label_ids)
tokenized_inputs["labels"] = labels
return tokenized_inputs
if align_mode == 'vaner': tokenized_ds = ds.map(tokenize_and_align_labels_vaner, batched=True)
data_collator = DataCollatorForTokenClassification(tokenizer=tokenizer)
def compute_metrics(p):
predictions, labels = p
predictions = np.argmax(predictions, axis=2)
true_predictions = [
[label_list[p] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
true_labels = [
[label_list[l] for (p, l) in zip(prediction, label) if l != -100]
for prediction, label in zip(predictions, labels)
]
predictions = true_predictions
references = true_labels
precision = precision_score(references, predictions)
recall = recall_score(references, predictions)
f1 = f1_score(references, predictions)
accuracy = calculate_accuracy([item for sublist in references for item in sublist],
[item for sublist in predictions for item in sublist])
# ipdb.set_trace()
return {
"precision": precision,
"recall": recall,
"f1": f1,
"accuracy":accuracy,
}
output_dir = 'vaner_{}'.format(task)
training_args = TrainingArguments(
output_dir=output_dir,
learning_rate=learning_rate,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
num_train_epochs=epochs,
weight_decay=0.01,
evaluation_strategy="epoch",
save_strategy="epoch",
save_total_limit=20,
load_best_model_at_end=True,
push_to_hub=False,
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_ds["train"],
eval_dataset=tokenized_ds["test"],
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics,
)
trainer.train()