-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrun_trpo.py
More file actions
70 lines (60 loc) · 2.41 KB
/
run_trpo.py
File metadata and controls
70 lines (60 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import argparse
import os.path as osp
import pickle
import tensorflow as tf
from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline
from rllab.envs.gym_env import GymEnv
from rllab.envs.normalized_env import normalize
from rllab.misc import ext
from rllab.misc.instrument import run_experiment_lite, stub
from sandbox.rocky.tf.algos.trpo import TRPO
from sandbox.rocky.tf.envs.base import TfEnv
from sandbox.rocky.tf.optimizers.conjugate_gradient_optimizer import (ConjugateGradientOptimizer,
FiniteDifferenceHvp)
from sandbox.rocky.tf.policies.gaussian_mlp_policy import GaussianMLPPolicy
parser = argparse.ArgumentParser()
parser.add_argument("env", help="The environment name from OpenAIGym environments")
parser.add_argument("--num_epochs", default=250, type=int)
parser.add_argument("--data_dir", default="./data_trpo/")
parser.add_argument("--use_ec2", action="store_true", help="Use your ec2 instances if configured")
parser.add_argument("--dont_terminate_machine", action="store_false", help="Whether to terminate your spot instance or not. Be careful.")
args = parser.parse_args()
stub(globals())
ext.set_seed(1)
gymenv = GymEnv(args.env, force_reset=True, record_video=True, record_log=True)
env = TfEnv(normalize(gymenv))
policy = GaussianMLPPolicy(
name="policy",
env_spec=env.spec,
# The neural network policy should have two hidden layers, each with 32 hidden units.
hidden_sizes=(100, 50, 25),
hidden_nonlinearity=tf.nn.relu,
)
baseline = LinearFeatureBaseline(env_spec=env.spec)
algo = TRPO(
env=env,
policy=policy,
baseline=baseline,
batch_size=5000,
max_path_length=env.horizon,
n_itr=args.num_epochs,
discount=0.99,
step_size=0.01,
optimizer=ConjugateGradientOptimizer(hvp_approach=FiniteDifferenceHvp(base_eps=1e-5))
)
run_experiment_lite(
algo.train(),
log_dir=None if args.use_ec2 else args.data_dir,
# Number of parallel workers for sampling
n_parallel=1,
# Only keep the snapshot parameters for the last iteration
snapshot_mode="last",
# Specifies the seed for the experiment. If this is not provided, a random seed
# will be used
exp_prefix="TRPO_" + args.env,
seed=1,
mode="ec2" if args.use_ec2 else "local",
plot=False,
terminate_machine=args.dont_terminate_machine,
added_project_directories=[osp.abspath(osp.join(osp.dirname(__file__), '.'))]
)