Spaces:
Running
Running
File size: 22,350 Bytes
6a34fd4 |
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
import torch
import torch.nn as nn
from torch.nn import functional as F
from torch.optim import Adam, SGD
from torch.utils.data import DataLoader
import pickle
from bert import BERT
from seq_model import BERTSM
from classifier_model import BERTForClassification
from optim_schedule import ScheduledOptim
import tqdm
import sys
import numpy as np
import visualization
from sklearn.metrics import precision_score, recall_score, f1_score
class ECE(nn.Module):
def __init__(self, n_bins=15):
"""
n_bins (int): number of confidence interval bins
"""
super(ECE, self).__init__()
bin_boundaries = torch.linspace(0, 1, n_bins + 1)
self.bin_lowers = bin_boundaries[:-1]
self.bin_uppers = bin_boundaries[1:]
def forward(self, logits, labels):
softmaxes = F.softmax(logits, dim=1)
confidences, predictions = torch.max(softmaxes, 1)
labels = torch.argmax(labels,1)
accuracies = predictions.eq(labels)
ece = torch.zeros(1, device=logits.device)
for bin_lower, bin_upper in zip(self.bin_lowers, self.bin_uppers):
# Calculated |confidence - accuracy| in each bin
in_bin = confidences.gt(bin_lower.item()) * confidences.le(bin_upper.item())
prop_in_bin = in_bin.float().mean()
if prop_in_bin.item() > 0:
accuracy_in_bin = accuracies[in_bin].float().mean()
avg_confidence_in_bin = confidences[in_bin].mean()
ece += torch.abs(avg_confidence_in_bin - accuracy_in_bin) * prop_in_bin
return ece
def accurate_nb(preds, labels):
pred_flat = np.argmax(preds, axis=1).flatten()
labels_flat = np.argmax(labels, axis=1).flatten()
labels_flat = labels.flatten()
return np.sum(pred_flat == labels_flat)
class BERTTrainer:
"""
# Sequence..
BERTTrainer make the pretrained BERT model with two LM training method.
1. Masked Language Model : 3.3.1 Task #1: Masked LM
"""
def __init__(self, bert: BERT, vocab_size: int,
train_dataloader: DataLoader, test_dataloader: DataLoader = None,
lr: float = 1e-4, betas=(0.9, 0.999), weight_decay: float = 0.01, warmup_steps=10000,
with_cuda: bool = True, cuda_devices=None, log_freq: int = 10, same_student_prediction = False,
workspace_name=None):
"""
:param bert: BERT model which you want to train
:param vocab_size: total word vocab size
:param train_dataloader: train dataset data loader
:param test_dataloader: test dataset data loader [can be None]
:param lr: learning rate of optimizer
:param betas: Adam optimizer betas
:param weight_decay: Adam optimizer weight decay param
:param with_cuda: traning with cuda
:param log_freq: logging frequency of the batch iteration
"""
# Setup cuda device for BERT training, argument -c, --cuda should be true
cuda_condition = torch.cuda.is_available() and with_cuda
self.device = torch.device("cuda:0" if cuda_condition else "cpu")
print("Device used = ", self.device)
# This BERT model will be saved every epoch
self.bert = bert
# Initialize the BERT Language Model, with BERT model
self.model = BERTSM(bert, vocab_size).to(self.device)
# Distributed GPU training if CUDA can detect more than 1 GPU
if with_cuda and torch.cuda.device_count() > 1:
print("Using %d GPUS for BERT" % torch.cuda.device_count())
self.model = nn.DataParallel(self.model, device_ids=cuda_devices)
# Setting the train and test data loader
self.train_data = train_dataloader
self.test_data = test_dataloader
# Setting the Adam optimizer with hyper-param
self.optim = Adam(self.model.parameters(), lr=lr, betas=betas, weight_decay=weight_decay)
self.optim_schedule = ScheduledOptim(self.optim, self.bert.hidden, n_warmup_steps=warmup_steps)
# Using Negative Log Likelihood Loss function for predicting the masked_token
self.criterion = nn.NLLLoss(ignore_index=0)
self.log_freq = log_freq
self.same_student_prediction = same_student_prediction
self.workspace_name = workspace_name
self.save_model = False
self.avg_loss = 10000
print("Total Parameters:", sum([p.nelement() for p in self.model.parameters()]))
def train(self, epoch):
self.iteration(epoch, self.train_data)
def test(self, epoch):
self.iteration(epoch, self.test_data, train=False)
def iteration(self, epoch, data_loader, train=True):
"""
loop over the data_loader for training or testing
if on train status, backward operation is activated
and also auto save the model every peoch
:param epoch: current epoch index
:param data_loader: torch.utils.data.DataLoader for iteration
:param train: boolean value of is train or test
:return: None
"""
str_code = "train" if train else "test"
code = "masked_prediction" if self.same_student_prediction else "masked"
self.log_file = f"{self.workspace_name}/logs/{code}/log_{str_code}_pretrained.txt"
bert_hidden_representations = []
if epoch == 0:
f = open(self.log_file, 'w')
f.close()
if not train:
self.avg_loss = 10000
# Setting the tqdm progress bar
data_iter = tqdm.tqdm(enumerate(data_loader),
desc="EP_%s:%d" % (str_code, epoch),
total=len(data_loader),
bar_format="{l_bar}{r_bar}")
avg_loss_mask = 0.0
total_correct_mask = 0
total_element_mask = 0
avg_loss_pred = 0.0
total_correct_pred = 0
total_element_pred = 0
avg_loss = 0.0
with open(self.log_file, 'a') as f:
sys.stdout = f
for i, data in data_iter:
# 0. batch_data will be sent into the device(GPU or cpu)
data = {key: value.to(self.device) for key, value in data.items()}
# 1. forward the next_sentence_prediction and masked_lm model
# next_sent_output, mask_lm_output = self.model.forward(data["bert_input"], data["segment_label"])
if self.same_student_prediction:
bert_hidden_rep, mask_lm_output, same_student_output = self.model.forward(data["bert_input"], data["segment_label"], self.same_student_prediction)
else:
bert_hidden_rep, mask_lm_output = self.model.forward(data["bert_input"], data["segment_label"], self.same_student_prediction)
embeddings = [h for h in bert_hidden_rep.cpu().detach().numpy()]
bert_hidden_representations.extend(embeddings)
# 2-2. NLLLoss of predicting masked token word
mask_loss = self.criterion(mask_lm_output.transpose(1, 2), data["bert_label"])
# 2-3. Adding next_loss and mask_loss : 3.4 Pre-training Procedure
if self.same_student_prediction:
# 2-1. NLL(negative log likelihood) loss of is_next classification result
same_student_loss = self.criterion(same_student_output, data["is_same_student"])
loss = same_student_loss + mask_loss
else:
loss = mask_loss
# 3. backward and optimization only in train
if train:
self.optim_schedule.zero_grad()
loss.backward()
self.optim_schedule.step_and_update_lr()
non_zero_mask = (data["bert_label"] != 0).float()
predictions = torch.argmax(mask_lm_output, dim=-1)
predicted_masked = predictions*non_zero_mask
mask_correct = ((data["bert_label"] == predicted_masked)*non_zero_mask).sum().item()
avg_loss_mask += loss.item()
total_correct_mask += mask_correct
total_element_mask += non_zero_mask.sum().item()
post_fix = {
"epoch": epoch,
"iter": i,
"avg_loss": avg_loss_mask / (i + 1),
"avg_acc_mask": total_correct_mask / total_element_mask * 100,
"loss": loss.item()
}
# next sentence prediction accuracy
if self.same_student_prediction:
correct = same_student_output.argmax(dim=-1).eq(data["is_same_student"]).sum().item()
avg_loss_pred += loss.item()
total_correct_pred += correct
total_element_pred += data["is_same_student"].nelement()
# correct = next_sent_output.argmax(dim=-1).eq(data["is_next"]).sum().item()
post_fix["avg_loss"] = avg_loss_pred / (i + 1)
post_fix["avg_acc_pred"] = total_correct_pred / total_element_pred * 100
post_fix["loss"] = loss.item()
avg_loss +=loss.item()
if i % self.log_freq == 0:
data_iter.write(str(post_fix))
# if not train and epoch > 20 :
# pickle.dump(mask_lm_output.cpu().detach().numpy(), open(f"logs/mask/mask_out_e{epoch}_{i}.pkl","wb"))
# pickle.dump(data["bert_label"].cpu().detach().numpy(), open(f"logs/mask/label_e{epoch}_{i}.pkl","wb"))
final_msg = {
"epoch": f"EP{epoch}_{str_code}",
"avg_loss": avg_loss / len(data_iter),
"total_masked_acc": total_correct_mask * 100.0 / total_element_mask
}
if self.same_student_prediction:
final_msg["total_prediction_acc"] = total_correct_pred * 100.0 / total_element_pred
print(final_msg)
# print("EP%d_%s, avg_loss=" % (epoch, str_code), avg_loss / len(data_iter), "total_masked_acc=", total_correct_mask * 100.0 / total_element_mask, "total_prediction_acc=", total_correct_pred * 100.0 / total_element_pred)
# else:
# print("EP%d_%s, avg_loss=" % (epoch, str_code), avg_loss / len(data_iter), "total_masked_acc=", total_correct_mask * 100.0 / total_element_mask)
# print("EP%d_%s, " % (epoch, str_code))
f.close()
sys.stdout = sys.__stdout__
self.save_model = False
if self.avg_loss > (avg_loss / len(data_iter)):
self.save_model = True
self.avg_loss = (avg_loss / len(data_iter))
# pickle.dump(bert_hidden_representations, open(f"embeddings/{code}/{str_code}_embeddings_{epoch}.pkl","wb"))
def save(self, epoch, file_path="output/bert_trained.model"):
"""
Saving the current BERT model on file_path
:param epoch: current epoch number
:param file_path: model output path which gonna be file_path+"ep%d" % epoch
:return: final_output_path
"""
output_path = file_path + ".ep%d" % epoch
torch.save(self.bert.cpu(), output_path)
self.bert.to(self.device)
print("EP:%d Model Saved on:" % epoch, output_path)
return output_path
class BERTFineTuneTrainer:
def __init__(self, bert: BERT, vocab_size: int,
train_dataloader: DataLoader, test_dataloader: DataLoader = None,
lr: float = 1e-4, betas=(0.9, 0.999), weight_decay: float = 0.01, warmup_steps=10000,
with_cuda: bool = True, cuda_devices=None, log_freq: int = 10, workspace_name=None, num_labels=2):
"""
:param bert: BERT model which you want to train
:param vocab_size: total word vocab size
:param train_dataloader: train dataset data loader
:param test_dataloader: test dataset data loader [can be None]
:param lr: learning rate of optimizer
:param betas: Adam optimizer betas
:param weight_decay: Adam optimizer weight decay param
:param with_cuda: traning with cuda
:param log_freq: logging frequency of the batch iteration
"""
# Setup cuda device for BERT training, argument -c, --cuda should be true
cuda_condition = torch.cuda.is_available() and with_cuda
self.device = torch.device("cuda:0" if cuda_condition else "cpu")
print("Device used = ", self.device)
# This BERT model will be saved every epoch
self.bert = bert
# for param in self.bert.parameters():
# param.requires_grad = False
# Initialize the BERT Language Model, with BERT model
self.model = BERTForClassification(self.bert, vocab_size, num_labels).to(self.device)
# Distributed GPU training if CUDA can detect more than 1 GPU
if with_cuda and torch.cuda.device_count() > 1:
print("Using %d GPUS for BERT" % torch.cuda.device_count())
self.model = nn.DataParallel(self.model, device_ids=cuda_devices)
# Setting the train and test data loader
self.train_data = train_dataloader
self.test_data = test_dataloader
self.optim = Adam(self.model.parameters(), lr=lr, weight_decay=weight_decay, eps=1e-9)
# self.scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'min', patience=2, factor=0.1)
if num_labels == 1:
self.criterion = nn.MSELoss()
elif num_labels == 2:
self.criterion = nn.CrossEntropyLoss()
elif num_labels > 2:
self.criterion = nn.BCEWithLogitsLoss()
self.ece_criterion = ECE().to(self.device)
self.log_freq = log_freq
self.workspace_name = workspace_name
self.save_model = False
self.avg_loss = 10000
print("Total Parameters:", sum([p.nelement() for p in self.model.parameters()]))
def train(self, epoch):
self.iteration(epoch, self.train_data)
def test(self, epoch):
self.iteration(epoch, self.test_data, train=False)
def iteration(self, epoch, data_loader, train=True):
"""
loop over the data_loader for training or testing
if on train status, backward operation is activated
and also auto save the model every peoch
:param epoch: current epoch index
:param data_loader: torch.utils.data.DataLoader for iteration
:param train: boolean value of is train or test
:return: None
"""
str_code = "train" if train else "test"
self.log_file = f"{self.workspace_name}/logs/masked/log_{str_code}_FS_finetuned.txt"
if epoch == 0:
f = open(self.log_file, 'w')
f.close()
if not train:
self.avg_loss = 10000
# Setting the tqdm progress bar
data_iter = tqdm.tqdm(enumerate(data_loader),
desc="EP_%s:%d" % (str_code, epoch),
total=len(data_loader),
bar_format="{l_bar}{r_bar}")
avg_loss = 0.0
total_correct = 0
total_element = 0
plabels = []
tlabels = []
eval_accurate_nb = 0
nb_eval_examples = 0
logits_list = []
labels_list = []
if train:
self.model.train()
else:
self.model.eval()
with open(self.log_file, 'a') as f:
sys.stdout = f
for i, data in data_iter:
# 0. batch_data will be sent into the device(GPU or cpu)
data = {key: value.to(self.device) for key, value in data.items()}
if train:
h_rep, logits = self.model.forward(data["bert_input"], data["segment_label"])
else:
with torch.no_grad():
h_rep, logits = self.model.forward(data["bert_input"], data["segment_label"])
# print(logits, logits.shape)
logits_list.append(logits.cpu())
labels_list.append(data["progress_status"].cpu())
# print(">>>>>>>>>>>>", progress_output)
# print(f"{epoch}---nelement--- {data['progress_status'].nelement()}")
# print(data["progress_status"].shape, logits.shape)
progress_loss = self.criterion(logits, data["progress_status"])
loss = progress_loss
if torch.cuda.device_count() > 1:
loss = loss.mean()
# 3. backward and optimization only in train
if train:
self.optim.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(self.model.parameters(), 1.0)
self.optim.step()
# progress prediction accuracy
# correct = progress_output.argmax(dim=-1).eq(data["progress_status"]).sum().item()
probs = nn.LogSoftmax(dim=-1)(logits)
predicted_labels = torch.argmax(probs, dim=-1)
true_labels = torch.argmax(data["progress_status"], dim=-1)
plabels.extend(predicted_labels.cpu().numpy())
tlabels.extend(true_labels.cpu().numpy())
# print(">>>>>>>>>>>>>>", predicted_labels, true_labels)
# Compare predicted labels to true labels and calculate accuracy
correct = (predicted_labels == true_labels).sum().item()
avg_loss += loss.item()
total_correct += correct
total_element += true_labels.nelement()
if train:
post_fix = {
"epoch": epoch,
"iter": i,
"avg_loss": avg_loss / (i + 1),
"avg_acc": total_correct / total_element * 100,
"loss": loss.item()
}
else:
logits = logits.detach().cpu().numpy()
label_ids = data["progress_status"].to('cpu').numpy()
tmp_eval_nb = accurate_nb(logits, label_ids)
eval_accurate_nb += tmp_eval_nb
nb_eval_examples += label_ids.shape[0]
total_element += data["progress_status"].nelement()
# avg_loss += loss.item()
post_fix = {
"epoch": epoch,
"iter": i,
"avg_loss": avg_loss / (i + 1),
"avg_acc": tmp_eval_nb / total_element * 100,
"loss": loss.item()
}
if i % self.log_freq == 0:
data_iter.write(str(post_fix))
# precisions = precision_score(plabels, tlabels, average="weighted")
# recalls = recall_score(plabels, tlabels, average="weighted")
f1_scores = f1_score(plabels, tlabels, average="weighted")
if train:
final_msg = {
"epoch": f"EP{epoch}_{str_code}",
"avg_loss": avg_loss / len(data_iter),
"total_acc": total_correct * 100.0 / total_element,
# "precisions": precisions,
# "recalls": recalls,
"f1_scores": f1_scores
}
else:
eval_accuracy = eval_accurate_nb/nb_eval_examples
logits_ece = torch.cat(logits_list)
labels_ece = torch.cat(labels_list)
ece = self.ece_criterion(logits_ece, labels_ece).item()
final_msg = {
"epoch": f"EP{epoch}_{str_code}",
"eval_accuracy": eval_accuracy,
"ece": ece,
"avg_loss": avg_loss / len(data_iter),
# "precisions": precisions,
# "recalls": recalls,
"f1_scores": f1_scores
}
if self.save_model:
conf_hist = visualization.ConfidenceHistogram()
plt_test = conf_hist.plot(np.array(logits_ece), np.array(labels_ece), title= f"Confidence Histogram {epoch}")
plt_test.savefig(f"{self.workspace_name}/plots/confidence_histogram/FS/conf_histogram_test_{epoch}.png",bbox_inches='tight')
plt_test.close()
rel_diagram = visualization.ReliabilityDiagram()
plt_test_2 = rel_diagram.plot(np.array(logits_ece), np.array(labels_ece),title=f"Reliability Diagram {epoch}")
plt_test_2.savefig(f"{self.workspace_name}/plots/confidence_histogram/FS/rel_diagram_test_{epoch}.png",bbox_inches='tight')
plt_test_2.close()
print(final_msg)
# print("EP%d_%s, avg_loss=" % (epoch, str_code), avg_loss / len(data_iter), "total_acc=", total_correct * 100.0 / total_element)
f.close()
sys.stdout = sys.__stdout__
if train:
self.save_model = False
if self.avg_loss > (avg_loss / len(data_iter)):
self.save_model = True
self.avg_loss = (avg_loss / len(data_iter))
# plt_test.show()
# print("EP%d_%s, " % (epoch, str_code))
def save(self, epoch, file_path="output/bert_fine_tuned_trained.model"):
"""
Saving the current BERT model on file_path
:param epoch: current epoch number
:param file_path: model output path which gonna be file_path+"ep%d" % epoch
:return: final_output_path
"""
output_path = file_path + ".ep%d" % epoch
torch.save(self.model.cpu(), output_path)
self.model.to(self.device)
print("EP:%d Model Saved on:" % epoch, output_path)
return output_path
|