-
Notifications
You must be signed in to change notification settings - Fork 0
/
bert+Fc.py
200 lines (167 loc) · 7.12 KB
/
bert+Fc.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
import torch as th
from transformers import AutoModel, AutoTokenizer
import torch.nn.functional as F
from utils import *
import dgl
import torch.utils.data as Data
from ignite.engine import Events, create_supervised_evaluator, create_supervised_trainer, Engine
from ignite.metrics import Accuracy, Loss
import numpy as np
import os
from datetime import datetime
from sklearn.metrics import accuracy_score
import argparse, shutil, logging
from torch.optim import lr_scheduler
from model import BertClassifier
parser = argparse.ArgumentParser()
parser.add_argument('--max_length', type=int, default=128, help='the input length for bert')
parser.add_argument('--batch_size', type=int, default=32)
parser.add_argument('--nb_epochs', type=int, default=60)
parser.add_argument('--bert_lr', type=float, default=1e-5)
parser.add_argument('--dataset', default='20ng', choices=['20ng', 'R8', 'R52', 'ohsumed', 'mr'])
parser.add_argument('--bert_init', type=str, default='roberta-base',
choices=['roberta-base', 'roberta-large', 'bert-base-uncased', 'bert-large-uncased'])
parser.add_argument('--checkpoint_dir', default=None,
help='checkpoint directory, [bert_init]_[dataset] if not specified')
args = parser.parse_args()
max_length = args.max_length
batch_size = args.batch_size
nb_epochs = args.nb_epochs
bert_lr = args.bert_lr
dataset = args.dataset
bert_init = args.bert_init
checkpoint_dir = args.checkpoint_dir
if checkpoint_dir is None:
ckpt_dir = './checkpoint/{}_{}'.format(bert_init, dataset)
else:
ckpt_dir = checkpoint_dir
os.makedirs(ckpt_dir, exist_ok=True)
shutil.copy(os.path.basename(__file__), ckpt_dir)
sh = logging.StreamHandler(sys.stdout)
sh.setFormatter(logging.Formatter('%(message)s'))
sh.setLevel(logging.INFO)
fh = logging.FileHandler(filename=os.path.join(ckpt_dir, 'training.log'), mode='w')
fh.setFormatter(logging.Formatter('%(message)s'))
fh.setLevel(logging.INFO)
logger = logging.getLogger('training logger')
logger.addHandler(sh)
logger.addHandler(fh)
logger.setLevel(logging.INFO)
cpu = th.device('cpu')
gpu = th.device('cuda:0')
logger.info('arguments:')
logger.info(str(args))
logger.info('checkpoints will be saved in {}'.format(ckpt_dir))
# Data Preprocess
adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask, train_size, test_size = load_corpus(dataset)
'''
y_train, y_val, y_test: n*c matrices
train_mask, val_mask, test_mask: n-d bool array
train_size, test_size: unused
'''
# compute number of real train/val/test/word nodes and number of classes
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] # 列数
# instantiate model according to class number
model = BertClassifier(pretrained_model=bert_init, nb_class=nb_class)
# transform one-hot label to class ID for pytorch computation
y = th.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:]
# load documents and compute input encodings
corpus_file = './data/corpus/' + dataset + '_shuffle.txt'
with open(corpus_file, 'r') as f:
text = f.read()
text = text.replace('\\', '')
text = text.split('\n')
print(text[10000])
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)
# create train/test/val datasets and dataloaders
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)
# Training
optimizer = th.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(th.long)
loss = F.cross_entropy(y_pred, y_true)
loss.backward()
optimizer.step()
train_loss = loss.item()
with th.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)
def test_step(engine, batch):
global model
with th.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)
metrics = {
'acc': Accuracy(),
'nll': Loss(th.nn.CrossEntropyLoss())
}
for n, f in metrics.items():
f.attach(evaluator, n)
@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")
th.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)