forked from kittykg/neural-dnf-mt-policy-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaxi_ppo_sweep.py
189 lines (153 loc) · 5.92 KB
/
taxi_ppo_sweep.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
import random
from datetime import datetime
from pathlib import Path
import traceback
import hydra
from hydra.core.hydra_config import HydraConfig
import numpy as np
from omegaconf import DictConfig
import torch
from torch.utils.tensorboard import SummaryWriter # type: ignore
import wandb
from common import synthesize
from taxi_ppo import train_ppo
from utils import post_to_discord_webhook
def train_ppo_wrapper(cfg: DictConfig):
# Randomly select a seed based on the current time
ts = datetime.now().timestamp()
random.seed(ts)
seed = random.randrange(10000)
# Set random seed
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
training_cfg = cfg["training"]
run = wandb.init(dir=HydraConfig.get().run.dir, sync_tensorboard=True)
use_ndnf = "ndnf" in training_cfg["experiment_name"]
use_mt = "mt" in training_cfg["experiment_name"]
slwc = training_cfg["share_layer_with_critic"]
# Model architecture
training_cfg["actor_latent_size"] = wandb.config.actor_latent_size
training_cfg["critic_latent_1"] = wandb.config.critic_latent_1
if not slwc:
training_cfg["critic_latent_2"] = wandb.config.critic_latent_2
else:
training_cfg["critic_latent_2"] = None
# Override the PPO parameters
training_cfg["lr_actor"] = wandb.config.lr_actor
training_cfg["lr_critic"] = wandb.config.lr_critic
training_cfg["num_envs"] = wandb.config.num_envs
training_cfg["num_steps"] = wandb.config.num_steps
training_cfg["num_minibatches"] = wandb.config.num_minibatches
training_cfg["update_epochs"] = wandb.config.update_epochs
training_cfg["gae_lambda"] = wandb.config.gae_lambda
training_cfg["clip_coef"] = wandb.config.clip_coef
training_cfg["vf_coef"] = wandb.config.vf_coef
training_cfg["ent_coef"] = wandb.config.ent_coef
if use_ndnf:
# Override the lambda parameter with the one from the sweep
training_cfg["dds"]["delta_decay_delay"] = int(
wandb.config.delta_decay_delay
)
training_cfg["dds"]["delta_decay_steps"] = int(
wandb.config.delta_decay_steps
)
# Override the auxiliary loss related parameters
training_cfg["aux_loss"][
"dis_l1_mod_lambda"
] = wandb.config.dis_l1_mod_lambda
if use_mt:
training_cfg["aux_loss"][
"mt_ce2_lambda"
] = wandb.config.mt_ce2_lambda
full_experiment_name = f"{training_cfg['experiment_name']}_{int(ts)}"
use_discord_webhook = cfg["webhook"]["use_discord_webhook"]
msg_body = None
errored = False
try:
writer_dir = Path(HydraConfig.get().run.dir) / "tb"
writer = SummaryWriter(writer_dir)
writer.add_text(
"hyperparameters",
"|param|value|\n|-|-|\n%s"
% (
"\n".join(
[
f"|{key}|{value}|"
for key, value in vars(training_cfg).items()
]
)
),
)
_, _, eval_log = train_ppo(
training_cfg, full_experiment_name, True, writer, save_model=True
)
wandb_log_dict = {}
# return_per_episode
argmax_eval_log = eval_log["argmax_eval_log"]
non_argmax_eval_log = eval_log["non_argmax_eval_log"]
argmax_return_per_episode_avg = synthesize(
argmax_eval_log["return_per_episode"]
)["mean"]
wandb_log_dict["eval/argmax_return_per_episode_avg"] = (
argmax_return_per_episode_avg
)
non_argmax_return_per_episode_avg = synthesize(
non_argmax_eval_log["return_per_episode"]
)["mean"]
wandb_log_dict["eval/non_argmax_return_per_episode_avg"] = (
non_argmax_return_per_episode_avg
)
# Aim to maximise the average return per episode
combined_metric = (
argmax_return_per_episode_avg + non_argmax_return_per_episode_avg
)
wandb_log_dict["combined_metric"] = combined_metric
if use_ndnf:
# We only check the argmax_eval_log for mutual exclusivity and
# missing actions
mutual_exclusivity = argmax_eval_log["mutual_exclusivity"]
wandb_log_dict["eval/mutual_exclusivity"] = int(
mutual_exclusivity # type: ignore
)
missing_actions = argmax_eval_log["missing_actions"]
wandb_log_dict["eval/missing_actions"] = int(
missing_actions # type: ignore
)
if not mutual_exclusivity:
# Mutual exclusivity is not satisfied
combined_metric -= 1000
if missing_actions:
# There are missing actions
combined_metric -= 1000
wandb.log(wandb_log_dict)
if use_discord_webhook:
msg_body = "Success!"
except BaseException as e:
if use_discord_webhook:
msg_body = "Check the logs for more details."
print(traceback.format_exc())
errored = True
finally:
if use_discord_webhook:
if msg_body is None:
msg_body = ""
webhook_url = cfg["webhook"]["discord_webhook_url"]
post_to_discord_webhook(
webhook_url=webhook_url,
experiment_name=full_experiment_name,
message_body=msg_body,
errored=errored,
)
wandb.finish()
@hydra.main(version_base=None, config_path="conf", config_name="config")
def run_experiment(cfg: DictConfig) -> None:
# torch.autograd.set_detect_anomaly(True)
use_wandb = cfg["wandb"]["use_wandb"]
assert use_wandb, "Must use wandb for hyperparameter search"
train_ppo_wrapper(cfg)
if __name__ == "__main__":
import multiprocessing as mp
if mp.get_start_method() != "fork":
mp.set_start_method("fork", force=True)
run_experiment()