-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_bert.py
180 lines (142 loc) · 5.6 KB
/
train_bert.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
import torch
import torch.nn.functional as F
from torch.optim import lr_scheduler
import torch.utils.data as Data
from transformers import AutoModel, AutoTokenizer
from utils import *
import dgl
import numpy as np
from datetime import datetime
from sklearn.metrics import accuracy_score
import os
import sys
import argparse
import shutil
import logging
from ignite.engine import Events, create_supervised_evaluator, create_supervised_trainer, Engine
from ignite.metrics import Accuracy, Loss
from ignite.contrib.handlers.tqdm_logger import ProgressBar
from model import BertClassifier
max_length = 128
batch_size = 128
nb_epochs = 30
bert_lr = 1e-4
dataset = "wellness"
ckpt_dir = './checkpoint/{}'.format(dataset)
args = [max_length, batch_size, nb_epochs, dataset, bert_lr]
os.makedirs(ckpt_dir, exist_ok=True)
streamhandle = logging.StreamHandler(sys.stdout)
streamhandle.setFormatter(logging.Formatter('%(message)s'))
streamhandle.setLevel(logging.INFO)
filehandle = logging.FileHandler(filename=os.path.join(ckpt_dir, 'training.log'), mode='w')
filehandle.setFormatter(logging.Formatter('%(message)s'))
filehandle.setLevel(logging.INFO)
logger = logging.getLogger('training logger')
logger.addHandler(streamhandle)
logger.addHandler(filehandle)
logger.setLevel(logging.INFO)
cpu = torch.device('cpu')
gpu = torch.device('cuda:1')
logger.info('params:')
logger.info(str(args))
logger.info('checkpoints path: {}'.format(ckpt_dir))
adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask, train_size, test_size = load_corpus(dataset)
nb_node = adj.shape[0]
nb_train, nb_val, nb_test = train_mask.sum(), val_mask.sum(), test_mask.sum()
nb_word = nb_node - nb_train - nb_val - nb_test
nb_class = y_train.shape[1]
model = BertClassifier(nb_class=nb_class)
y = torch.LongTensor((y_train + y_val +y_test).argmax(axis=1))
label = {}
label['train'], label['val'], label['test'] = y[:nb_train], y[nb_train:nb_train+nb_val], y[-nb_test:]
corpus_file = './data/corpus/'+dataset+'_shuffle.txt'
with open(corpus_file, 'r', encoding="utf-8") as f:
text = f.read()
text = text.replace('\\', '')
text = text.split('\n')
def encode_input(text, tokenizer):
input = tokenizer(text, max_length=max_length, truncation=True, padding=True, return_tensors='pt')
return input.input_ids, input.attention_mask
input_ids, attention_mask = {}, {}
input_ids_, attention_mask_ = encode_input(text, model.tokenizer)
input_ids['train'], input_ids['val'], input_ids['test'] = input_ids_[:nb_train], input_ids_[nb_train:nb_train+nb_val], input_ids_[-nb_test:]
attention_mask['train'], attention_mask['val'], attention_mask['test'] = attention_mask_[:nb_train], attention_mask_[nb_train:nb_train+nb_val], attention_mask_[-nb_test:]
datasets = {}
loader = {}
for split in ['train', 'val', 'test']:
datasets[split] = Data.TensorDataset(input_ids[split], attention_mask[split], label[split])
loader[split] = Data.DataLoader(datasets[split], batch_size=batch_size, shuffle=True)
optimizer = torch.optim.Adam(model.parameters(), lr=bert_lr)
scheduler = lr_scheduler.MultiStepLR(optimizer, milestones=[30], gamma=0.1)
def train_step(engine, batch):
global model, optimizer
model.train()
model = model.to(gpu)
optimizer.zero_grad()
(input_ids, attention_mask, label) = [x.to(gpu) for x in batch]
optimizer.zero_grad()
y_pred = model(input_ids, attention_mask)
y_true = label.type(torch.long)
loss = F.cross_entropy(y_pred, y_true)
loss.backward()
optimizer.step()
train_loss = loss.item()
with torch.no_grad():
y_true = y_true.detach().cpu()
y_pred = y_pred.argmax(axis=1).detach().cpu()
train_acc = accuracy_score(y_true, y_pred)
return train_loss, train_acc
trainer = Engine(train_step)
pbar = ProgressBar()
pbar.attach(trainer)
def test_step(engine, batch):
global model
with torch.no_grad():
model.eval()
model = model.to(gpu)
(input_ids, attention_mask, label) = [x.to(gpu) for x in batch]
optimizer.zero_grad()
y_pred = model(input_ids, attention_mask)
y_true = label
return y_pred, y_true
evaluator = Engine(test_step)
eval_pbar = ProgressBar()
eval_pbar.attach(evaluator)
metrics={
'acc': Accuracy(),
'nll': Loss(torch.nn.CrossEntropyLoss())
}
for name, function in metrics.items():
function.attach(evaluator, name)
@trainer.on(Events.EPOCH_COMPLETED)
def log_training_results(trainer):
evaluator.run(loader['train'])
metrics = evaluator.state.metrics
train_acc, train_nll = metrics["acc"], metrics["nll"]
evaluator.run(loader['val'])
metrics = evaluator.state.metrics
val_acc, val_nll = metrics["acc"], metrics["nll"]
evaluator.run(loader['test'])
metrics = evaluator.state.metrics
test_acc, test_nll = metrics["acc"], metrics["nll"]
logger.info(
"\rEpoch: {} Train acc: {:.4f} loss: {:.4f} Val acc: {:.4f} loss: {:.4f} Test acc: {:.4f} loss: {:.4f}"
.format(trainer.state.epoch, train_acc, train_nll, val_acc, val_nll, test_acc, test_nll)
)
if val_acc > log_training_results.best_val_acc:
logger.info("New checkpoint")
torch.save(
{
'bert_model': model.bert_model.state_dict(),
'classifier': model.classifier.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': trainer.state.epoch,
},
os.path.join(
ckpt_dir, 'checkpoint.pth'
)
)
log_training_results.best_val_acc = val_acc
scheduler.step()
log_training_results.best_val_acc = 0
trainer.run(loader['train'], max_epochs=nb_epochs)