-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinetune.py
156 lines (121 loc) · 5.31 KB
/
finetune.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
import os
import transformers
import datasets
import torch
from pathlib import Path
from datasets import load_dataset
from transformers import (AutoTokenizer, AutoModelForSequenceClassification,
TrainingArguments, Trainer,
AutoModelForMaskedLM,AutoModelForSequenceClassification, AdamW, get_linear_schedule_with_warmup)
import numpy as np
import re
import pandas as pd
from tqdm.notebook import tqdm
from sklearn.metrics import f1_score, hamming_loss
import argparse
import math
class CustomTrainer(Trainer):
def compute_loss(self, model, inputs, return_outputs=False):
labels = inputs.pop("labels")
labels = labels.unsqueeze(0)
outputs = model(**inputs)
logits = outputs.logits
loss_fct = torch.nn.BCEWithLogitsLoss()
loss = loss_fct(logits.view(-1, self.model.config.num_labels),
labels.float().view(-1, self.model.config.num_labels))
return (loss, outputs) if return_outputs else loss
def customMetrics(ep):
print(ep.predictions)
print(ep.predictions.shape)
m = np.max(ep.predictions,axis=1,keepdims=True) #returns max of each row and keeps same dims
e_x = np.exp(ep.predictions - m) #subtracts each row with its max value
s = np.sum(e_x,axis=1,keepdims=True) #returns sum of each row and keeps same dims
f_x = e_x / s
pred_round = np.round(f_x)
print(pred_round, ep.label_ids)
pred = pred_round == ep.label_ids[:len(pred_round)]
negatives = np.logical_not(ep.label_ids[:len(pred)])
guess = np.logical_and(pred, ep.label_ids[:len(pred)])
not_cite = np.logical_and(pred, negatives)
#print(np.sum(pred), np.sum(ep.label_ids[:len(pred)]), guess, not_cite)
return {'tp': int(np.sum(guess.astype(float))),
'fn': (int(np.sum(ep.label_ids[:len(pred)])) - int(np.sum(guess.astype(float)))),
'tn': int(np.sum(not_cite.astype(float))),
'fp': (int(np.sum(negatives)) - int(np.sum(not_cite.astype(float)))),
'macro-f1': f1_score(ep.label_ids, pred_round, average='macro'),
'micro-f1': f1_score(ep.label_ids, pred_round, average='micro'),
'hamming': hamming_loss(ep.label_ids, pred_round)
}
parser = argparse.ArgumentParser(description='Help me god')
parser.add_argument('-l', '--label-len', help='label length')
parser.add_argument('-f', '--form', help='format')
parser.add_argument('-r', '--resume', help='resume from checkpoint', action='store_true')
parser.add_argument('-m', '--model', help='model to fine-tune')
parser.add_argument('-t', '--token-len', default='512', help='tokenizer max length')
args = parser.parse_args()
form = args.form
l = int(args.label_len)
resume = args.resume
max_len = int(args.token_len)
print('performing fine-tuning with model {2} label length {0} and format {1}'.format(l, form, args.model))
print('resuming from checkpoint={0}'.format(args.resume))
print('loading data')
# load data
model_ckpt = args.model
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
model = AutoModelForSequenceClassification.from_pretrained(model_ckpt, num_labels=l).to('cuda')
device = 'cuda'
def prep(data, name, preproc, label_len):
cols = data.column_names
cols.remove("labels")
print(cols)
data_enc = data.map(tokenize_and_encode_in, batched=True, remove_columns=cols)
data_enc.set_format("torch")
data_enc = (data_enc
.map(lambda x : {"float_labels": x["labels"].to(torch.float)}, remove_columns=["labels"])
.rename_column("float_labels", "labels"))
return data_enc
def tokenize_and_encode_in(examples):
return tokenizer(examples['input'], max_length=max_len, truncation=True, padding=True)
train = load_dataset('json', data_files=f'data/{form}/train-citationcleaned-{l}.json', split='train')
val = load_dataset('json', data_files=f'data/{form}/val-citationcleaned-{l}.json', split='train')
test = load_dataset('json', data_files=f'data/{form}/test-citationcleaned-{l}.json', split='train')
train_enc = prep(train, 'train', form, l)
val_enc = prep(val, 'val', form, l)
test_enc = prep(test, 'test', form, l)
#model = RobertaBaseline(roberta, N_input=512*768, N_output=20)
model.resize_token_embeddings(len(tokenizer))
#multitask_model.encoder.resize_token_embeddings(len(tokenizer))
epochs = 20
batch_size=4
run_name = 'baseline-{0}-{1}-{2}'.format(form, l, model_ckpt)
args = TrainingArguments(
output_dir='baseline/{0}'.format(run_name),
evaluation_strategy = "steps",
learning_rate=2e-5,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
num_train_epochs=20,
weight_decay=0.01,
load_best_model_at_end=True,
do_eval=True,
logging_first_step=True,
logging_steps=500,
dataloader_num_workers=16,
do_train=True,
fp16=True,
run_name = run_name,
report_to = 'wandb',
eval_accumulation_steps=100,
save_total_limit = 5, # Only last 5 models are saved. Older ones are deleted.
)
#del trainer
trainer = CustomTrainer(model=model,
args=args,
train_dataset=train_enc,
eval_dataset=val_enc,
tokenizer=tokenizer,
compute_metrics=customMetrics
)
trainer.train(resume_from_checkpoint=resume)
print(trainer.predict(test_enc))