Exemplo n.º 1
0
                        action='store_true',
                        default=False,
                        help='Use Ingr embedding in encoder')
    parser.add_argument('--shared-proj',
                        action='store_true',
                        default=False,
                        help='Share projection layers for name and steps')

    parser.add_argument('--load-checkpoint',
                        type=str,
                        default=None,
                        help='Load from state dict checkpoint')
    args = parser.parse_args()

    start = datetime.now()
    USE_CUDA, DEVICE = get_device()

    # Filters
    MAX_NAME = 15
    MAX_INGR = 5
    MAX_INGR_TOK = 20
    MAX_STEP_TOK = 256

    # Reproducibility
    seed = 42
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)

    # Args
    data_dir = args.data_dir
def main():

    import os
    import torch
    import argparse
    import torch.nn.init as init

    from recipe_gen.utils import get_device, count_parameters

    parser = argparse.ArgumentParser(
        description='Baseline for recipe generation (dynamic attn)')
    parser.add_argument('--gold-path',
                        type=str,
                        required=True,
                        help='location of the data corpus')
    parser.add_argument('--pred-path',
                        type=str,
                        required=True,
                        help='location of the data corpus')
    parser.add_argument('--model-path',
                        type=str,
                        required=True,
                        help='location of the data corpus')

    args = parser.parse_args()

    start = datetime.now()
    USE_CUDA, DEVICE = get_device()

    args = parser.parse_args()

    gold_path = args.gold_path
    pred_path = args.pred_path
    model_path = args.model_path

    model = AbsoluteOrderTeacher(embedding_dim=768,
                                 hidden_size=256,
                                 gru_layers=2)
    gold_test_df = pd.read_msgpack(gold_path)
    pred_test_df = pd.read_msgpack(pred_path)

    gold_bert_feature_lists = []
    for index, row in tqdm(gold_test_df.iterrows(), total=len(gold_test_df)):
        row = row['bert_features_steps'].tolist()
        if type(row[-1]) == tuple:
            pads = [x for x in row[-1]]
            new_row = np.array(row[:-1] + pads)
            if new_row.shape[0] > 15:
                new_row = new_row[:15]
            gold_bert_feature_lists.append(new_row)
        else:
            new_row = np.array(row)
            if new_row.shape[0] > 15:
                new_row = new_row[:15]
            gold_bert_feature_lists.append(np.array(new_row))
    gold_test_df['bert_features_steps'] = gold_bert_feature_lists

    print(gold_test_df.head(2))

    pred_bert_feature_lists = []
    for index, row in tqdm(pred_test_df.iterrows(), total=len(pred_test_df)):
        row = row['bert_features_steps'].tolist()
        if type(row[-1]) == tuple:
            pads = [x for x in row[-1]]
            new_row = np.array(row[:-1] + pads)
            if new_row.shape[0] > 15:
                new_row = new_row[:15]
            pred_bert_feature_lists.append(new_row)
        else:
            new_row = np.array(row)
            if new_row.shape[0] > 15:
                new_row = new_row[:15]
            pred_bert_feature_lists.append(np.array(new_row))
    pred_test_df['bert_features_steps'] = pred_bert_feature_lists

    eval_df = pd.merge(gold_test_df, pred_test_df, left_on='i', right_on='i')
    eval_df.dropna()
    eval_df.columns = [
        'i', 'bert_features_step_lists', 'bert_features_pred_step_lists'
    ]
    print(eval_df.head(2))
    eval_df = eval_df.sample(frac=0.5)
    print(len(eval_df))

    # Load state dict
    state_dict = torch.load(model_path)
    model.load_state_dict(state_dict, strict=True)
    model.to(DEVICE)
    print('Model loaded')

    model.eval()
    with torch.no_grad():
        eval_loss = test_run_epoch(device=DEVICE,
                                   df=eval_df,
                                   model=model,
                                   batch_size=16)

    print('The absolute oder score is: {}'.format(eval_loss))