def eval(model, opt, SRC, TRG, last_loss, k_n, total_n): validation = torch.zeros((1, 2)).float() labellll = torch.tensor(0).long() labellll = labellll.view(-1, 1) validation = torch.tensor(validation).cuda() labellll = torch.tensor(labellll).cuda() model.eval() with torch.no_grad(): for i, batch in enumerate(opt.train1): src1 = batch.src.transpose(0, 1) trg1 = batch.trg.transpose(0, 1) trg_input1 = trg1[:, :-1] src_mask1, trg_mask1 = create_masks(src1, trg_input1, opt) src_mask1 = torch.tensor(src_mask1).cuda() src1 = torch.tensor(src1).cuda() trg_input1 = torch.tensor(trg_input1).cuda() trg_mask1 = torch.tensor(trg_mask1).cuda() val, valabel = model(src1, trg_input1, src_mask1, trg_mask1, last_loss, k_n, total_n) valabel = valabel.view(-1, 1) validation = torch.cat((validation, val), dim=0) labellll = torch.cat((labellll, valabel), dim=0) validation = validation[1:, :] labellll = labellll[1:, :] validation = validation.detach().cpu().numpy() labellll = labellll.detach().cpu().numpy() validation = np.argmax(validation, axis=1) validation = validation.reshape(-1, 1) F_1 = f1_score(labellll, validation, average='binary') print("val:", F_1) f1.append(F_1) plt.figure() plt.plot(f1) plt.savefig(name + "val.png")
def train_model(model, opt, SRC, TRG): with torch.no_grad(): print("testing model...") model.train() a = 0 prediction = torch.zeros((1, 2)).float() label = torch.tensor(0).long() label = label.view(-1, 1) prediction = torch.tensor(prediction).cuda() label = torch.tensor(label).cuda() for i, batch in enumerate(opt.train): src = batch.src.transpose(0, 1) trg = batch.trg.transpose(0, 1) trg_input = trg[:, :-1] src_mask, trg_mask = create_masks(src, trg_input, opt) src_mask = torch.tensor(src_mask).cuda() src = torch.tensor(src).cuda() trg_input = torch.tensor(trg_input).cuda() trg_mask = torch.tensor(trg_mask).cuda() if opt.min_strlen <= src.size(1) < opt.max_strlen: preds, ys = model(src, trg_input, src_mask, trg_mask) ys = ys.view(-1, 1) prediction = torch.cat((prediction, preds), dim=0) label = torch.cat((label, ys), dim=0) #a1 = opt.test_iteration #while a > a1: # prediction = prediction[1:, :] # label = label[1:, :] # prediction = prediction.detach().cpu().numpy() # label = label.detach().cpu().numpy() # prediction = np.argmax(prediction, axis=1) # prediction = prediction.reshape(-1, 1) # F_measure1 = f1_score(label, prediction, average='weighted') # print("f1_score:", F_measure1) prediction = prediction[1:, :] label = label[1:, :] print(prediction.size(), label.size()) if prediction.size(0) != label.size(0): if prediction.size(0) >= label.size(0): makeup = torch.tensor( (prediction.size(0) - label.size(0))).long() one = torch.tensor(1).long() label = torch.cat( (label.long(), torch.zeros(makeup, one).long().cuda()), dim=0) else: makeup = torch.tensor( (label.size(0) - prediction.size(0))).float() two = torch.tensor(2).float() prediction = torch.cat( (prediction, torch.zeros(makeup, two).cuda()), dim=0) prediction1 = prediction.detach().cpu().numpy() label1 = label.detach().cpu().numpy() prediction1 = np.argmax(prediction1, axis=1) prediction1 = prediction1.reshape(-1, 1) F_measure1 = f1_score(label1, prediction1, average='binary') print(np.shape(prediction1)[0]) print("f1_score:", F_measure1) torch.cuda.empty_cache() return F_measure1, prediction, label
def train_model(model, opt, SRC, TRG): savep = 50 print("training model...") model.train() #The reward of each epoch rewards = torch.zeros(opt.epochs).float() #The counting marice for action k_n = torch.ones(opt.n_layers).float() #Total mean reward mean_reward = torch.tensor(0).float() #The reward matrix(loss matrix for each layer) last_loss = abs(torch.ones(opt.n_layers)) #The number of total action total_n = torch.tensor(1).float() #Compute the layer distribution of each epoch knn_epoch = torch.zeros(opt.epochs, opt.n_layers) for epoch in range(opt.epochs): total_loss = 0 loss_value = 0 for i, batch in enumerate(opt.train): src = batch.src.transpose(0, 1) trg = batch.trg.transpose(0, 1) trg_input = trg[:, :-1] src_mask, trg_mask = create_masks(src, trg_input, opt) preds, ys, rewardss, k_n, action = model(src, trg_input, src_mask, trg_mask, last_loss, k_n, total_n) preds = torch.log(preds) opt.optimizer.zero_grad() loss = criterion(preds, ys) loss.backward() opt.optimizer.step() loss1 = loss print(loss1) mean_reward, total_n, last_loss, knn_epoch=dyna(action,rewards,k_n,mean_reward,loss,\ last_loss,total_n,rewardss,knn_epoch,epoch) if opt.SGDR == True: opt.sched.step() loss_value = loss_value + loss1 print(loss_value) #if (epoch // savep) * savep - epoch == 0: # dst = name + str(epoch) # os.mkdir(dst) # print("saving weights to " + dst + "/...") # torch.save(model.state_dict(), f'{dst}/model_weights') # pickle.dump(SRC, open(f'{dst}/SRC.pkl', 'wb')) # pickle.dump(TRG, open(f'{dst}/TRG.pkl', 'wb')) loss_value = loss_value / (i + 1) losss.append(loss_value) print(losss) pl_greedy(k_n, opt) pl_epoch(knn_epoch, opt, last_loss, k_n, total_n) plt.figure() plt.plot(losss) plt.savefig(name + ".png")
def eval(model, opt, SRC, TRG, epoch, name, best_epoch, best_val): validation = torch.zeros((1, 2)).float() labellll = torch.tensor(0).long() labellll = labellll.view(-1, 1) validation = torch.tensor(validation).cuda() labellll = torch.tensor(labellll).cuda() model.eval() with torch.no_grad(): for i, batch in enumerate(opt.train1): src1 = batch.src.transpose(0, 1) trg1 = batch.trg.transpose(0, 1) trg_input1 = trg1[:, :-1] src_mask1, trg_mask1 = create_masks(src1, trg_input1, opt) src_mask1 = torch.tensor(src_mask1).cuda() src1 = torch.tensor(src1).cuda() trg_input1 = torch.tensor(trg_input1).cuda() trg_mask1 = torch.tensor(trg_mask1).cuda() val, valabel = model(src1, trg_input1, src_mask1, trg_mask1) valabel = valabel.view(-1, 1) validation = torch.cat((validation, val), dim=0) labellll = torch.cat((labellll, valabel), dim=0) validation = validation[1:, :] labellll = labellll[1:, :] print("vali_size:", labellll.size(0)) validation = validation.detach().cpu().numpy() labellll = labellll.detach().cpu().numpy() validation = np.argmax(validation, axis=1) validation = validation.reshape(-1, 1) accuracy = accuracy_score(labellll, validation, normalize=True) print("accuracy:", accuracy) F_1 = f1_score(labellll, validation, average='binary') if F_1 > best_val: best_val = F_1 best_epoch = epoch + opt.restart else: best_epoch = best_epoch best_val = best_val print("val:", F_1) f1.append(F_1) plt.figure() plt.plot(f1) plt.savefig(name + "validation.png") valtext = name + "validation.txt" np.savetxt(valtext, f1) return best_epoch, best_val
def train_model(model, opt, SRC, TRG): name = '128_L' + str(opt.n_layers) + str(opt.aaa) savep = 5 print("training model...") model.train() #Initialize the test epoch doc_epoch = 50 test_epoch = doc_epoch dst = name dst1 = dst + "1" losstext = "losstext.csv" #dst1=name + str(test_epoch)+'_c_'+str(epoch+opt.restart) os.mkdir(dst) os.mkdir(dst1) best_val = opt.bestval best_epoch = 0 + opt.restart for epoch in range(opt.epochs): total_loss = 0 loss_value = 0 for i, batch in enumerate(opt.train): src = batch.src.transpose(0, 1) trg = batch.trg.transpose(0, 1) trg_input = trg[:, :-1] src_mask, trg_mask = create_masks(src, trg_input, opt) src_mask = torch.tensor(src_mask).cuda() src = torch.tensor(src).cuda() trg_input = torch.tensor(trg_input).cuda() trg_mask = torch.tensor(trg_mask).cuda() preds, ys = model(src, trg_input, src_mask, trg_mask) preds = torch.log(preds) opt.optimizer.zero_grad() loss = criterion(preds, ys) loss.backward() opt.optimizer.step() loss1 = loss loss1 = loss.detach().cpu().numpy() #print("current_loss:",loss1) if opt.SGDR == True: opt.sched.step() loss_value = loss_value + loss1 if (epoch // savep) * savep - epoch == 0: shutil.rmtree(dst) dst = name + str(epoch + opt.restart) os.mkdir(dst) print("saving trained weights to " + dst + "/...") torch.save(model.state_dict(), f'{dst}/test_weights') pickle.dump(SRC, open(f'{dst}/SRC.pkl', 'wb')) pickle.dump(TRG, open(f'{dst}/TRG.pkl', 'wb')) loss_value = loss_value / (i + 1) losss.append(loss_value) print("loss list:", losss) #plt.figure() #plt.plot(losss) #losstext=name+"val_epo" #np.savetxt(losstext, losss) #plt.savefig(name + ".png") best_epoch, best_val = eval(model, opt, SRC, TRG, epoch, name, best_epoch, best_val) print("best_epoch:", best_epoch) print("best_val:", best_val) print("current_epoch", epoch) if best_epoch >= doc_epoch: if best_epoch > test_epoch: shutil.rmtree(dst1) test_epoch = best_epoch xianshi = int(best_val * 100) xianshi = xianshi / 100 dst1 = str(xianshi) + '_' + str(epoch + opt.restart) + '_' + str( opt.aaa) + '_' + str( opt.batchsize) os.mkdir(dst1) print("saving tested weights to " + dst1 + "/...") torch.save(model.state_dict(), f'{dst1}/test_weights') pickle.dump(SRC, open(f'{dst1}/SRC.pkl', 'wb')) pickle.dump(TRG, open(f'{dst1}/TRG.pkl', 'wb'))