-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
186 lines (156 loc) · 5.92 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
import os
import time
import gymnasium as gym
import hydra
import numpy as np
import torch
from tqdm import trange
from actor import RandomActor, TD3_Actor
from critic import Critic
from logger import TensorboardSummaries as Summaries
from replay_buffer import ReplayBuffer
from utils import (
compute_actor_loss,
compute_critic_target,
optimize,
play_and_record,
update_target_networks,
)
class Trainer:
def __init__(self, cfg):
self.cfg = cfg
def train(self):
env_name = self.cfg.env_name
env = gym.make(env_name, render_mode="rgb_array")
env = Summaries(env, env_name)
state_dim = env.observation_space.shape[0]
action_dim = env.action_space.shape[0]
action_lim = (env.action_space.low[0], env.action_space.high[0])
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
exp_replay = ReplayBuffer(self.cfg.max_buffer_size)
# models to train
actor = TD3_Actor(
state_dim, action_dim, self.cfg.hidden_size, action_lim, DEVICE
).to(DEVICE)
critic1 = Critic(state_dim, action_dim, self.cfg.hidden_size).to(DEVICE)
critic2 = Critic(state_dim, action_dim, self.cfg.hidden_size).to(DEVICE)
# target networks: slow-updated copies of actor and two critics
target_critic1 = Critic(state_dim, action_dim, self.cfg.hidden_size).to(DEVICE)
target_critic2 = Critic(state_dim, action_dim, self.cfg.hidden_size).to(DEVICE)
target_actor = TD3_Actor(
state_dim, action_dim, self.cfg.hidden_size, action_lim, DEVICE
).to(DEVICE)
# initialize them as copies of original models
target_critic1.load_state_dict(critic1.state_dict())
target_critic2.load_state_dict(critic2.state_dict())
target_actor.load_state_dict(actor.state_dict())
# optimizers: for every model
opt_actor = torch.optim.Adam(actor.parameters(), lr=self.cfg.lr)
opt_critic1 = torch.optim.Adam(critic1.parameters(), lr=self.cfg.lr)
opt_critic2 = torch.optim.Adam(critic2.parameters(), lr=self.cfg.lr)
np.random.seed(self.cfg.seed)
torch.manual_seed(self.cfg.seed)
interaction_state, _ = env.reset(seed=self.cfg.seed)
random_actor = RandomActor(env)
for n_iterations in trange(
0, self.cfg.iterations, self.cfg.timesteps_per_epoch
):
# collect data using random policy to collect more diverse starting data
if len(exp_replay) < self.cfg.start_timesteps:
_, interaction_state = play_and_record(
interaction_state,
random_actor,
env,
exp_replay,
self.cfg.timesteps_per_epoch,
)
continue
_, interaction_state = play_and_record(
interaction_state,
actor,
env,
exp_replay,
self.cfg.timesteps_per_epoch,
)
states, actions, rewards, next_states, is_done = exp_replay.sample(
self.cfg.batch_size
)
states = torch.tensor(states, device=DEVICE, dtype=torch.float)
actions = torch.tensor(actions, device=DEVICE, dtype=torch.float)
rewards = torch.tensor(rewards, device=DEVICE, dtype=torch.float)
next_states = torch.tensor(next_states, device=DEVICE, dtype=torch.float)
is_done = torch.tensor(
is_done.astype("float32"), device=DEVICE, dtype=torch.float
)
critic1_loss = (
critic1.get_qvalues(states, actions)
- compute_critic_target(
target_actor,
target_critic1,
target_critic2,
rewards,
next_states,
self.cfg.gamma,
is_done,
)
) ** 2
optimize(
env,
"critic1",
critic1,
opt_critic1,
critic1_loss,
self.cfg.max_grad_norm,
n_iterations,
)
critic2_loss = (
critic2.get_qvalues(states, actions)
- compute_critic_target(
target_actor,
target_critic1,
target_critic2,
rewards,
next_states,
self.cfg.gamma,
is_done,
)
) ** 2
optimize(
env,
"critic2",
critic2,
opt_critic2,
critic2_loss,
self.cfg.max_grad_norm,
n_iterations,
)
if n_iterations % self.cfg.policy_update_freq == 0:
actor_loss = compute_actor_loss(actor, target_critic1, states)
optimize(
env,
"actor",
actor,
opt_actor,
actor_loss,
self.cfg.max_grad_norm,
n_iterations,
)
update_target_networks(critic1, target_critic1, self.cfg.tau)
update_target_networks(critic2, target_critic2, self.cfg.tau)
update_target_networks(actor, target_actor, self.cfg.tau)
if not os.path.exists(self.cfg.model_save_path):
os.makedirs(self.cfg.model_save_path)
path = (
self.cfg.model_save_path
+ "/actor_"
+ str(self.cfg.iterations)
+ "_"
+ str(time.time())
)
torch.save(actor, path)
@hydra.main(config_path="configs", config_name="cheetah_config", version_base="1.3.2")
def main(cfg):
trainer = Trainer(cfg)
trainer.train()
if __name__ == "__main__":
main()