예제 #1
0
def call_experiment(exp_name,
                    algo,
                    seed=0,
                    num_cpu=1,
                    data_dir=None,
                    verbose=False,
                    **kwargs):
    """
    Run an algorithm with hyperparameters (kwargs), plus configuration

    Arguments:
        exp_name : name for experiment
        algo : callable algorithm function
        seed : random number generator seed
        num_cpu : number of MPI processes to use for algo
        data_dir : directory to store experiment results. If None will use default directory.
        **kwargs : all kwargs to pass to algo

    Returns:
        results : any results returned by algorithm
    """
    # in case seed not in passed kwargs dict
    kwargs['seed'] = seed

    # print experiment details
    table = PrettyTable()
    print("\nRunning experiment: {}".format(exp_name))
    table.field_names = ["Hyperparameter", "Value"]
    for k, v in kwargs.items():
        table.add_row([k, v])
    print("\n", table, "\n")

    # handle logger args and env function
    if 'logger_kwargs' not in kwargs:
        kwargs['logger_kwargs'] = setup_logger_kwargs(exp_name, data_dir, seed,
                                                      verbose)

    if 'env_name' in kwargs:
        import gym
        env_name = kwargs['env_name']
        kwargs['env_fn'] = lambda: gym.make(env_name)
        del kwargs['env_name']

    # fork (if applicable) and run experiment
    mpi_fork(num_cpu)
    results = algo(**kwargs)

    # print end of experiment message
    logger_kwargs = kwargs['logger_kwargs']
    print("\nEnd of Experiment.\n")
    print("Results available in {}".format(logger_kwargs['output_dir']))

    return results
예제 #2
0
def run_alg(alg_fn, env, alg_args, seed, exp_name, verbose):
    """
    Run algorithm
    """
    logger_kwargs = log.setup_logger_kwargs(exp_name,
                                            seed=seed,
                                            verbose=verbose)
    preprocess_fn, obs_dim = preprocess.get_preprocess_fn(env)
    alg_args["seed"] = seed
    alg_args["logger_kwargs"] = logger_kwargs
    alg_args["preprocess_fn"] = preprocess_fn
    alg_args["obs_dim"] = obs_dim
    alg_fn(lambda: gym.make(env), **alg_args)
예제 #3
0
    parser.add_argument("--seed", type=int, default=0)
    parser.add_argument("--render", action="store_true")
    parser.add_argument("--renderlast", action="store_true")
    parser.add_argument("--exp_name", type=str, default=None)
    parser.add_argument("--returns",
                        type=str,
                        default="simple",
                        help="Return calculation to use ['simple', 'r2g']")
    args = parser.parse_args()

    model_fn = mlp_actor
    if args.model == "cnn":
        model_fn = cnn_actor
    model_kwargs = {"hidden_sizes": args.hidden_sizes}

    print(f"\nSimple Policy Gradient using {args.returns} returns")
    exp_name = f"simplepg_{args.returns}_" + args.env if args.exp_name is None else args.exp_name
    logger_kwargs = setup_logger_kwargs(exp_name, seed=args.seed)

    simplepg(lambda: gym.make(args.env),
             model_fn,
             model_kwargs=model_kwargs,
             epochs=args.epochs,
             lr=args.lr,
             batch_size=args.batch_size,
             seed=args.seed,
             render=args.render,
             render_last=args.renderlast,
             return_fn=args.returns,
             logger_kwargs=logger_kwargs)