forked from araffin/rl-baselines-zoo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
339 lines (288 loc) · 15.2 KB
/
train.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import argparse
import datetime
import difflib
import os
from collections import OrderedDict
from pprint import pprint
import warnings
import importlib
import custom_gym_envs
# For pybullet envs
warnings.filterwarnings("ignore")
import gym
try:
import pybullet_envs
except ImportError:
pybullet_envs = None
import numpy as np
import yaml
try:
import highway_env
except ImportError:
highway_env = None
from mpi4py import MPI
from stable_baselines.common import set_global_seeds
from stable_baselines.common.cmd_util import make_atari_env
from stable_baselines.common.vec_env import VecFrameStack, SubprocVecEnv, VecNormalize, DummyVecEnv
from stable_baselines.ddpg import AdaptiveParamNoiseSpec, NormalActionNoise, OrnsteinUhlenbeckActionNoise
from stable_baselines.ppo2.ppo2 import constfn
from utils import make_env, ALGOS, linear_schedule, get_latest_run_id, get_wrapper_class
from utils.hyperparams_opt import hyperparam_optimization
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--env', type=str, nargs='+', default=["CartPole-v1"], help='environment ID(s)')
parser.add_argument('-tb', '--tensorboard-log', help='Tensorboard log dir', default='', type=str)
parser.add_argument('-i', '--trained-agent', help='Path to a pretrained agent to continue training',
default='', type=str)
parser.add_argument('--algo', help='RL Algorithm', default='ppo2',
type=str, required=False, choices=list(ALGOS.keys()))
parser.add_argument('-n', '--n-timesteps', help='Overwrite the number of timesteps', default=-1,
type=int)
parser.add_argument('--log-interval', help='Override log interval (default: -1, no change)', default=-1,
type=int)
parser.add_argument('-f', '--log-folder', help='Log folder', type=str, default='logs')
parser.add_argument('--seed', help='Random generator seed', type=int, default=0)
parser.add_argument('--n-trials', help='Number of trials for optimizing hyperparameters', type=int, default=10)
parser.add_argument('-optimize', '--optimize-hyperparameters', action='store_true', default=False,
help='Run hyperparameters search')
parser.add_argument('--n-jobs', help='Number of parallel jobs when optimizing hyperparameters', type=int, default=1)
parser.add_argument('--sampler', help='Sampler to use when optimizing hyperparameters', type=str,
default='random', choices=['random', 'tpe'])
parser.add_argument('--pruner', help='Pruner to use when optimizing hyperparameters', type=str,
default='median', choices=['halving', 'median', 'none'])
parser.add_argument('--verbose', help='Verbose mode (0: no output, 1: INFO)', default=1,
type=int)
parser.add_argument('--gym-packages', type=str, nargs='+', default=[], help='Additional external Gym environemnt package modules to import (e.g. gym_minigrid)')
args = parser.parse_args()
# Going through custom gym packages to let them register in the global registory
for env_module in args.gym_packages:
importlib.import_module(env_module)
env_ids = args.env
registered_envs = set(gym.envs.registry.env_specs.keys())
for env_id in env_ids:
# If the environment is not found, suggest the closest match
if env_id not in registered_envs:
try:
closest_match = difflib.get_close_matches(env_id, registered_envs, n=1)[0]
except IndexError:
closest_match = "'no close match found...'"
raise ValueError('{} not found in gym registry, you maybe meant {}?'.format(env_id, closest_match))
set_global_seeds(args.seed)
if args.trained_agent != "":
assert args.trained_agent.endswith('.pkl') and os.path.isfile(args.trained_agent), \
"The trained_agent must be a valid path to a .pkl file"
rank = 0
if MPI.COMM_WORLD.Get_size() > 1:
print("Using MPI for multiprocessing with {} workers".format(MPI.COMM_WORLD.Get_size()))
rank = MPI.COMM_WORLD.Get_rank()
print("Worker rank: {}".format(rank))
args.seed += rank
if rank != 0:
args.verbose = 0
args.tensorboard_log = ''
for env_id in env_ids:
tensorboard_log = None if args.tensorboard_log == '' else os.path.join(args.tensorboard_log, env_id)
is_atari = False
if 'NoFrameskip' in env_id:
is_atari = True
print("=" * 10, env_id, "=" * 10)
# Load hyperparameters from yaml file
with open('hyperparams/{}.yml'.format(args.algo), 'r') as f:
hyperparams_dict = yaml.load(f)
if env_id in list(hyperparams_dict.keys()):
hyperparams = hyperparams_dict[env_id]
elif is_atari:
hyperparams = hyperparams_dict['atari']
else:
raise ValueError("Hyperparameters not found for {}-{}".format(args.algo, env_id))
# Sort hyperparams that will be saved
saved_hyperparams = OrderedDict([(key, hyperparams[key]) for key in sorted(hyperparams.keys())])
algo_ = args.algo
# HER is only a wrapper around an algo
if args.algo == 'her':
algo_ = saved_hyperparams['model_class']
assert algo_ in {'sac', 'ddpg', 'dqn'}, "{} is not compatible with HER".format(algo_)
# Retrieve the model class
hyperparams['model_class'] = ALGOS[saved_hyperparams['model_class']]
if args.verbose > 0:
pprint(saved_hyperparams)
n_envs = hyperparams.get('n_envs', 1)
if args.verbose > 0:
print("Using {} environments".format(n_envs))
# Create learning rate schedules for ppo2 and sac
if algo_ in ["ppo2", "sac"]:
for key in ['learning_rate', 'cliprange']:
if key not in hyperparams:
continue
if isinstance(hyperparams[key], str):
schedule, initial_value = hyperparams[key].split('_')
initial_value = float(initial_value)
hyperparams[key] = linear_schedule(initial_value)
elif isinstance(hyperparams[key], float):
hyperparams[key] = constfn(hyperparams[key])
else:
raise ValueError('Invalid valid for {}: {}'.format(key, hyperparams[key]))
# Should we overwrite the number of timesteps?
if args.n_timesteps > 0:
if args.verbose:
print("Overwriting n_timesteps with n={}".format(args.n_timesteps))
n_timesteps = args.n_timesteps
else:
n_timesteps = int(hyperparams['n_timesteps'])
normalize = False
normalize_kwargs = {}
if 'normalize' in hyperparams.keys():
normalize = hyperparams['normalize']
if isinstance(normalize, str):
normalize_kwargs = eval(normalize)
normalize = True
del hyperparams['normalize']
if 'policy_kwargs' in hyperparams.keys():
hyperparams['policy_kwargs'] = eval(hyperparams['policy_kwargs'])
# Delete keys so the dict can be pass to the model constructor
if 'n_envs' in hyperparams.keys():
del hyperparams['n_envs']
del hyperparams['n_timesteps']
# obtain a class object from a wrapper name string in hyperparams
# and delete the entry
env_wrapper = get_wrapper_class(hyperparams)
if 'env_wrapper' in hyperparams.keys():
del hyperparams['env_wrapper']
def create_env(n_envs):
"""
Create the environment and wrap it if necessary
:param n_envs: (int)
:return: (gym.Env)
"""
global hyperparams
if is_atari:
if args.verbose > 0:
print("Using Atari wrapper")
env = make_atari_env(env_id, num_env=n_envs, seed=args.seed)
# Frame-stacking with 4 frames
env = VecFrameStack(env, n_stack=4)
elif algo_ in ['dqn', 'ddpg']:
if hyperparams.get('normalize', False):
print("WARNING: normalization not supported yet for DDPG/DQN")
env = gym.make(env_id)
env.seed(args.seed)
if env_wrapper is not None:
env = env_wrapper(env)
else:
if n_envs == 1:
env = DummyVecEnv([make_env(env_id, 0, args.seed, wrapper_class=env_wrapper)])
else:
# env = SubprocVecEnv([make_env(env_id, i, args.seed) for i in range(n_envs)])
# On most env, SubprocVecEnv does not help and is quite memory hungry
env = DummyVecEnv([make_env(env_id, i, args.seed, wrapper_class=env_wrapper) for i in range(n_envs)])
if normalize:
if args.verbose > 0:
if len(normalize_kwargs) > 0:
print("Normalization activated: {}".format(normalize_kwargs))
else:
print("Normalizing input and reward")
env = VecNormalize(env, **normalize_kwargs)
# Optional Frame-stacking
if hyperparams.get('frame_stack', False):
n_stack = hyperparams['frame_stack']
env = VecFrameStack(env, n_stack)
print("Stacking {} frames".format(n_stack))
del hyperparams['frame_stack']
return env
env = create_env(n_envs)
# Stop env processes to free memory
if args.optimize_hyperparameters and n_envs > 1:
env.close()
# Parse noise string for DDPG and SAC
if algo_ in ['ddpg', 'sac'] and hyperparams.get('noise_type') is not None:
noise_type = hyperparams['noise_type'].strip()
noise_std = hyperparams['noise_std']
n_actions = env.action_space.shape[0]
if 'adaptive-param' in noise_type:
assert algo_ == 'ddpg', 'Parameter is not supported by SAC'
hyperparams['param_noise'] = AdaptiveParamNoiseSpec(initial_stddev=noise_std,
desired_action_stddev=noise_std)
elif 'normal' in noise_type:
hyperparams['action_noise'] = NormalActionNoise(mean=np.zeros(n_actions),
sigma=noise_std * np.ones(n_actions))
elif 'ornstein-uhlenbeck' in noise_type:
hyperparams['action_noise'] = OrnsteinUhlenbeckActionNoise(mean=np.zeros(n_actions),
sigma=noise_std * np.ones(n_actions))
else:
raise RuntimeError('Unknown noise type "{}"'.format(noise_type))
print("Applying {} noise with std {}".format(noise_type, noise_std))
del hyperparams['noise_type']
del hyperparams['noise_std']
if args.trained_agent.endswith('.pkl') and os.path.isfile(args.trained_agent):
# Continue training
print("Loading pretrained agent")
# Policy should not be changed
del hyperparams['policy']
model = ALGOS[args.algo].load(args.trained_agent, env=env,
tensorboard_log=tensorboard_log, verbose=args.verbose, **hyperparams)
exp_folder = args.trained_agent.split('.pkl')[0]
if normalize:
print("Loading saved running average")
env.load_running_average(exp_folder)
elif args.optimize_hyperparameters:
if args.verbose > 0:
print("Optimizing hyperparameters")
def create_model(*_args, **kwargs):
"""
Helper to create a model with different hyperparameters
"""
return ALGOS[args.algo](env=create_env(n_envs), tensorboard_log=tensorboard_log,
verbose=0, **kwargs)
data_frame = hyperparam_optimization(args.algo, create_model, create_env, n_trials=args.n_trials,
n_timesteps=n_timesteps, hyperparams=hyperparams,
n_jobs=args.n_jobs, seed=args.seed,
sampler_method=args.sampler, pruner_method=args.pruner,
verbose=args.verbose)
report_name = "report_{}_{}-trials-{}-{}-{}.csv".format(env_id, args.n_trials, n_timesteps,
args.sampler, args.pruner)
log_path = os.path.join(args.log_folder, args.algo, report_name)
if args.verbose:
print("Writing report to {}".format(log_path))
os.makedirs(os.path.dirname(log_path), exist_ok=True)
data_frame.to_csv(log_path)
exit()
else:
# Train an agent from scratch
model = ALGOS[args.algo](env=env, tensorboard_log=tensorboard_log, verbose=args.verbose, **hyperparams)
def get_expt_name(algo_name, env_name):
now = datetime.datetime.now()
expt_name = "{}_{}_{}".format(algo_name, env_name, now.strftime("%m%d_%H%M"))
return expt_name
kwargs = {}
if args.log_interval > -1:
kwargs = {'log_interval': args.log_interval}
expt_name = get_expt_name(args.algo, env_id)
kwargs.update({'tb_log_name': expt_name})
start = datetime.datetime.now()
model.learn(n_timesteps, **kwargs)
end = datetime.datetime.now()
training_time = end-start
days = divmod(training_time.total_seconds(), 86400) # Get days (without [0]!)
hours = divmod(days[1], 3600) # Use remainder of days to calc hours
minutes = divmod(hours[1], 60) # Use remainder of hours to calc minutes
seconds = divmod(minutes[1], 1) # Use remainder of minutes to calc seconds
print("Training time: {0} days, {1} hours, {2} minutes and {3} seconds for {4} steps".format(
int(days[0]), int(hours[0]), int(minutes[0]), int(seconds[0]), n_timesteps))
# Save trained model
log_path = "{}/{}/".format(args.log_folder, env_id)
save_path = os.path.join(log_path, expt_name)
params_path = "{}/{}".format(save_path, env_id)
os.makedirs(params_path, exist_ok=True)
# Only save worker of rank 0 when using mpi
if rank == 0:
print("Saving to {}".format(save_path))
model.save("{}/{}".format(save_path, env_id))
# Save hyperparams
with open(os.path.join(params_path, 'config.yml'), 'w') as f:
yaml.dump(saved_hyperparams, f)
if normalize:
# Unwrap
if isinstance(env, VecFrameStack):
env = env.venv
# Important: save the running average, for testing the agent we need that normalization
env.save_running_average(params_path)