test_y = test_data.targets lstm = BiLSTM(INPUT_SIZE, HIDDEN_SIZE, NUM_LAYERS, 10).to(device) optimizer = torch.optim.Adam(lstm.parameters(), lr=LR) loss_func = nn.CrossEntropyLoss() for epoch in range(EPOCH): for step, (x, y) in enumerate(train_loader): b_x = Variable(x.view(-1, TIME_STEP, INPUT_SIZE).to(device)) b_y = Variable(y.to(device)) output = lstm(b_x) loss = loss_func(output, b_y) optimizer.zero_grad() loss.backward() optimizer.step() if step%50 == 0: test_output = lstm(test_x.view(-1, TIME_STEP, INPUT_SIZE).to(device)) pred_y = torch.max(test_output, 1)[1].data.cpu().squeeze() accuracy = sum(pred_y == test_y)/float(test_y.size(0)) print('Epoch: ',epoch, '| train loss:%.4f' %loss.data.item(),'| test accuracy:%.2f' %accuracy) test_output = lstm(test_x[:10].view(-1, TIME_STEP, INPUT_SIZE).to(device)) pred_y = torch.max(test_output,1)[1].data.cpu().numpy().squeeze() print(pred_y, 'prediction number') print(test_y[:10].numpy(), 'real number') torch.save(lstm.state_dict(), './model/model.ckpt')
def main(config): trainDataPath = config['data']['trainDataPath'] validDataPath = config['data']['validDataPath'] testDataPath = config['data']['testDataPath'] modelName = config['modelName'] batchSize = config['model']['batchSize'] epochNum = config['model']['epochNum'] earlyStop = config['model']['earlyStop'] learningRate = config['model']['learningRate'] modelSavePath = config['model']['modelSavePath'] #GPU/CPU DEVICE = config['DEVICE'] trianDataset = NERDataset(trainDataPath, config) validDataset = NERDataset(validDataPath, config) testDataset = NERDataset(testDataPath, config) trainIter = data.DataLoader(dataset=trianDataset, batch_size=batchSize, shuffle=True, num_workers=4, collate_fn=pad) validIter = data.DataLoader(dataset=validDataset, batch_size=batchSize, shuffle=False, num_workers=4, collate_fn=pad) testIter = data.DataLoader(dataset=testDataset, batch_size=batchSize, shuffle=False, num_workers=4, collate_fn=pad) if modelName == 'bilstm': net = BiLSTM(config) train = bilstmTrain eval = bilstmEval if torch.cuda.device_count() > 1: net = nn.DataParallel(net) if modelName == 'bilstm_crf': net = BiLSTM_CRF(config) train = bilstmCRFTrain eval = bilstmCRFEval if modelName == 'transformer_crf': net = Transformer_CRF(config) train = transformerCRFTrain eval = transformerCRFEval if modelName == 'cnn': net = CNN(config) train = cnnTrain eval = cnnEval net = net.to(DEVICE) lossFunction = nn.NLLLoss() optimizer = optim.Adam(net.parameters(), lr=learningRate, betas=(0.9, 0.999), eps=1e-08) earlyNumber, beforeLoss, maxScore = 0, sys.maxsize, -1 #开始训练 for epoch in range(epochNum): print('第%d次迭代: ' % epoch) totalLoss = train(net, trainIter, optimizer=optimizer, criterion=lossFunction, DEVICE=DEVICE) print('训练损失为: %f' % totalLoss) totalLoss, f1Score = eval(net, validIter, criterion=lossFunction, DEVICE=DEVICE) if f1Score > maxScore: maxScore = f1Score torch.save(net.state_dict(), modelSavePath) print('验证损失为:%f f1Score:%f / %f' % (totalLoss, f1Score, maxScore)) if f1Score < maxScore: earlyNumber += 1 print('earyStop: %d/%d' % (earlyNumber, earlyStop)) else: earlyNumber = 0 if earlyNumber >= earlyStop: break print('\n') #加载最优模型 net.load_state_dict(torch.load(modelSavePath)) totalLoss, f1Score = eval(net, testIter, criterion=lossFunction, DEVICE=DEVICE) print('测试损失为: %f, f1Score: %f' % (totalLoss, f1Score))
ranks = len(values) - temp #Descending #print(acc_labels, entry) groundT = acc_labels.index(1) MRR.append(1.0 / ranks[groundT]) test_mrr = sum(MRR) / len(MRR) #print("mrr at epoch", epoch, test_mrr) if best_MRR < test_mrr: best_MRR = test_mrr #import pdb; pdb.set_trace() acc = get_accuracy(truth_res, pred_res) if best_accuracy < acc: best_accuracy = acc torch.save( model.state_dict(), "/home/knarang2/UserCredibility/models/" + dataset_NLP + "_LSTM.pt") #print("Test Accuracy at epoch", epoch, acc) print("Training and Test Accuracy and MRR at epoch", epoch, '{0:.3f}'.format(train_acc), '{0:.3f}'.format(acc), '{0:.3f}'.format(test_mrr)) if (train_acc - prev_train_acc) < 0.01: print("Exiting training") break print("best_mrr and accuracy", best_MRR, best_accuracy) result_list.append(best_MRR) result_list_acc.append(best_accuracy) except KeyboardInterrupt: