-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
120 lines (102 loc) · 3.91 KB
/
helpers.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
import csv
import torch
import pandas as pd
from tqdm import tqdm
from config import Config
from sklearn.metrics import accuracy_score, f1_score
from transformers import get_linear_schedule_with_warmup
FULL_FINETUNING = False
def df_for_ner(file_paths, le):
sentences = []
words = []
tags = []
numeric_tags = []
for file_path in file_paths:
with open(file_path, 'r', encoding='utf-8') as file:
for line in tqdm(file):
line = line.strip()
if line:
word, tag = line.split('\t')
words.append(word)
tags.append(tag)
numeric_tags.append(le.transform([tag])[0])
else:
sentences.append((words, tags, numeric_tags))
words = []
tags = []
numeric_tags = []
if words:
sentences.append((words, tags, numeric_tags))
return pd.DataFrame(data=sentences, columns=['words','tags', 'numeric_tags'])
def train_fn(train_data_loader, model, optimizer, device, scheduler):
#Train the Model
model.train()
loss_ = 0
acc_ = 0
for data in tqdm(train_data_loader, total = len(train_data_loader)):
for i, j in data.items():
data[i] = j.to(device)
#Backward Propagation
optimizer.zero_grad()
tag, loss = model(**data)
preds = torch.argmax(torch.softmax(tag, dim=0), dim=-1).flatten()
targets = data['target_tags'].flatten()
acc_ += accuracy_score(targets.cpu().data.numpy(), preds.cpu().data.numpy())
loss.backward()
optimizer.step()
scheduler.step()
loss_ += loss.item()
return model, loss_ / len(train_data_loader), acc_ / len(train_data_loader)
def val_fn(val_data_loader, model, device, test=False):
model.eval()
loss_ = 0
acc_ = 0
if test:
f1_ = 0
for data in tqdm(val_data_loader, total = len(val_data_loader)):
for i, j in data.items():
data[i] = j.to(device)
tag, loss = model(**data)
preds = torch.argmax(torch.softmax(tag, dim=0), dim=-1).flatten()
targets = data['target_tags'].flatten()
acc_ += accuracy_score(targets.cpu().data.numpy(), preds.cpu().data.numpy())
if test:
f1_ += f1_score(targets, preds, average="weighted")
loss_ += loss.item()
if test:
return loss_ / len(val_data_loader), acc_ / len(val_data_loader), f1_ / len(val_data_loader)
return loss_ / len(val_data_loader), acc_ / len(val_data_loader)
#Function for getparameters
def _get_hyperparameters(model, ff):
# ff: full_finetuning
if ff:
param_optimizer = list(model.named_parameters())
no_decay = ["bias", "gamma", "beta"]
optimizer_grouped_parameters = [
{
"params": [
p for n, p in param_optimizer if not any(nd in n for nd in no_decay)
],
"weight_decay_rate": 0.01,
},
{
"params": [
p for n, p in param_optimizer if any(nd in n for nd in no_decay)
],
"weight_decay_rate": 0.0,
},
]
else:
param_optimizer = list(model.classifier.named_parameters())
optimizer_grouped_parameters = [{"params": [p for n, p in param_optimizer]}]
return optimizer_grouped_parameters
def get_optimizer_scheduler(model, num_train_samples, epochs, train_batch_size, ff, lr=0.01, ):
optimizer_grouped_parameters = model._get_hyperparameters(ff if ff else FULL_FINETUNING)
optimizer = torch.optim.AdamW(optimizer_grouped_parameters, lr=lr)
num_train_steps = int(num_train_samples / train_batch_size * epochs)
scheduler = get_linear_schedule_with_warmup(
optimizer,
num_warmup_steps=0,
num_training_steps=num_train_steps
)
return optimizer, scheduler