forked from werner-duvaud/muzero-general
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
300 lines (268 loc) · 11.3 KB
/
trainer.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
import copy
import time
import numpy
import ray
import torch
import models
@ray.remote
class Trainer:
"""
Class which run in a dedicated thread to train a neural network and save it
in the shared storage.
"""
def __init__(self, initial_checkpoint, config):
self.config = config
# Fix random generator seed
numpy.random.seed(self.config.seed)
torch.manual_seed(self.config.seed)
# Initialize the network
self.model = models.MuZeroNetwork(self.config)
self.model.set_weights(copy.deepcopy(initial_checkpoint["weights"]))
self.model.to(torch.device("cuda" if self.config.train_on_gpu else "cpu"))
self.model.train()
self.training_step = initial_checkpoint["training_step"]
if "cuda" not in str(next(self.model.parameters()).device):
print("You are not training on GPU.\n")
# Initialize the optimizer
if self.config.optimizer == "SGD":
self.optimizer = torch.optim.SGD(
self.model.parameters(),
lr=self.config.lr_init,
momentum=self.config.momentum,
weight_decay=self.config.weight_decay,
)
elif self.config.optimizer == "Adam":
self.optimizer = torch.optim.Adam(
self.model.parameters(),
lr=self.config.lr_init,
weight_decay=self.config.weight_decay,
)
else:
raise NotImplementedError(
f"{self.config.optimizer} is not implemented. You can change the optimizer manually in trainer.py."
)
if initial_checkpoint["optimizer_state"] is not None:
print("Loading optimizer...\n")
self.optimizer.load_state_dict(
copy.deepcopy(initial_checkpoint["optimizer_state"])
)
def continuous_update_weights(self, replay_buffer, shared_storage):
# Wait for the replay buffer to be filled
while ray.get(shared_storage.get_info.remote("num_played_games")) < 1:
time.sleep(0.1)
next_batch = replay_buffer.get_batch.remote()
# Training loop
while self.training_step < self.config.training_steps and not ray.get(
shared_storage.get_info.remote("terminate") # terminate是用来记录replay buffer等其它程序是否终止的,跟game的状态无关
):
index_batch, batch = ray.get(next_batch)
next_batch = replay_buffer.get_batch.remote()
self.update_lr()
(
priorities,
total_loss,
value_loss,
reward_loss,
policy_loss,
) = self.update_weights(batch)
if self.config.PER:
# Save new priorities in the replay buffer (See https://arxiv.org/abs/1803.00933)
replay_buffer.update_priorities.remote(priorities, index_batch)
# Save to the shared storage
if self.training_step % self.config.checkpoint_interval == 0:
shared_storage.set_info.remote(
{
"weights": copy.deepcopy(self.model.get_weights()),
"optimizer_state": copy.deepcopy(
models.dict_to_cpu(self.optimizer.state_dict())
),
}
)
if self.config.save_model:
shared_storage.save_checkpoint.remote()
shared_storage.set_info.remote(
{
"training_step": self.training_step,
"lr": self.optimizer.param_groups[0]["lr"],
"total_loss": total_loss,
"value_loss": value_loss,
"reward_loss": reward_loss,
"policy_loss": policy_loss,
}
)
# Managing the self-play / training ratio
if self.config.training_delay:
time.sleep(self.config.training_delay)
if self.config.ratio:
while (
self.training_step
/ max(
1, ray.get(shared_storage.get_info.remote("num_played_steps"))
)
> self.config.ratio
and self.training_step < self.config.training_steps
and not ray.get(shared_storage.get_info.remote("terminate")) # terminate是用来记录replay buffer等其它程序是否终止的,跟game的状态无关
):
time.sleep(0.5)
def update_weights(self, batch):
"""
Perform one training step.
"""
(
observation_batch,
action_batch,
target_value,
target_reward,
target_policy,
weight_batch,
gradient_scale_batch,
) = batch
# Keep values as scalars for calculating the priorities for the prioritized replay
target_value_scalar = numpy.array(target_value, dtype="float32")
priorities = numpy.zeros_like(target_value_scalar)
device = next(self.model.parameters()).device
if self.config.PER:
weight_batch = torch.tensor(weight_batch.copy()).float().to(device)
observation_batch = (
torch.tensor(numpy.array(observation_batch)).float().to(device)
)
action_batch = torch.tensor(action_batch).long().to(device).unsqueeze(-1)
target_value = torch.tensor(target_value).float().to(device)
target_reward = torch.tensor(target_reward).float().to(device)
target_policy = torch.tensor(target_policy).float().to(device)
gradient_scale_batch = torch.tensor(gradient_scale_batch).float().to(device)
# observation_batch: batch, channels, height, width
# action_batch: batch, num_unroll_steps+1, 1 (unsqueeze)
# target_value: batch, num_unroll_steps+1
# target_reward: batch, num_unroll_steps+1
# target_policy: batch, num_unroll_steps+1, len(action_space)
# gradient_scale_batch: batch, num_unroll_steps+1
target_value = models.scalar_to_support(target_value, self.config.support_size)
target_reward = models.scalar_to_support(
target_reward, self.config.support_size
)
# target_value: batch, num_unroll_steps+1, 2*support_size+1
# target_reward: batch, num_unroll_steps+1, 2*support_size+1
## Generate predictions
value, reward, policy_logits, hidden_state = self.model.initial_inference(
observation_batch
)
predictions = [(value, reward, policy_logits)]
for i in range(1, action_batch.shape[1]):
value, reward, policy_logits, hidden_state = self.model.recurrent_inference(
hidden_state, action_batch[:, i]
)
# Scale the gradient at the start of the dynamics function (See paper appendix Training)
hidden_state.register_hook(lambda grad: grad * 0.5)
predictions.append((value, reward, policy_logits))
# predictions: num_unroll_steps+1, 3, batch, 2*support_size+1 | 2*support_size+1 | 9 (according to the 2nd dim)
## Compute losses
value_loss, reward_loss, policy_loss = (0, 0, 0)
value, reward, policy_logits = predictions[0]
# Ignore reward loss for the first batch step
current_value_loss, _, current_policy_loss = self.loss_function(
value.squeeze(-1),
reward.squeeze(-1),
policy_logits,
target_value[:, 0],
target_reward[:, 0],
target_policy[:, 0],
)
value_loss += current_value_loss
policy_loss += current_policy_loss
# Compute priorities for the prioritized replay (See paper appendix Training)
pred_value_scalar = (
models.support_to_scalar(value, self.config.support_size)
.detach()
.cpu()
.numpy()
.squeeze()
)
priorities[:, 0] = (
numpy.abs(pred_value_scalar - target_value_scalar[:, 0])
** self.config.PER_alpha
)
for i in range(1, len(predictions)):
value, reward, policy_logits = predictions[i]
(
current_value_loss,
current_reward_loss,
current_policy_loss,
) = self.loss_function(
value.squeeze(-1),
reward.squeeze(-1),
policy_logits,
target_value[:, i],
target_reward[:, i],
target_policy[:, i],
)
# Scale gradient by the number of unroll steps (See paper appendix Training)
current_value_loss.register_hook(
lambda grad: grad / gradient_scale_batch[:, i]
)
current_reward_loss.register_hook(
lambda grad: grad / gradient_scale_batch[:, i]
)
current_policy_loss.register_hook(
lambda grad: grad / gradient_scale_batch[:, i]
)
value_loss += current_value_loss
reward_loss += current_reward_loss
policy_loss += current_policy_loss
# Compute priorities for the prioritized replay (See paper appendix Training)
pred_value_scalar = (
models.support_to_scalar(value, self.config.support_size)
.detach()
.cpu()
.numpy()
.squeeze()
)
priorities[:, i] = (
numpy.abs(pred_value_scalar - target_value_scalar[:, i])
** self.config.PER_alpha
)
# Scale the value loss, paper recommends by 0.25 (See paper appendix Reanalyze)
loss = value_loss * self.config.value_loss_weight + reward_loss + policy_loss
if self.config.PER:
# Correct PER bias by using importance-sampling (IS) weights
loss *= weight_batch
# Mean over batch dimension (pseudocode do a sum)
loss = loss.mean()
# Optimize
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.training_step += 1
return (
priorities,
# For log purpose
loss.item(),
value_loss.mean().item(),
reward_loss.mean().item(),
policy_loss.mean().item(),
)
def update_lr(self):
"""
Update learning rate
"""
lr = self.config.lr_init * self.config.lr_decay_rate ** (
self.training_step / self.config.lr_decay_steps
)
for param_group in self.optimizer.param_groups: # 更新optimizer的lr
param_group["lr"] = lr
@staticmethod
def loss_function(
value,
reward,
policy_logits,
target_value,
target_reward,
target_policy,
):
# Cross-entropy seems to have a better convergence than MSE
value_loss = (-target_value * torch.nn.LogSoftmax(dim=1)(value)).sum(1)
reward_loss = (-target_reward * torch.nn.LogSoftmax(dim=1)(reward)).sum(1)
policy_loss = (-target_policy * torch.nn.LogSoftmax(dim=1)(policy_logits)).sum(
1
)
return value_loss, reward_loss, policy_loss