forked from neubig/minbert-assignment
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathclassifier.py
281 lines (227 loc) · 10.4 KB
/
classifier.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import time, random, numpy as np, argparse, sys, re, os
import pandas as pd
from types import SimpleNamespace
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from sklearn.metrics import classification_report, f1_score, recall_score, accuracy_score
# change it with respect to the original model
from tokenizer import BertTokenizer
from bert import BertModel
from optimizer import AdamW
from tqdm import tqdm
TQDM_DISABLE=False
# fix the random seed
def seed_everything(seed=11711):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
class BertSentClassifier(torch.nn.Module):
def __init__(self, config):
super(BertSentClassifier, self).__init__()
self.num_labels = config.num_labels
self.bert = BertModel.from_pretrained('bert-base-uncased')
self.output_dropout_prob = self.bert.config.hidden_dropout_prob
# freeze mode does not require updating bert paramters.
for param in self.bert.parameters():
if config.option == 'freeze':
param.requires_grad = False
elif config.option == 'finetune':
param.requires_grad = True
# todo
raise NotImplementedError
def forward(self, input_ids, attention_mask):
# todo
# the final bert contextual embedding is the hidden state of the [CLS] token (the first token)
raise NotImplementedError
# create a custom Dataset Class to be used for the dataloader
class BertDataset(Dataset):
def __init__(self, dataset, args):
self.dataset = dataset
self.p = args
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
def __len__(self):
return len(self.dataset)
def __getitem__(self, idx):
ele = self.dataset[idx]
return ele
def pad_data(self, data):
sents = [x[0] for x in data]
labels = [x[1] for x in data]
encoding = self.tokenizer(sents, return_tensors='pt', padding=True, truncation=True)
token_ids = torch.LongTensor(encoding['input_ids'])
attention_mask = torch.LongTensor(encoding['attention_mask'])
token_type_ids = torch.LongTensor(encoding['token_type_ids'])
labels = torch.LongTensor(labels)
return token_ids, token_type_ids, attention_mask, labels, sents
def collate_fn(self, all_data):
token_ids, token_type_ids, attention_mask, labels, sents = self.pad_data(all_data)
batched_data = {
'token_ids': token_ids,
'token_type_ids': token_type_ids,
'attention_mask': attention_mask,
'labels': labels,
'sents': sents,
}
return batched_data
# create the data which is a list of (sentence, label, token for the labels)
def create_data(filename, flag='train'):
# specify the tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
num_labels = {}
data = []
df = pd.read_csv(filename, index_col=None)
labels = df['label']
orig_sents = df['data']
for label, orig_sent in zip(labels, orig_sents):
sent = orig_sent.lower().strip()
tokens = tokenizer.tokenize("[CLS] " + sent + " [SEP]")
label = int(label)
if label not in num_labels:
num_labels[label] = len(num_labels)
data.append((sent, label, tokens))
print(f"load {len(data)} data from {filename}")
if flag == 'train':
return data, len(num_labels)
else:
return data
# perform model evaluation in terms of the accuracy and f1 score.
def model_eval(dataloader, model, device):
model.eval() # switch to eval model, will turn off randomness like dropout
y_true = []
y_pred = []
sents = []
for step, batch in enumerate(tqdm(dataloader, desc=f'eval', disable=TQDM_DISABLE)):
b_ids, b_type_ids, b_mask, b_labels, b_sents = batch['token_ids'], batch['token_type_ids'], \
batch['attention_mask'], batch['labels'], batch['sents']
b_ids = b_ids.to(device)
b_mask = b_mask.to(device)
logits = model(b_ids, b_mask)
logits = logits.detach().cpu().numpy()
preds = np.argmax(logits, axis=1).flatten()
b_labels = b_labels.flatten()
y_true.extend(b_labels)
y_pred.extend(preds)
sents.extend(b_sents)
f1 = f1_score(y_true, y_pred, average='macro')
acc = accuracy_score(y_true, y_pred)
return acc, f1, y_pred, y_true, sents
def save_model(model, optimizer, args, config, filepath):
save_info = {
'model': model.state_dict(),
'optim': optimizer.state_dict(),
'args': args,
'model_config': config,
'system_rng': random.getstate(),
'numpy_rng': np.random.get_state(),
'torch_rng': torch.random.get_rng_state(),
}
torch.save(save_info, filepath)
print(f"save the model to {filepath}")
def train(args):
device = torch.device('cuda') if args.use_gpu else torch.device('cpu')
#### Load data
# create the data and its corresponding datasets and dataloader
train_data, num_labels = create_data(args.train, 'train')
dev_data = create_data(args.dev, 'valid')
train_dataset = BertDataset(train_data, args)
dev_dataset = BertDataset(dev_data, args)
train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=args.batch_size,
collate_fn=train_dataset.collate_fn)
dev_dataloader = DataLoader(dev_dataset, shuffle=False, batch_size=args.batch_size,
collate_fn=dev_dataset.collate_fn)
#### Init model
config = {'hidden_dropout_prob': args.hidden_dropout_prob,
'num_labels': num_labels,
'hidden_size': 768,
'data_dir': '.',
'option': args.option}
config = SimpleNamespace(**config)
# initialize the Senetence Classification Model
model = BertSentClassifier(config)
model = model.to(device)
lr = args.lr
## specify the optimizer
optimizer = AdamW(model.parameters(), lr=lr)
best_dev_acc = 0
## run for the specified number of epochs
for epoch in range(args.epochs):
model.train()
train_loss = 0
num_batches = 0
for step, batch in enumerate(tqdm(train_dataloader, desc=f'train-{epoch}', disable=TQDM_DISABLE)):
b_ids, b_type_ids, b_mask, b_labels, b_sents = batch['token_ids'], batch['token_type_ids'], batch[
'attention_mask'], batch['labels'], batch['sents']
b_ids = b_ids.to(device)
b_mask = b_mask.to(device)
b_labels = b_labels.to(device)
optimizer.zero_grad()
logits = model(b_ids, b_mask)
loss = F.nll_loss(logits, b_labels.view(-1), reduction='sum') / args.batch_size
loss.backward()
optimizer.step()
train_loss += loss.item()
num_batches += 1
train_loss = train_loss / (num_batches)
train_acc, train_f1, *_ = model_eval(train_dataloader, model, device)
dev_acc, dev_f1, *_ = model_eval(dev_dataloader, model, device)
if dev_acc > best_dev_acc:
best_dev_acc = dev_acc
save_model(model, optimizer, args, config, args.filepath)
print(f"epoch {epoch}: train loss :: {train_loss :.3f}, train acc :: {train_acc :.3f}, dev acc :: {dev_acc :.3f}")
def test(args):
with torch.no_grad():
device = torch.device('cuda') if args.use_gpu else torch.device('cpu')
saved = torch.load(args.filepath)
config = saved['model_config']
model = BertSentClassifier(config)
model.load_state_dict(saved['model'])
model = model.to(device)
print(f"load model from {args.filepath}")
dev_data = create_data(args.dev, 'valid')
dev_dataset = BertDataset(dev_data, args)
# DO NOT SHUFFLE
dev_dataloader = DataLoader(dev_dataset, shuffle=False, batch_size=args.batch_size, collate_fn=dev_dataset.collate_fn)
test_data = create_data(args.test, 'test')
test_dataset = BertDataset(test_data, args)
# DO NOT SHUFFLE
test_dataloader = DataLoader(test_dataset, shuffle=False, batch_size=args.batch_size, collate_fn=test_dataset.collate_fn)
dev_acc, dev_f1, dev_pred, dev_true, dev_sents = model_eval(dev_dataloader, model, device)
test_acc, test_f1, test_pred, test_true, test_sents = model_eval(test_dataloader, model, device)
print(f"dev acc :: {dev_acc :.3f}")
df = pd.DataFrame({'ID': list(range(len(dev_sents))), 'label': dev_pred})
df.to_csv(args.dev_out, index=None)
print(f"test acc :: {test_acc :.3f}")
df = pd.DataFrame({'ID': list(range(len(test_sents))), 'label': test_pred})
df.to_csv(args.test_out, index=None)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("--train", type=str, default="data/cfimdb-train.csv")
parser.add_argument("--dev", type=str, default="data/cfimdb-dev.csv")
parser.add_argument("--test", type=str, default="data/cfimdb-test.csv")
parser.add_argument("--seed", type=int, default=11711)
parser.add_argument("--epochs", type=int, default=10)
parser.add_argument("--option", type=str,
help='freeze: the BERT parameters are frozen; finetune: BERT parameters are updated',
choices=('freeze', 'finetune'), default="freeze")
parser.add_argument("--use_gpu", action='store_true')
parser.add_argument("--dev_out", type=str, default="cfimdb-dev-output.csv")
parser.add_argument("--test_out", type=str, default="cfimdb-test-output.csv")
# hyper parameters
parser.add_argument("--batch_size", help='sst: 64, cfimdb: 8 can fit a 12GB GPU', type=int, default=8)
parser.add_argument("--hidden_dropout_prob", type=float, default=0.3)
parser.add_argument("--lr", type=float, help="learning rate, default lr for 'freeze': 1e-3, 'finetune': 1e-5",
default=1e-5)
args = parser.parse_args()
print(f"args: {vars(args)}")
return args
if __name__ == "__main__":
args = get_args()
args.filepath = f'{args.option}-{args.epochs}-{args.lr}.pt' # save path
seed_everything(args.seed) # fix the seed for reproducibility
train(args)
test(args)