forked from eugenevinitsky/sequential_social_dilemma_games
-
Notifications
You must be signed in to change notification settings - Fork 2
/
train.py
377 lines (331 loc) · 13.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import argparse
import copy
import random
import sys
from datetime import datetime
import pytz
import ray
from ray import tune
from ray.rllib.agents.registry import get_agent_class
from ray.rllib.models import ModelCatalog
from ray.tune import Experiment
from ray.tune.registry import register_env
from ray.tune.schedulers import PopulationBasedTraining
from algorithms.a3c_baseline import build_a3c_baseline_trainer
from algorithms.a3c_moa import build_a3c_moa_trainer
from algorithms.impala_baseline import build_impala_baseline_trainer
from algorithms.impala_moa import build_impala_moa_trainer
from algorithms.ppo_baseline import build_ppo_baseline_trainer
from algorithms.ppo_moa import build_ppo_moa_trainer
from algorithms.ppo_scm import build_ppo_scm_trainer
from config.default_args import add_default_args
from models.baseline_model import BaselineModel
from models.moa_model import MOAModel
from models.scm_model import SocialCuriosityModule
from social_dilemmas.envs.env_creator import get_env_creator
from utility_funcs import update_nested_dict
parser = argparse.ArgumentParser()
add_default_args(parser)
def build_experiment_config_dict(args):
"""
Create a config dict for a single Experiment object.
:param args: The parsed arguments.
:return: An Experiment config dict.
"""
env_creator = get_env_creator(args.env, args.num_agents, args)
env_name = args.env + "_env"
register_env(env_name, env_creator)
single_env = env_creator(args.num_agents)
obs_space = single_env.observation_space
act_space = single_env.action_space
model_name = args.model + "_lstm"
if args.model == "scm":
ModelCatalog.register_custom_model(model_name, SocialCuriosityModule)
elif args.model == "moa":
ModelCatalog.register_custom_model(model_name, MOAModel)
elif args.model == "baseline":
ModelCatalog.register_custom_model(model_name, BaselineModel)
# Each policy can have a different configuration (including custom model)
def gen_policy():
return None, obs_space, act_space, {"custom_model": model_name}
# Create 1 distinct policy per agent
policy_graphs = {}
for i in range(args.num_agents):
policy_graphs["agent-" + str(i)] = gen_policy()
def policy_mapping_fn(agent_id):
return agent_id
agent_cls = get_agent_class(args.algorithm)
config = copy.deepcopy(agent_cls._default_config)
config["env"] = env_name
config["eager"] = args.eager_mode
# information for replay
config["env_config"]["func_create"] = env_creator
config["env_config"]["env_name"] = env_name
if env_name == "switch_env":
config["env_config"]["num_switches"] = args.num_switches
conv_filters = [[6, [3, 3], 1]]
fcnet_hiddens = [32, 32]
lstm_cell_size = 128
train_batch_size = (
args.train_batch_size
if args.train_batch_size is not None
else max(1, args.num_workers) * args.num_envs_per_worker * args.rollout_fragment_length
)
lr_schedule = (
list(zip(args.lr_schedule_steps, args.lr_schedule_weights))
if args.lr_schedule_steps is not None and args.lr_schedule_weights is not None
else None
)
# hyperparams
update_nested_dict(
config,
{
"horizon": 1000,
"gamma": 0.99,
"lr": args.lr,
"lr_schedule": lr_schedule,
"rollout_fragment_length": args.rollout_fragment_length,
"train_batch_size": train_batch_size,
"num_workers": args.num_workers,
"num_envs_per_worker": args.num_envs_per_worker,
"num_gpus": args.gpus_for_driver, # The number of GPUs for the driver
"num_cpus_for_driver": args.cpus_for_driver,
"num_gpus_per_worker": args.gpus_per_worker, # Can be a fraction
"num_cpus_per_worker": args.cpus_per_worker, # Can be a fraction
"entropy_coeff": args.entropy_coeff,
"grad_clip": args.grad_clip,
"multiagent": {"policies": policy_graphs, "policy_mapping_fn": policy_mapping_fn},
"callbacks": single_env.get_environment_callbacks(),
"model": {
"custom_model": model_name,
"use_lstm": False,
"conv_filters": conv_filters,
"fcnet_hiddens": fcnet_hiddens,
"custom_options": {
"cell_size": lstm_cell_size,
"num_other_agents": args.num_agents - 1,
},
},
},
)
if args.model != "baseline":
config["model"]["custom_options"].update(
{
"moa_loss_weight": args.moa_loss_weight,
"influence_reward_clip": 10,
"influence_reward_weight": args.influence_reward_weight,
"influence_reward_schedule_steps": args.influence_reward_schedule_steps,
"influence_reward_schedule_weights": args.influence_reward_schedule_weights,
"return_agent_actions": True,
"influence_divergence_measure": "kl",
"train_moa_only_when_visible": True,
"influence_only_when_visible": True,
}
)
if args.model == "scm":
config["model"]["custom_options"].update(
{
"scm_loss_weight": args.scm_loss_weight,
"curiosity_reward_clip": 10,
"curiosity_reward_weight": args.curiosity_reward_weight,
"curiosity_reward_schedule_steps": args.curiosity_reward_schedule_steps,
"curiosity_reward_schedule_weights": args.curiosity_reward_schedule_weights,
"scm_forward_vs_inverse_loss_weight": args.scm_forward_vs_inverse_loss_weight,
}
)
if args.tune_hparams:
tune_dict = create_hparam_tune_dict(model=args.model, is_config=True)
update_nested_dict(config, tune_dict)
if args.algorithm == "PPO":
config.update(
{
"num_sgd_iter": 10,
"sgd_minibatch_size": args.ppo_sgd_minibatch_size
if args.ppo_sgd_minibatch_size is not None
else train_batch_size / 4,
"vf_loss_coeff": 1e-4,
"vf_share_layers": True,
}
)
elif args.algorithm == "A3C" or args.algorithm == "IMPALA":
config.update({"vf_loss_coeff": 0.1})
else:
sys.exit("The only available algorithms are A3C, PPO and IMPALA")
return config
def get_trainer(args, config):
"""
Creates a trainer depending on what args are specified.
:param args: The parsed arguments.
:param config: The config dict that is provided to the trainer.
:return: A new trainer.
"""
if args.model == "baseline":
if args.algorithm == "A3C":
trainer = build_a3c_baseline_trainer(config)
if args.algorithm == "PPO":
trainer = build_ppo_baseline_trainer(config)
if args.algorithm == "IMPALA":
trainer = build_impala_baseline_trainer(config)
elif args.model == "moa":
if args.algorithm == "A3C":
trainer = build_a3c_moa_trainer(config)
if args.algorithm == "PPO":
trainer = build_ppo_moa_trainer(config)
if args.algorithm == "IMPALA":
trainer = build_impala_moa_trainer(config)
elif args.model == "scm":
if args.algorithm == "A3C":
# trainer = build_a3c_scm_trainer(config)
raise NotImplementedError
if args.algorithm == "PPO":
trainer = build_ppo_scm_trainer(config)
if args.algorithm == "IMPALA":
# trainer = build_impala_scm_trainer(config)
raise NotImplementedError
if trainer is None:
raise NotImplementedError("The provided combination of model and algorithm was not found.")
return trainer
def initialize_ray(args):
"""
Initialize ray and automatically turn on local mode when debugging.
:param args: The parsed arguments.
"""
if sys.gettrace() is not None:
print(
"Debug mode detected through sys.gettrace(), turning on ray local mode. Saving"
" experiment under ray_results/debug_experiment"
)
args.local_mode = True
if args.multi_node and args.local_mode:
sys.exit("You cannot have both local mode and multi node on at the same time")
ray.init(
address=args.address,
local_mode=args.local_mode,
memory=args.memory,
object_store_memory=args.object_store_memory,
redis_max_memory=args.redis_max_memory,
include_webui=False,
)
def get_experiment_name(args):
"""
Build an experiment name based on environment, model and algorithm.
:param args: The parsed arguments.
:return: The experiment name.
"""
if sys.gettrace() is not None:
exp_name = "debug_experiment"
elif args.exp_name is None:
exp_name = args.env + "_" + args.model + "_" + args.algorithm
else:
exp_name = args.exp_name
return exp_name
def build_experiment_dict(args, experiment_name, trainer, config):
"""
Creates all parameters needed to create an Experiment object and puts them into a dict.
:param args: The parsed arguments .
:param experiment_name: The experiment name.
:param trainer: The trainer used for the experiment.
:param config: The config dict with experiment parameters.
:return: A dict that can be unpacked to create an Experiment object.
"""
experiment_dict = {
"name": experiment_name,
"run": trainer,
"stop": {},
"checkpoint_freq": args.checkpoint_frequency,
"config": config,
"num_samples": args.num_samples,
"max_failures": -1,
}
if args.stop_at_episode_reward_min is not None:
experiment_dict["stop"]["episode_reward_min"] = args.stop_at_episode_reward_min
if args.stop_at_timesteps_total is not None:
experiment_dict["stop"]["timesteps_total"] = args.stop_at_timesteps_total
if args.use_s3:
date = datetime.now(tz=pytz.utc)
date = date.astimezone(pytz.timezone("US/Pacific")).strftime("%m-%d-%Y")
s3_string = "s3://ssd-reproduce/" + date + "/" + experiment_name
experiment_dict["upload_dir"] = s3_string
return experiment_dict
def create_experiment(args):
"""
Create a single experiment from arguments.
:param args: The parsed arguments.
:return: A new experiment with its own trainer.
"""
experiment_name = get_experiment_name(args)
config = build_experiment_config_dict(args)
trainer = get_trainer(args=args, config=config)
experiment_dict = build_experiment_dict(args, experiment_name, trainer, config)
return Experiment(**experiment_dict)
def create_hparam_tune_dict(model, is_config=False):
"""
Create a hyperparameter tuning dict for population-based training.
:param is_config: Whether these hyperparameters are being used in the config dict or not.
When used for the config dict, all hyperparameter-generating functions need to be wrapped with
tune.sample_from, so we do this automatically here.
When it is not used for the config dict, it is for PBT initialization, where a lambda is needed
as a function wrapper.
:return: The hyperparameter tune dict.
"""
def wrapper(fn):
if is_config:
return tune.sample_from(lambda spec: fn)
else:
return lambda: fn
baseline_options = {}
model_options = {}
if model == "baseline":
baseline_options = {
"entropy_coeff": wrapper(random.expovariate(1000)),
"lr": wrapper(random.uniform(0.00001, 0.01)),
}
if model == "moa":
model_options = {
"moa_loss_weight": wrapper(random.expovariate(15)),
"influence_reward_weight": wrapper(random.expovariate(1)),
}
elif model == "scm":
model_options = {
"scm_loss_weight": wrapper(random.expovariate(2)),
"curiosity_reward_weight": wrapper(random.expovariate(1)),
"scm_forward_vs_inverse_loss_weight": wrapper(random.uniform(0, 1)),
}
hparam_dict = {
**baseline_options,
"model": {"custom_options": model_options},
}
return hparam_dict
def create_pbt_scheduler(model):
"""
Create a population-based training (PBT) scheduler.
:return: A new PBT scheduler.
"""
hyperparam_mutations = create_hparam_tune_dict(model=model, is_config=False)
pbt = PopulationBasedTraining(
time_attr="training_iteration",
perturbation_interval=10,
metric="episode_reward_mean",
mode="max",
hyperparam_mutations=hyperparam_mutations,
)
return pbt
def run(args, experiments):
"""
Run one or more experiments, with ray settings contained in args.
:param args: The args to initialize ray with
:param experiments: A list of experiments to run
"""
initialize_ray(args)
scheduler = create_pbt_scheduler(args.model) if args.tune_hparams else None
tune.run_experiments(
experiments,
queue_trials=args.use_s3,
resume=args.resume,
scheduler=scheduler,
reuse_actors=args.tune_hparams,
)
if __name__ == "__main__":
parsed_args = parser.parse_args()
experiment = create_experiment(parsed_args)
run(parsed_args, experiment)