This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtrain.py
218 lines (170 loc) · 6.68 KB
/
train.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
"""
Script to train model.
"""
import os
import time
import bdrk
import numpy as np
import pandas as pd
import torch
from torch.nn import functional as F
from torch.utils.data import DataLoader
from sklearn import metrics
from sklearn.model_selection import train_test_split
from utils.data import TextDataset
from utils.model import load_tokenizer, load_model
BATCH_SIZE = int(os.getenv("BATCH_SIZE", "8"))
EPOCHS = int(os.getenv("EPOCHS", "10"))
# pylint: disable=too-few-public-methods
class CFG:
"""Configuration."""
max_len = 256
batch_size = BATCH_SIZE
epochs = EPOCHS
lr = 5e-6
patience = 5
n_classes = 2
finetuned_model_path = "/artefact/finetuned_model.pth"
# pylint: disable=too-many-locals
def train_fn(cfg, model, train_loader, valid_loader, device):
"""Train function."""
optimizer = torch.optim.Adam(model.parameters(), lr=cfg.lr)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
optimizer, "min", factor=0.5, patience=2, verbose=True, eps=1e-6)
best_loss = np.inf
counter = 0
for epoch in range(cfg.epochs):
start_time = time.time()
model.train()
train_loss = 0.
y_train = list()
pred_train = list()
optimizer.zero_grad()
for data in train_loader:
ids = data["ids"].to(device)
mask = data["mask"].to(device)
targets = data["targets"].to(device)
outputs = model(ids, attention_mask=mask, labels=targets)
loss, logits = outputs[:2]
loss.backward()
optimizer.step()
optimizer.zero_grad()
train_loss += loss.item() / len(train_loader)
y_train.append(targets.cpu().numpy())
pred_train.append(logits.detach().cpu().numpy().argmax(axis=1))
y_train = np.concatenate(y_train)
pred_train = np.concatenate(pred_train)
train_acc = metrics.accuracy_score(y_train, pred_train)
model.eval()
valid_loss = 0.
y_valid = list()
pred_valid = list()
for data in valid_loader:
ids = data["ids"].to(device)
mask = data["mask"].to(device)
targets = data["targets"].to(device)
with torch.no_grad():
outputs = model(ids, attention_mask=mask, labels=targets)
loss, logits = outputs[:2]
valid_loss += loss.item() / len(valid_loader)
y_valid.append(targets.cpu().numpy())
pred_valid.append(logits.cpu().numpy().argmax(axis=1))
scheduler.step(valid_loss)
y_valid = np.concatenate(y_valid)
pred_valid = np.concatenate(pred_valid)
valid_acc = metrics.accuracy_score(y_valid, pred_valid)
print(f"Epoch {epoch + 1}/{cfg.epochs}: elapsed time: {time.time() - start_time:.0f}s\n"
f" loss: {train_loss:.4f} train_acc: {train_acc:.4f}"
f" - valid_loss: {valid_loss:.4f} valid_acc: {valid_acc:.4f}")
if valid_loss < best_loss:
print(f"Epoch {epoch + 1}: valid_loss improved from {best_loss:.5f} to {valid_loss:.5f}, "
f"saving model to {cfg.finetuned_model_path}")
best_loss = valid_loss
counter = 0
torch.save(model.state_dict(), cfg.finetuned_model_path)
else:
print(f"Epoch {epoch + 1}: valid_loss did not improve from {best_loss:.5f}")
counter += 1
if counter == cfg.patience:
break
def predict(model, data_loader, device):
"""Predict function."""
model.eval()
y_true = list()
y_prob = list()
for data in data_loader:
ids = data["ids"].to(device)
mask = data["mask"].to(device)
targets = data["targets"].to(device)
with torch.no_grad():
logits = model(ids, attention_mask=mask)[0]
y_true.append(targets.cpu().numpy())
y_prob.append(F.softmax(logits, dim=1).cpu().numpy())
y_true = np.concatenate(y_true)
y_prob = np.concatenate(y_prob)
return y_true, y_prob
def compute_log_metrics(y_val, y_prob, y_pred):
"""Compute and log metrics."""
acc = metrics.accuracy_score(y_val, y_pred)
precision = metrics.precision_score(y_val, y_pred)
recall = metrics.recall_score(y_val, y_pred)
f1_score = metrics.f1_score(y_val, y_pred)
roc_auc = metrics.roc_auc_score(y_val, y_prob)
avg_prc = metrics.average_precision_score(y_val, y_prob)
print(f" Accuracy = {acc:.6f}")
print(f" Precision = {precision:.6f}")
print(f" Recall = {recall:.6f}")
print(f" F1 score = {f1_score:.6f}")
print(f" ROC AUC = {roc_auc:.6f}")
print(f" Average precision = {avg_prc:.6f}")
# Log metrics
bdrk.log_metrics(
{
"Accuracy": acc,
"Precision": precision,
"Recall": recall,
"F1 score": f1_score,
"ROC AUC": roc_auc,
"Avg precision": avg_prc,
}
)
bdrk.log_binary_classifier_metrics(
y_val.astype(int).tolist(), y_prob.flatten().tolist()
)
# pylint: disable=too-many-locals
def train():
"""Train"""
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"\nDevice: {device}")
if torch.cuda.device_count() > 0:
print(f" Found GPU at: {torch.cuda.get_device_name(0)}")
tokenizer = load_tokenizer()
print("\nLoad data")
data = pd.read_csv("data/adhoc_sentences.tsv", sep="\t")
print(" data shape =", data.shape)
print("\nSplit train and validation data")
train_df, valid_df = train_test_split(data, test_size=0.2, random_state=0)
print(f" Train Dataset: {train_df.shape}")
print(f" Valid Dataset: {valid_df.shape}")
train_data = TextDataset(
train_df["sentence"].tolist(), tokenizer, CFG.max_len, labels=train_df["sentiment"].tolist())
train_loader = DataLoader(
train_data, batch_size=CFG.batch_size, shuffle=True, num_workers=0)
valid_data = TextDataset(
valid_df["sentence"].tolist(), tokenizer, CFG.max_len, labels=valid_df["sentiment"].tolist())
valid_loader = DataLoader(
valid_data, batch_size=CFG.batch_size, shuffle=False, num_workers=0)
print("\nTrain model")
model = load_model(CFG.n_classes)
model.to(device)
train_fn(CFG, model, train_loader, valid_loader, device)
print("\nEvaluate")
trained_model = load_model(CFG.n_classes, CFG.finetuned_model_path, device)
trained_model.to(device)
y_true, y_prob = predict(trained_model, valid_loader, device)
y_pred = y_prob.argmax(axis=1)
compute_log_metrics(y_true, y_prob[:, 1], y_pred)
if __name__ == "__main__":
bdrk.init()
with bdrk.start_run():
train()