counts = counts.to(device)
            ids = ids.to(device)
            batch_size = cw_idxs.size(0)

            # Forward
            output_dict = model(cw_idxs, cc_idxs, qw_idxs, qc_idxs, ids,
                                start_idxs, end_idxs, counts)
            loss = output_dict['loss']
            nll_meter.update(loss.item(), batch_size)

            # Log info
            progress_bar.update(batch_size)
            progress_bar.set_postfix(NLL=nll_meter.avg)
            pred_dict.update(output_dict["predictions"])

    model.set_eval_data(None)
    model.train()

    # Get F1 and EM scores
    eval_dict = eval_dicts(gold_dict, pred_dict)
    results_list = [('Loss', nll_meter.avg), ('F1', eval_dict['F1']),
                    ('EM', eval_dict['EM'])]

    eval_dict = OrderedDict(results_list)

    return eval_dict, pred_dict


if __name__ == '__main__':
    main(get_train_args())
Ejemplo n.º 2
0
    question_char_idxs = merge_2d(question_char_idxs)

    # number_indices = merge_1d(number_indices, pad_value = -1)
    start_indices = merge_1d(start_indices, pad_value=-1)
    end_indices = merge_1d(end_indices, pad_value=-1)
    counts = merge_0d(counts)  # TODO check
    # add_sub_expressions = merge_2d(add_sub_expressions, pad_value = -1)

    ids = merge_0d(ids)

    return (context_idxs, context_char_idxs, question_idxs, question_char_idxs,
            start_indices, end_indices, counts, ids)


if __name__ == "__main__":
    args = get_train_args()
    torch.manual_seed(224)
    print("Building datasets...", end=" ")
    train_dataset = DROP(args.train_record_file)
    train_loader = data.DataLoader(train_dataset,
                                   batch_size=args.batch_size,
                                   shuffle=True,
                                   num_workers=args.num_workers,
                                   collate_fn=collate_fn)
    dev_dataset = DROP(args.dev_record_file)
    dev_loader = data.DataLoader(dev_dataset,
                                 batch_size=args.batch_size,
                                 shuffle=False,
                                 num_workers=args.num_workers,
                                 collate_fn=collate_fn)
    print("Done!")