if __name__ == '__main__': EP = 100 SAVING_DIR = '../models/' tokenizer = BertTokenizer.from_pretrained( '/home/zydq/.torch/models/bert/chinese-bert_chinese_wwm_pytorch', do_lower_case=True) train_loader, val_loader = get_data_loaders( rv_path='../data/TRAIN/Train_reviews.csv', lb_path='../data/TRAIN/Train_labels.csv', tokenizer=tokenizer, batch_size=12, val_split=0.15) model = OpinioNet.from_pretrained( '/home/zydq/.torch/models/bert/chinese-bert_chinese_wwm_pytorch') model.cuda() optimizer = Adam(model.parameters(), lr=5e-6) scheduler = GradualWarmupScheduler(optimizer, total_epoch=2) best_val_f1 = 0 best_val_loss = float('inf') for e in range(EP): print('Epoch [%d/%d] train:' % (e, EP)) train_loss, train_f1, train_pr, train_rc = train_epoch( model, train_loader, optimizer, scheduler) print("loss %.5f, f1 %.5f, pr %.5f, rc %.5f" % (train_loss, train_f1, train_pr, train_rc)) print('Epoch [%d/%d] eval:' % (e, EP)) val_loss, val_f1, val_pr, val_rc = eval_epoch(model, val_loader)
tokenizer = BertTokenizer.from_pretrained(model_config['path'], do_lower_case=True) cv_loaders = get_data_loaders_cv( rv_path='../data/TRAIN/Train_laptop_reviews.csv', lb_path='../data/TRAIN/Train_laptop_labels.csv', tokenizer=tokenizer, batch_size=args.bs, type='laptop', folds=FOLDS) BEST_THRESHS = [0.1] * FOLDS BEST_F1 = [0] * FOLDS for cv_idx, (train_loader, val_loader) in enumerate(cv_loaders): model = OpinioNet.from_pretrained(model_config['path'], version=model_config['version'], focal=model_config['focal']) model.load_state_dict( torch.load('../models/pretrained_' + model_config['name'])) model.cuda() optimizer = Adam(model.parameters(), lr=model_config['lr']) scheduler = GradualWarmupScheduler(optimizer, total_epoch=10 * len(train_loader)) best_val_f1 = 0 best_val_loss = float('inf') for e in range(EP): print('Epoch [%d/%d] train:' % (e, EP)) train_loss, train_f1, train_pr, train_rc = train_epoch( model, train_loader, optimizer, scheduler, type='laptop')
return total_loss, total_f1, total_pr, total_rc if __name__ == '__main__': EP = 100 SAVING_DIR = '../models/' tokenizer = BertTokenizer.from_pretrained('/home/zydq/.torch/models/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12', do_lower_case=True) # tokenizer = BertTokenizer.from_pretrained('/home/zydq/.tf/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12', # do_lower_case=True) makeup_train_loader, makeup_val_loader, laptop_train_loader, laptop_val_loader, corpus_loader = \ get_data_loaders_round2(tokenizer, batch_size=12) model = OpinioNet.from_pretrained('/home/zydq/.torch/models/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12') # model = OpinioNet.from_pretrained('/home/zydq/.tf/bert/chinese_roberta_wwm_ext_L-12_H-768_A-12', from_tf=True) model.cuda() optimizer = Adam(model.parameters(), lr=6e-6) scheduler = GradualWarmupScheduler(optimizer, total_epoch=2*max(len(makeup_train_loader), len(corpus_loader))) best_val_f1 = 0 best_val_loss = float('inf') for e in range(EP): print('Epoch [%d/%d] train:' % (e, EP)) train_loss, train_lm_loss, train_f1, train_pr, train_rc = train_epoch(model, makeup_train_loader, laptop_train_loader, corpus_loader, optimizer, scheduler) print("loss %.5f, lm loss %.5f f1 %.5f, pr %.5f, rc %.5f" % (train_loss, train_lm_loss, train_f1, train_pr, train_rc)) print('Epoch [%d/%d] makeup eval:' % (e, EP)) val_loss, val_f1, val_pr, val_rc = eval_epoch(model, makeup_val_loader, type='makeup') print("makeup_val: loss %.5f, f1 %.5f, pr %.5f, rc %.5f" % (val_loss, val_f1, val_pr, val_rc))