-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathner_train.py
More file actions
executable file
·166 lines (135 loc) · 5.83 KB
/
ner_train.py
File metadata and controls
executable file
·166 lines (135 loc) · 5.83 KB
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
import yaml
import os
import argparse
import torch
import wandb
import numpy as np
import pandas as pd
import torch.nn as nn
import torch.optim as optim
import tqdm
from models.BertSequence import build_bert_sequence_model, load_bert_sequence_model
from dataloaders.ner_conll2003 import conll2003_get_dataloader
from sklearn.metrics import classification_report
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
from dataloaders.ner_conll2003 import get_labels
def train(model, dataloader, optimizer, mode='train'):
epoch_loss = 0.0
preds, labs = [], []
if mode == 'train':
model.train()
optimizer.zero_grad()
else:
model.eval()
for batch in tqdm.tqdm(dataloader):
ids = batch['input_ids'].to(device, dtype = torch.long)
mask = batch['attention_mask'].to(device, dtype = torch.long)
labels = batch['labels'].to(device, dtype = torch.long)
loss, tr_logits = model(input_ids=ids, attention_mask=mask, labels=labels)
# gradient clipping
# torch.nn.utils.clip_grad_norm_(
# parameters=model.parameters(), max_norm=MAX_GRAD_NORM
# )
# backward pass
if mode == 'train':
loss.backward()
optimizer.step()
optimizer.zero_grad()
epoch_loss += loss.item()
# compute training accuracy
flattened_targets = labels.view(-1) # shape (batch_size * seq_len,)
active_logits = tr_logits.view(-1, model.module.num_labels) # shape (batch_size * seq_len, num_labels)
flattened_predictions = torch.argmax(active_logits, axis=1) # shape (batch_size * seq_len,)
# only compute accuracy at active labels
active_accuracy = labels.view(-1) != -100 # shape (batch_size, seq_len)
labels = torch.masked_select(flattened_targets, active_accuracy)
predictions = torch.masked_select(flattened_predictions, active_accuracy)
labels = labels.cpu().detach().numpy()
predictions = predictions.cpu().detach().numpy()
preds.extend(predictions)
labs.extend(labels)
# print(classification_report(labs, preds))
metrics = {"{}_acc".format(mode): accuracy_score(labs, preds),
"{}_f1".format(mode): f1_score(labs, preds, average="weighted"),
"{}_precision".format(mode): precision_score(labs, preds, average="weighted"),
"{}_recall".format(mode): recall_score(labs, preds, average="weighted"),
"{}_loss".format(mode): epoch_loss/len(dataloader)}
return metrics, preds, labs
if __name__ == '__main__':
train_file = os.path.join('data', 'conll2003', 'ner', 'noise_BItags_30sen_30tok.txt')
val_file = os.path.join('data', 'conll2003', 'ner', 'valid.txt')
dir_checkpoint = os.path.join('checkpoints', 'conll2003', 'SEED4_NER_CoNLL2003_noise_BItags_30sen_30tok')
run_name = 'SEED4_NER_CoNLL2003_noise_BItags_30sen_30tok'
batch_size = 64
device = 'cuda'
num_labels = len(get_labels())
num_epochs = 20
SEED = 4
name_project = "NER-CoNLL2003"
learning_rate = 5e-5
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
if not os.path.isdir(dir_checkpoint):
print(f"Directory {dir_checkpoint} does not exist")
os.makedirs(dir_checkpoint)
print(f"Created {dir_checkpoint}")
if not torch.cuda.is_available() and device == 'cuda':
print('Your device don\'t have cuda, please check or select cpu and retraining')
exit(0)
if device == 'cuda':
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
# Load data
train_dataloader = conll2003_get_dataloader(
file_name=train_file,
batch_size=batch_size,
mode='train',
num_workers=32
)
val_dataloader = conll2003_get_dataloader(
file_name=val_file,
batch_size=batch_size,
mode='test',
num_workers=32
)
# Build model
model = build_bert_sequence_model(num_labels=num_labels,device=device)
optimizer = optim.AdamW(model.parameters(), lr=learning_rate, betas=(0.9, 0.999))
# config best
wandb.init(project=name_project, name= run_name)
wandb.config = {
"seed": SEED,
"train_file": train_file,
"val_file": val_file,
"num_labels": num_labels,
"learning_rate": learning_rate,
"epochs": num_epochs,
"batch_size": batch_size
}
best_valid_f1 = (-1.0) * float("Inf")
best_epoch = 0
for epoch in range(num_epochs):
print(f"Epoch: {epoch+1}/{num_epochs}")
train_metrics, train_preds, train_labs = train(model, train_dataloader, optimizer, mode='train')
val_metrics, val_preds, val_labs = train(model, val_dataloader, optimizer, mode='val')
print("Train metricc:", train_metrics)
print("Val metrics", val_metrics)
torch.save(model.state_dict(), os.path.join(dir_checkpoint, f'epoch_{epoch+1}.pt'))
if best_valid_f1 < val_metrics["val_f1"]:
best_valid_f1 = val_metrics["val_f1"]
torch.save(model.state_dict(), os.path.join(dir_checkpoint, f'best.pt'))
f = open(os.path.join(dir_checkpoint, f'best_epoch.txt'), "w")
f.write(str(epoch+1))
f.close()
print(f"Best model saved at epoch {epoch+1}")
print("Classification Report on Train Dataset:")
print(classification_report(train_labs, train_preds))
print("Classification Report on Valid Dataset:")
print(classification_report(val_labs, val_preds))
train_metrics.update(val_metrics)
wandb.log(train_metrics)
wandb.watch(model)
print(f'Finished training')