Exemple #1
0
def test(config):
    device = torch.device(config.device)
    # load data
    data_dir = os.path.join(config.model, config.dataset)
    test_fname = os.path.join(data_dir, config.test_data)
    test_data = get_loader(test_fname, config.batch)
    wordemb = np.loadtxt(os.path.join(data_dir, config.wordmat_file))
    # charemb = np.loadtxt(os.path.join(data_dir, config.charmat_file))
    # init model
    model = GCM(dim_word=config.dim_word,
                num_channel=config.num_channel,
                kernel_size=config.kernel_size,
                aspect_kernel_size=config.aspect_kernel_size,
                num_layer=config.num_layer,
                num_class=config.num_class,
                wordmat=wordemb,
                dropout_rate=config.dropout_rate,
                device=device)
    # load model
    model_save_dir = os.path.join(config.model_save, config.dataset,
                                  config.model)
    result_dir = os.path.join(config.result_save, config.dataset, config.model,
                              'test')
    if not os.path.exists(result_dir):
        os.makedirs(result_dir)
    save_fout = open(os.path.join(result_dir, 'best.txt'), 'w')
    model.load_state_dict(torch.load(os.path.join(model_save_dir, 'best.pth')))
    model = model.to(device)
    model.eval()
    # init loss
    logit_list = []
    rating_list = []
    for batch_data in tqdm(test_data):
        sent_ids, lens, aspect_ids, aspect_lens, polarity, pws = batch_data
        sent_ids, aspect_ids, polarity = sent_ids.to(device), aspect_ids.to(
            device), polarity.to(device)
        logit = model(sent_ids, aspect_ids)
        save(sent_ids.tolist(), lens.tolist(), aspect_ids.tolist(),
             aspect_lens.tolist(), polarity.tolist(), logit.tolist(),
             save_fout, config)
        logit_list.append(logit.cpu().data.numpy())
        rating_list.append(polarity.cpu().data.numpy())
    test_acc, test_precision, test_recall, test_f1 = get_score(
        np.concatenate(logit_list, 0), np.concatenate(rating_list, 0))
    print(
        'test_acc=%.4f, test_precision=%.4f, test_recall=%.4f, test_f1=%.4f' %
        (test_acc, test_precision, test_recall, test_f1))