示例#1
0
def _eval(mymodel,
          args,
          data_loader,
          data_set=None,
          RELOAD_MODEL=None,
          use_cuda=False):
    old_model_path = os.path.join(args.result_dir, RELOAD_MODEL)
    if RELOAD_MODEL is not None and os.path.exists(old_model_path):
        mymodel.load_model(old_model_path)
        LOGGER.info("Reload model successfully~")
    else:
        LOGGER.info(
            f'There is no such file in {old_model_path}, End evaluation')
        return

    LOGGER.info(f'reload model {RELOAD_MODEL}')
    hyper_param = {
        'batch_size': 100,
        'issave': True,
        'result_dir': args.result_dir
    }

    timer = Timer()
    timer.set(args.time_budget)
    with timer.time_limit('eval'):
        mymodel.eval_model(data_loader, data_set, hyper_param, rebuild=True)
示例#2
0
def _train(mymodel, args, data_loader, train_dataset=None, eval_dataset=None, RELOAD_MODEL=None, use_cuda=False):
    if RELOAD_MODEL is None:
        LOGGER.info(f'Rebuild model')
    else:
        old_model_path = os.path.join(args.result_dir, RELOAD_MODEL)
        if os.path.exists(old_model_path):
            mymodel.load_model(old_model_path)
            LOGGER.info("Reload model successfully~")
        else:
            LOGGER.info(f'There is no such file in {old_model_path}, Rebuild model')

    ##TODO:
    BATCH_SIZE = 8 if data_loader.sentence_max_len > 200 else 16
    if use_cuda:
        train_param = {
            'EPOCH': 30,         #45  TODO:15
            'batch_size': BATCH_SIZE,    #512   TODO:64
            'learning_rate_bert': 5e-5,  
            'learning_rate_upper': 1e-3,  #TODO:1e-3
            'bert_finetune': True,
            'visualize_length': 20, #10
            'isshuffle': True,
            'result_dir': args.result_dir,
            'model_name':'model_test.p'
        }

    else:
        train_param = {
            'EPOCH': CPU_EPOCH,         #45
            'batch_size': CPU_BATCHSIZE,    #512
            'learning_rate_bert': 5e-5,
            'learning_rate_upper': 1e-3,
            'bert_finetune': False,
            'visualize_length': CPU_VISUAL, #10
            'isshuffle': True,
            'result_dir': args.result_dir,
            'model_name':'model_test.p'
        }        

    timer = Timer()
    timer.set(args.time_budget)
    loss_record = None
    with timer.time_limit('training'):
        loss_record, score_record = mymodel.train_model(data_loader, hyper_param=train_param, train_dataset=train_dataset, eval_dataset=eval_dataset)

    loss_record = np.array(loss_record)
    loss_save_path = os.path.join(args.result_dir, 'loss_train.txt')
    loss_img_path = os.path.join(args.result_dir, 'loss.png')
    np.savetxt(loss_save_path, loss_record)

    score_record = np.array(score_record)
    score_save_path = os.path.join(args.result_dir, 'score_train.txt')
    score_img_path = os.path.join(args.result_dir, 'score.png')
    np.savetxt(score_save_path, score_record)  ### (epochs, 3)

    loss = np.loadtxt(loss_save_path).reshape(1, -1)
    score = np.loadtxt(score_save_path).T
    plot_img(loss, loss_img_path)
    plot_img(score, score_img_path)