-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathredo_dqn.py
More file actions
233 lines (201 loc) · 8.47 KB
/
redo_dqn.py
File metadata and controls
233 lines (201 loc) · 8.47 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
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
# docs and experiment results can be found at https://docs.cleanrl.dev/rl-algorithms/dqn/#dqn_ataripy
import os
import random
import time
from pathlib import Path
import gymnasium as gym
import numpy as np
import torch
import torch.nn.functional as F
import torch.optim as optim
import tyro
import wandb
from src.agent import QNetwork, linear_schedule
from src.buffer import ReplayBuffer
from src.config import Config
from src.redo import run_redo
from src.utils import lecun_normal_initializer, make_env, set_cuda_configuration
def dqn_loss(
q_network: QNetwork,
target_network: QNetwork,
obs: torch.Tensor,
next_obs: torch.Tensor,
actions: torch.Tensor,
rewards: torch.Tensor,
dones: torch.Tensor,
gamma: float,
) -> tuple[torch.Tensor, torch.Tensor]:
"""Compute the double DQN loss."""
with torch.no_grad():
# Get value estimates from the target network
target_vals = target_network.forward(next_obs)
# Select actions through the policy network
policy_actions = q_network(next_obs).argmax(dim=1)
target_max = target_vals[range(len(target_vals)), policy_actions]
# Calculate Q-target
td_target = rewards.flatten() + gamma * target_max * (1 - dones.flatten())
old_val = q_network(obs).gather(1, actions).squeeze()
return F.mse_loss(td_target, old_val), old_val
def main(cfg: Config) -> None:
"""Main training method for ReDO DQN."""
run_name = f"{cfg.env_id}__{cfg.exp_name}__{cfg.seed}__{int(time.time())}"
wandb.init(
project=cfg.wandb_project_name,
entity=cfg.wandb_entity,
config=vars(cfg),
name=run_name,
monitor_gym=True,
save_code=True,
mode="online" if cfg.track else "disabled",
)
if cfg.save_model:
evaluation_episode = 0
wandb.define_metric("evaluation_episode")
wandb.define_metric("eval/episodic_return", step_metric="evaluation_episode")
# To get deterministic pytorch to work
if cfg.torch_deterministic:
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":4096:8"
torch.use_deterministic_algorithms(True)
# TRY NOT TO MODIFY: seeding
random.seed(cfg.seed)
np.random.seed(cfg.seed)
torch.manual_seed(cfg.seed)
torch.set_float32_matmul_precision("high")
device = set_cuda_configuration(cfg.gpu)
# env setup
envs = gym.vector.SyncVectorEnv(
[make_env(cfg.env_id, cfg.seed + i, i, cfg.capture_video, run_name) for i in range(cfg.num_envs)]
)
assert isinstance(envs.single_action_space, gym.spaces.Discrete), "only discrete action space is supported"
q_network = QNetwork(envs).to(device)
if cfg.use_lecun_init:
# Use the same initialization scheme as jax/flax
q_network.apply(lecun_normal_initializer)
optimizer = optim.Adam(q_network.parameters(), lr=cfg.learning_rate, eps=cfg.adam_eps)
target_network = QNetwork(envs).to(device)
target_network.load_state_dict(q_network.state_dict())
rb = ReplayBuffer(
cfg.buffer_size,
envs.single_observation_space,
envs.single_action_space,
device,
optimize_memory_usage=True,
handle_timeout_termination=False,
)
start_time = time.time()
# TRY NOT TO MODIFY: start the game
obs, _ = envs.reset(seed=cfg.seed)
for global_step in range(cfg.total_timesteps):
# ALGO LOGIC: put action logic here
epsilon = linear_schedule(cfg.start_e, cfg.end_e, cfg.exploration_fraction * cfg.total_timesteps, global_step)
if random.random() < epsilon:
actions = np.array([envs.single_action_space.sample() for _ in range(envs.num_envs)])
else:
q_values = q_network(torch.Tensor(obs).to(device))
actions = torch.argmax(q_values, dim=1).cpu().numpy()
# TRY NOT TO MODIFY: execute the game and log data.
next_obs, rewards, terminated, truncated, infos = envs.step(actions)
# TRY NOT TO MODIFY: record rewards for plotting purposes
if "final_info" in infos:
for info in infos["final_info"]:
# Skip the envs that are not done
if "episode" not in info:
continue
epi_return = info["episode"]["r"].item()
print(f"global_step={global_step}, episodic_return={epi_return}")
wandb.log(
{
"charts/episodic_return": epi_return,
"charts/episodic_length": info["episode"]["l"].item(),
"charts/epsilon": epsilon,
},
step=global_step,
)
# TRY NOT TO MODIFY: save data to reply buffer; handle `final_observation`
real_next_obs = next_obs.copy()
for idx, d in enumerate(truncated):
if d:
real_next_obs[idx] = infos["final_observation"][idx]
rb.add(obs, real_next_obs, actions, rewards, terminated, infos)
# TRY NOT TO MODIFY: CRUCIAL step easy to overlook
obs = next_obs
# ALGO LOGIC: training.
if global_step > cfg.learning_starts:
# Flag for logging
done_update = False
if done_update := global_step % cfg.train_frequency == 0:
data = rb.sample(cfg.batch_size)
loss, old_val = dqn_loss(
q_network=q_network,
target_network=target_network,
obs=data.observations,
next_obs=data.next_observations,
actions=data.actions,
rewards=data.rewards,
dones=data.dones,
gamma=cfg.gamma,
)
# optimize the model
optimizer.zero_grad()
loss.backward()
optimizer.step()
logs = {
"losses/td_loss": loss,
"losses/q_values": old_val.mean().item(),
"charts/SPS": int(global_step / (time.time() - start_time)),
}
if global_step % cfg.redo_check_interval == 0:
redo_samples = rb.sample(cfg.redo_bs)
redo_out = run_redo(
redo_samples.observations,
model=q_network,
optimizer=optimizer,
tau=cfg.redo_tau,
re_initialize=cfg.enable_redo,
use_lecun_init=cfg.use_lecun_init,
)
q_network = redo_out["model"]
optimizer = redo_out["optimizer"]
logs |= {
f"regularization/dormant_t={cfg.redo_tau}_fraction": redo_out["dormant_fraction"],
f"regularization/dormant_t={cfg.redo_tau}_count": redo_out["dormant_count"],
"regularization/dormant_t=0.0_fraction": redo_out["zero_fraction"],
"regularization/dormant_t=0.0_count": redo_out["zero_count"],
}
if global_step % 100 == 0 and done_update:
print("SPS:", int(global_step / (time.time() - start_time)))
wandb.log(
logs,
step=global_step,
)
# update target network
if global_step % cfg.target_network_frequency == 0:
for target_network_param, q_network_param in zip(target_network.parameters(), q_network.parameters()):
target_network_param.data.copy_(
cfg.tau * q_network_param.data + (1.0 - cfg.tau) * target_network_param.data
)
if cfg.save_model:
model_path = Path(f"runs/{run_name}/{cfg.exp_name}")
model_path.mkdir(parents=True, exist_ok=True)
torch.save(q_network.state_dict(), model_path / ".cleanrl_model")
print(f"model saved to {model_path}")
from src.evaluate import evaluate
episodic_returns = evaluate(
model_path=model_path,
make_env=make_env,
env_id=cfg.env_id,
eval_episodes=10,
run_name=f"{run_name}-eval",
Model=QNetwork,
device=device,
epsilon=0.05,
capture_video=False,
)
for episodic_return in episodic_returns:
wandb.log({"evaluation_episode": evaluation_episode, "eval/episodic_return": episodic_return})
evaluation_episode += 1
envs.close()
wandb.finish()
if __name__ == "__main__":
cfg = tyro.cli(Config)
main(cfg)