def ensemble(results, ans_unk_idx):
    final_result = masked_unk_softmax(results[0], dim=1, mask_idx=ans_unk_idx)

    if len(results) == 1:
        return final_result

    for result in results[1:]:
        final_result += masked_unk_softmax(result, dim=1, mask_idx=ans_unk_idx)

    return final_result
def ensemble(results, ans_unk_idx):
    final_result = masked_unk_softmax(results[0], dim=1, mask_idx=ans_unk_idx)

    if len(results) == 1:
        return final_result

    for result in results[1:]:
        final_result += masked_unk_softmax(result, dim=1, mask_idx=ans_unk_idx)

    return final_result
Example #3
0
def run_model(current_model, data_reader, UNK_idx=0):
    softmax_tot = []
    q_id_tot = []

    start = timeit.default_timer()
    for i, batch in enumerate(data_reader):
        if (i + 1) % 100 == 0:
            end = timeit.default_timer()
            time = end - start
            start = timeit.default_timer()
            print(" process batch %d for test for %.1f s" % (i + 1, time))
            sys.stdout.flush()

        verbose_info = batch['verbose_info']
        q_ids = verbose_info['question_id'].cpu().numpy().tolist()
        logit_res = one_stage_run_model(batch, current_model, eval_mode=True)
        softmax_res = masked_unk_softmax(logit_res, dim=1, mask_idx=UNK_idx)
        softmax_res = softmax_res.data.cpu().numpy().astype(np.float16)
        q_id_tot += q_ids
        softmax_tot.append(softmax_res)
    softmax_result = np.vstack(softmax_tot)

    return q_id_tot, softmax_result
Example #4
0
def run_model(current_model, data_reader, UNK_idx=0):
    softmax_tot = []
    q_id_tot = []

    start = timeit.default_timer()
    for i, batch in enumerate(data_reader):
        if (i+1) % 100 == 0:
            end = timeit.default_timer()
            time = end - start
            start = timeit.default_timer()
            print(" process batch %d for test for %.1f s" % (i+1, time))
            sys.stdout.flush()

        verbose_info = batch['verbose_info']
        q_ids = verbose_info['question_id'].cpu().numpy().tolist()
        logit_res = one_stage_run_model(batch, current_model, eval_mode=True)
        softmax_res = masked_unk_softmax(logit_res, dim=1, mask_idx=UNK_idx)
        softmax_res = softmax_res.data.cpu().numpy().astype(np.float16)
        q_id_tot += q_ids
        softmax_tot.append(softmax_res)
    softmax_result = np.vstack(softmax_tot)

    return q_id_tot, softmax_result
    os.makedirs(out_dir, exist_ok=True)

    if is_one_stageModel(model_type):
        for i, batch in enumerate(data_reader_val):
            if i % 100 == 0:
                print("process batch %d" % i)
            verbose_info = batch["verbose_info"]
            answer_scores = batch["ans_scores"]
            answer_scores_np = answer_scores.numpy()

            for imd, current_model in enumerate(current_models):
                # print("process model %d"%imd)
                logit_res = one_stage_run_model(batch, current_model)

                softmax_res = masked_unk_softmax(logit_res,
                                                 dim=1,
                                                 mask_idx=ans_dic.UNK_idx)
                softmax_res_data = softmax_res.data.cpu().numpy()

                with open(os.path.join(out_dir, tmp_model_file_name % imd),
                          "wb") as w:
                    pickle.dump(softmax_res_data, w)

            ensembled_soft_max_result = ensemble_model(out_dir, num_of_model)

            nsample, _ = answer_scores_np.shape
            total_sample += nsample
            scores = compute_score_with_prob(ensembled_soft_max_result,
                                             answer_scores_np)
            total_score += scores
    os.makedirs(out_dir, exist_ok=True)

    if is_one_stageModel(model_type):
        for i, batch in enumerate(data_reader_val):
            if i % 100 == 0:
                print("process batch %d" % i)
            verbose_info = batch['verbose_info']
            answer_scores = batch['ans_scores']
            answer_scores_np = answer_scores.numpy()

            for imd, current_model in enumerate(current_models):
                # print("process model %d"%imd)
                logit_res = one_stage_run_model(batch, current_model)

                softmax_res = masked_unk_softmax(logit_res,
                                                 dim=1,
                                                 mask_idx=ans_dic.UNK_idx)
                softmax_res_data = softmax_res.data.cpu().numpy()

                with open(os.path.join(out_dir,
                                       tmp_model_file_name % imd), 'wb') as w:
                    pickle.dump(softmax_res_data, w)

            ensembled_soft_max_result = ensemble_model(out_dir, num_of_model)

            nsample, _ = answer_scores_np.shape
            total_sample += nsample
            scores = compute_score_with_prob(ensembled_soft_max_result,
                                             answer_scores_np)
            total_score += scores