-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSurprise_experiment.py
300 lines (241 loc) · 8.6 KB
/
Surprise_experiment.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# RL algorithm
from sandbox.surprisebased.algos.plus.trpo_plus import TRPO
# Exploration incentives
from exploration_bonuses.surprisal_bonus import SurprisalBonus
from exploration_bonuses.prediction_error_bonus import PredictionErrorBonus
from exploration_bonuses.approx_kl_div_n_step_bonus import ApproxKLNStepBonus
# Gym
from rllab.envs.gym_env import GymEnv
from sandbox.surprisebased.envs.normalized_atari_env import NormalizedAtariEnv
# Sparse reward tasks
# ---easier (classic control)
from sandbox.surprisebased.envs.mountain_car_env_x import MountainCarEnvX
from sandbox.surprisebased.envs.cartpole_swingup_env_x import CartpoleSwingupEnvX
# ---harder (locomotion)
from sandbox.surprisebased.envs.half_cheetah_env_x import HalfCheetahEnvX
from sandbox.surprisebased.envs.swimmer_env_x import SwimmerEnvX
from rllab.envs.mujoco.gather.swimmer_gather_env import SwimmerGatherEnv
from rllab.envs.mujoco.gather.ant_gather_env import AntGatherEnv
from rllab.envs.mujoco.maze.swimmer_maze_env import SwimmerMazeEnv
from rllab.envs.mujoco.maze.ant_maze_env import AntMazeEnv
# Baselines
from rllab.baselines.linear_feature_baseline import LinearFeatureBaseline
from rllab.baselines.gaussian_mlp_baseline import GaussianMLPBaseline
import lasagne.nonlinearities as NL
# Policy
from rllab.policies.categorical_mlp_policy import CategoricalMLPPolicy
from rllab.policies.gaussian_mlp_policy import GaussianMLPPolicy
from rllab.optimizers.conjugate_gradient_optimizer import ConjugateGradientOptimizer
# Instrumentation
from rllab.envs.normalized_env import normalize
from rllab.misc.instrument import stub, run_experiment_lite
#from custom_plotter import *
stub(globals())
#===========================#
# SPARSE REWARD EXPERIMENTS #
#===========================#
mc_experiment = {
'env_name': 'MountainCarEnvX',
'task_type': 'classic',
'env_call': MountainCarEnvX,
'normalize_env': False
}
cps_experiment = {
'env_name': 'CartpoleSwingupEnvX',
'task_type': 'classic',
'env_call': CartpoleSwingupEnvX,
'normalize_env': False
}
hc_experiment = {
'env_name': 'HalfCheetahEnvX',
'task_type': 'locomotion',
'env_call': HalfCheetahEnvX,
'normalize_env': True
}
swim_experiment = {
'env_name': 'SwimmerEnvX',
'task_type': 'locomotion',
'env_call': SwimmerEnvX,
'normalize_env': True
}
sg_experiment = {
'env_name': 'SwimmerGather',
'task_type': 'heirarchical',
'env_call': SwimmerGatherEnv,
'normalize_env': True
}
#===================#
# ATARI EXPERIMENTS #
#===================#
ven_experiment = {
'env_name': 'Venture-ram-v0',
'task_type': 'atari'
}
bh_experiment = {
'env_name': 'BankHeist-ram-v0',
'task_type': 'atari'
}
fw_experiment = {
'env_name': 'Freeway-ram-v0',
'task_type': 'atari'
}
pong_experiment = {
'env_name': 'Pong-ram-v0',
'task_type': 'atari'
}
experiment = cps_experiment
experiment_name = 'TRPO-surprisal-demo-' + experiment['env_name']
for j in range(5):
task_type = experiment['task_type']
if task_type=='atari':
env = NormalizedAtariEnv(GymEnv(experiment['env_name'],record_video=False))
else:
env = experiment['env_call']()
if experiment['normalize_env']:
env = normalize(env)
if task_type == 'classic':
trpo_max_path_length = 500
trpo_batch_size = 5000
trpo_subsample_factor = 1
trpo_step_size = 0.01
expl_lambda = 0.001
policy = GaussianMLPPolicy(
env_spec=env.spec,
hidden_sizes=(32,),
)
baseline = GaussianMLPBaseline(
env_spec=env.spec,
regressor_args={
'hidden_sizes':(32,),
'hidden_nonlinearity': NL.tanh,
'learn_std':False,
'step_size':0.01,
'optimizer':ConjugateGradientOptimizer(subsample_factor=trpo_subsample_factor)
}
)
elif task_type == 'locomotion':
trpo_max_path_length = 500
trpo_batch_size = 5000
trpo_subsample_factor = 1
trpo_step_size = 0.05
expl_lambda = 0.001
policy = GaussianMLPPolicy(
env_spec=env.spec,
hidden_sizes=(64,32),
)
baseline = LinearFeatureBaseline(env_spec=env.spec)
elif task_type == 'heirarchical':
trpo_max_path_length = 500
trpo_batch_size = 50000
trpo_subsample_factor = 0.1
trpo_step_size = 0.01
expl_lambda = 0.0001
policy = GaussianMLPPolicy(
env_spec=env.spec,
hidden_sizes=(64,32),
)
baseline = LinearFeatureBaseline(env_spec=env.spec)
elif task_type == 'atari':
trpo_max_path_length = 7000
trpo_batch_size = 50000
trpo_subsample_factor = 0.2
trpo_step_size = 0.01
expl_lambda = 0.005
policy = CategoricalMLPPolicy(
env_spec = env.spec,
hidden_sizes=(64,32),
)
baseline = GaussianMLPBaseline(
env_spec=env.spec,
regressor_args={
'hidden_sizes':(64,32),
'hidden_nonlinearity': NL.tanh,
'learn_std':False,
'step_size':0.01,
'optimizer':ConjugateGradientOptimizer(subsample_factor=trpo_subsample_factor)
}
)
dynamics_batch_size = 5000 #10000
dynamics_replay_size = 5000000 #1000000
dynamics_hidden_sizes = (64,64)#(32,)
dynamics_step_size = 0.001#0.01
dynamics_subsample_factor = 1 #0.5
dynamics_weight_decay = 1
sur = SurprisalBonus(
env.spec,
use_grads=False,
normalize_bonus=False,
use_replay_pool=True,
weight_decay=dynamics_weight_decay,
batch_size=dynamics_batch_size,
max_pool_size=dynamics_replay_size,
hidden_sizes=dynamics_hidden_sizes,
hidden_nonlinearity=NL.tanh,
regressor_args={
'use_trust_region':True,
'step_size':dynamics_step_size,
'optimizer':ConjugateGradientOptimizer(subsample_factor=dynamics_subsample_factor)
}
)
pred = PredictionErrorBonus(
env.spec,
normalize_bonus=False,
use_replay_pool=True,
weight_decay=dynamics_weight_decay,
batch_size=dynamics_batch_size,
max_pool_size=dynamics_replay_size,
hidden_sizes=dynamics_hidden_sizes,
hidden_nonlinearity=NL.tanh,
regressor_args={
'use_trust_region':True,
'step_size':dynamics_step_size,
'optimizer':ConjugateGradientOptimizer(subsample_factor=dynamics_subsample_factor)
},
use_square_error=True,
)
akln = ApproxKLNStepBonus(
env.spec,
lag_steps=1,
use_replay_pool=True,
weight_decay=dynamics_weight_decay,
batch_size=dynamics_batch_size,
max_pool_size=dynamics_replay_size,
hidden_sizes=dynamics_hidden_sizes,
hidden_nonlinearity=NL.tanh,
regressor_args={
'use_trust_region':True,
'step_size':dynamics_step_size,
'optimizer':ConjugateGradientOptimizer(subsample_factor=dynamics_subsample_factor)
}
)
algo = TRPO(
env=env,
exploration_bonus=sur,
#exploration_bonus=pred,
#exploration_bonus=akln,
exploration_lambda=expl_lambda,
normalize_bonus=True,
nonnegative_bonus_mean=False, #True,
all_paths=True,
use_bonus_in_baseline=False,
policy=policy,
baseline=baseline,
batch_size=trpo_batch_size,
max_path_length=trpo_max_path_length,
n_itr=500,
discount=0.995,
gae_lambda=0.95,
step_size=trpo_step_size,
min_num_paths=0,
optimizer=ConjugateGradientOptimizer(subsample_factor=trpo_subsample_factor),
#plot=True,
)
run_experiment_lite(
algo.train(),
n_parallel=4,
snapshot_mode="last",
seed=j,
exp_prefix=experiment_name,
#mode="ec2",
#plot=True,
)