예제 #1
0
def main():
    import argparse
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--debug',
                        action='store_true',
                        default=False,
                        help='set to True to see debugging visualization')
    parser.add_argument('--prefix',
                        type=str,
                        defiault='default',
                        help='a nickanme for the training')
    parser.add_argument('--model',
                        type=str,
                        default='synthesis_baseline',
                        choices=[
                            'synthesis_baseline', 'induction_baseline',
                            'summarizer', 'full'
                        ],
                        help='specify which type of models to train')
    parser.add_argument('--dataset_type',
                        type=str,
                        default='karel',
                        choices=['karel', 'vizdoom'])
    parser.add_argument('--dataset_path',
                        type=str,
                        default='datasets/karel_dataset',
                        help='the path to your dataset')
    parser.add_argument('--checkpoint',
                        type=str,
                        default=None,
                        help='specify the path to a pre-trained checkpoint')
    # log
    parser.add_argument('--log_step',
                        type=int,
                        default=10,
                        help='the frequency of outputing log info')
    parser.add_argument('--write_summary_step',
                        type=int,
                        default=100,
                        help=' the frequency of writing TensorBoard sumamries')
    parser.add_argument('--test_sample_step',
                        type=int,
                        default=100,
                        help='the frequency of performing '
                        'testing inference during training')
    # hyperparameters
    parser.add_argument('--num_k',
                        type=int,
                        default=10,
                        help='the number of seen demonstrations')
    parser.add_argument('--batch_size', type=int, default=32)
    parser.add_argument('--learning_rate', type=float, default=0.001)
    parser.add_argument('--lr_weight_decay',
                        action='store_true',
                        default=False,
                        help='set to `True` to perform expotential weight '
                        'decay on the learning rate')
    parser.add_argument(
        '--scheduled_sampling',
        action='store_true',
        default=False,
        help='set to True to train models with scheduled sampling')
    parser.add_argument('--scheduled_sampling_decay_steps',
                        type=int,
                        default=20000,
                        help='the number of training steps required to decay'
                        'scheduled sampling probability to minimum.')
    # model hyperparameters
    parser.add_argument('--encoder_rnn_type',
                        default='lstm',
                        choices=['lstm', 'rnn', 'gru'])
    parser.add_argument('--num_lstm_cell_units', type=int, default=512)
    parser.add_argument('--demo_aggregation',
                        type=str,
                        default='avgpool',
                        choices=['concat', 'avgpool', 'maxpool'],
                        help='how to aggregate the demo features')

    config = parser.parse_args()

    if config.dataset_type == 'karel':
        import karel_env.dataset_karel as dataset
        dataset_train, dataset_test, dataset_val \
            = dataset.create_default_splits(config.dataset_path, num_k=config.num_k)
    elif config.dataset_type == 'vizdoom':
        import vizdoom_env.dataset_vizdoom as dataset
        dataset_train, dataset_test, dataset_val \
            = dataset.create_default_splits(config.dataset_path, num_k=config.num_k)
    else:
        raise ValueError(config.dataset)

    # Set data dimension in configuration
    data_tuple = dataset_train.get_data(dataset_train.ids[0])
    # s_h: state history, demonstrations
    # a_h: action history, sequence of actions
    # per: sequence of perception primitives
    program, _, s_h, test_s_h, a_h, _, _, _, program_len, demo_len, test_demo_len, \
        per, test_per = data_tuple[:13]

    config.dim_program_token = np.asarray(program.shape)[0]
    config.max_program_len = np.asarray(program.shape)[1]
    config.k = np.asarray(s_h.shape)[0]
    config.test_k = np.asarray(test_s_h.shape)[0]
    config.max_demo_len = np.asarray(s_h.shape)[1]
    config.h = np.asarray(s_h.shape)[2]
    config.w = np.asarray(s_h.shape)[3]
    config.depth = np.asarray(s_h.shape)[4]
    config.action_space = np.asarray(a_h.shape)[2]
    config.per_dim = np.asarray(per.shape)[2]
    if config.dataset_type == 'karel':
        config.dsl_type = dataset_train.dsl_type
        config.env_type = dataset_train.env_type
        config.vizdoom_pos_keys = []
        config.vizdoom_max_init_pos_len = -1
        config.perception_type = ''
        config.level = None
    elif config.dataset_type == 'vizdoom':
        config.dsl_type = 'vizdoom_default'  # vizdoom has 1 dsl type for now
        config.env_type = 'vizdoom_default'  # vizdoom has 1 env type
        config.vizdoom_pos_keys = dataset_train.vizdoom_pos_keys
        config.vizdoom_max_init_pos_len = dataset_train.vizdoom_max_init_pos_len
        config.perception_type = dataset_train.perception_type
        config.level = dataset_train.level

    trainer = Trainer(config, dataset_train, dataset_test)

    log.warning("dataset: %s, learning_rate: %f", config.dataset_path,
                config.learning_rate)
    trainer.train()
예제 #2
0
def main():
    import argparse
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--model',
                        type=str,
                        default='synthesis_baseline',
                        choices=[
                            'synthesis_baseline', 'induction_baseline',
                            'summarizer', 'full'
                        ],
                        help='specify which type of models to evaluate')
    parser.add_argument('--dataset_type',
                        type=str,
                        default='karel',
                        choices=['karel', 'vizdoom'])
    parser.add_argument('--dataset_path',
                        type=str,
                        default='datasets/karel_dataset',
                        help='the path to your dataset')
    parser.add_argument('--dataset_split',
                        type=str,
                        default='test',
                        choices=['train', 'test', 'val'],
                        help='specify the data split to evaluate')
    parser.add_argument('--checkpoint',
                        type=str,
                        default='',
                        help='the path to a trained checkpoint')
    parser.add_argument('--train_dir',
                        type=str,
                        default='',
                        help='the path to train_dir. '
                        'the newest checkpoint will be evaluated')
    parser.add_argument('--output_dir',
                        type=str,
                        default=None,
                        help='the directory to write out programs')
    parser.add_argument('--max_steps',
                        type=int,
                        default=0,
                        help='the number of batches to evaluate. '
                        'set to 0 to evaluate all testing data')
    # hyperparameters
    parser.add_argument('--num_k',
                        type=int,
                        default=10,
                        help='the number of seen demonstrations')
    parser.add_argument('--batch_size', type=int, default=20)
    # model hyperparameters
    parser.add_argument('--encoder_rnn_type',
                        default='lstm',
                        choices=['lstm', 'rnn', 'gru'])
    parser.add_argument('--num_lstm_cell_units', type=int, default=512)
    parser.add_argument('--demo_aggregation',
                        type=str,
                        default='avgpool',
                        choices=['concat', 'avgpool', 'maxpool'],
                        help='how to aggregate the demo features')
    # evaluation task
    parser.add_argument(
        '--no_loss',
        action='store_true',
        default=False,
        help='set to True to not print out the accuracies and losses')
    parser.add_argument('--pred_program',
                        action='store_true',
                        default=False,
                        help='set to True to write out '
                        'predicted and ground truth programs')
    parser.add_argument('--result_data',
                        action='store_true',
                        default=False,
                        help='set to True to save evaluation results')
    parser.add_argument('--result_data_path',
                        type=str,
                        default='result.hdf5',
                        help='the file path to save evaluation results')
    # specify the ids of the testing data that you want to test
    parser.add_argument('--id_list',
                        type=str,
                        help='specify the ids of the data points '
                        'that you want to evaluate. '
                        'By default a whole data split will be evaluated')
    # unseen test
    parser.add_argument('--unseen_test', action='store_true', default=False)
    # write summary file
    parser.add_argument(
        '--quiet',
        action='store_true',
        default=False,
        help='set to True to not log out accuracies and losses '
        'for every batch')
    parser.add_argument('--no_write_summary',
                        action='store_true',
                        default=False,
                        help='set to False to write out '
                        'the summary of accuracies and losses')
    parser.add_argument(
        '--summary_file',
        type=str,
        default='report.txt',
        help='the path to write the summary of accuracies and losses')
    config = parser.parse_args()

    config.write_summary = not config.no_write_summary

    if config.dataset_type == 'karel':
        import karel_env.dataset_karel as dataset
    elif config.datasete_type == 'vizdoom':
        import vizdoom_env.dataset_vizdoom as dataset
    else:
        raise ValueError(config.dataset)

    dataset_train, dataset_test, dataset_val = \
        dataset.create_default_splits(config.dataset_path,
                                        is_train=False, num_k=config.num_k)
    if config.dataset_split == 'train':
        target_dataset = dataset_train
    elif config.dataset_split == 'test':
        target_dataset = dataset_test
    elif config.dataset_split == 'val':
        target_dataset = dataset_val
    else:
        raise ValueError('Unknown dataset split')

    if not config.max_steps > 0:
        config.max_steps = int(len(target_dataset._ids) / config.batch_size)

    if config.dataset_type == 'karel':
        config.perception_type = ''
    elif config.dataset_type == 'vizdoom':
        config.perception_type = target_dataset.perception_type
    else:
        raise ValueError(config.dataset)
    # }}}

    # Data dim
    # [n, max_program_len], [max_program_len], [k, max_demo_len, h, w, depth]
    # [k, max_len_demo, ac], [1], [k]
    data_tuple = target_dataset.get_data(target_dataset.ids[0])
    program, _, s_h, test_s_h, a_h, _, _, _, program_len, demo_len, test_demo_len, \
        per, test_per = data_tuple[:13]

    config.dim_program_token = np.asarray(program.shape)[0]
    config.max_program_len = np.asarray(program.shape)[1]
    config.k = np.asarray(s_h.shape)[0]
    config.test_k = np.asarray(test_s_h.shape)[0]
    config.max_demo_len = np.asarray(s_h.shape)[1]
    config.h = np.asarray(s_h.shape)[2]
    config.w = np.asarray(s_h.shape)[3]
    config.depth = np.asarray(s_h.shape)[4]
    config.action_space = np.asarray(a_h.shape)[2]
    config.per_dim = np.asarray(per.shape)[2]
    if config.dataset_type == 'karel':
        config.dsl_type = target_dataset.dsl_type
        config.env_type = target_dataset.env_type
        config.vizdoom_pos_keys = []
        config.vizdoom_max_init_pos_len = -1
        config.level = None
    elif config.dataset_type == 'vizdoom':
        config.dsl_type = 'vizdoom_default'  # vizdoom has 1 dsl type for now
        config.env_type = 'vizdoom_default'  # vizdoom has 1 env type
        config.vizdoom_pos_keys = target_dataset.vizdoom_pos_keys
        config.vizdoom_max_init_pos_len = target_dataset.vizdoom_max_init_pos_len
        config.level = target_dataset.level

    evaler = Evaler(config, target_dataset)

    log.warning("dataset: %s", config.dataset_path)
    evaler.eval_run()