-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdriver.py
312 lines (266 loc) · 16.6 KB
/
driver.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
import torch
import torch.optim as optim
import torch.nn as nn
from torch.utils.tensorboard import SummaryWriter
import ray
import os
import numpy as np
import random
from model import PolicyNet, QNet
from runner import RLRunner
from parameter import *
ray.init()
print("Welcome to Reactive Autonomous Navigation!")
writer = SummaryWriter(train_path)
if not os.path.exists(model_path):
os.makedirs(model_path)
if not os.path.exists(gifs_path):
os.makedirs(gifs_path)
def writeToTensorBoard(writer, tensorboardData, curr_episode):
tensorboardData = np.array(tensorboardData)
tensorboardData = list(np.nanmean(tensorboardData, axis=0))
reward, value, policyLoss, qValueLoss, entropy, policyGradNorm, qValueGradNorm, log_alpha, alphaLoss, travel_dist, success_rate, explored_rate = tensorboardData
writer.add_scalar(tag='Losses/Value', scalar_value=value, global_step=curr_episode)
writer.add_scalar(tag='Losses/Policy Loss', scalar_value=policyLoss, global_step=curr_episode)
writer.add_scalar(tag='Losses/Alpha Loss', scalar_value=alphaLoss, global_step=curr_episode)
writer.add_scalar(tag='Losses/Q Value Loss', scalar_value=qValueLoss, global_step=curr_episode)
writer.add_scalar(tag='Losses/Entropy', scalar_value=entropy, global_step=curr_episode)
writer.add_scalar(tag='Losses/Policy Grad Norm', scalar_value=policyGradNorm, global_step=curr_episode)
writer.add_scalar(tag='Losses/Q Value Grad Norm', scalar_value=qValueGradNorm, global_step=curr_episode)
writer.add_scalar(tag='Losses/Log Alpha', scalar_value=log_alpha, global_step=curr_episode)
writer.add_scalar(tag='Perf/Reward', scalar_value=reward, global_step=curr_episode)
writer.add_scalar(tag='Perf/Travel Distance', scalar_value=travel_dist, global_step=curr_episode)
writer.add_scalar(tag='Perf/Explored Rate', scalar_value=explored_rate, global_step=curr_episode)
writer.add_scalar(tag='Perf/Success Rate', scalar_value=success_rate, global_step=curr_episode)
def main():
device = torch.device('cuda') if USE_GPU_GLOBAL else torch.device('cpu')
local_device = torch.device('cuda') if USE_GPU else torch.device('cpu')
global_policy_net = PolicyNet(INPUT_DIM, EMBEDDING_DIM).to(device)
global_q_net1 = QNet(INPUT_DIM, EMBEDDING_DIM).to(device)
global_q_net2 = QNet(INPUT_DIM, EMBEDDING_DIM).to(device)
log_alpha = torch.FloatTensor([-2]).to(device) # not trainable when loaded from checkpoint, manually tune it for now
log_alpha.requires_grad = True
global_target_q_net1 = QNet(INPUT_DIM, EMBEDDING_DIM).to(device)
global_target_q_net2 = QNet(INPUT_DIM, EMBEDDING_DIM).to(device)
# initialize optimizers
global_policy_optimizer = optim.Adam(global_policy_net.parameters(), lr=LR)
global_q_net1_optimizer = optim.Adam(global_q_net1.parameters(), lr=2e-5)
global_q_net2_optimizer = optim.Adam(global_q_net2.parameters(), lr=2e-5)
log_alpha_optimizer = optim.Adam([log_alpha], lr=1e-4)
# initialize decay (not use)
policy_lr_decay = optim.lr_scheduler.StepLR(global_policy_optimizer, step_size=DECAY_STEP, gamma=0.96)
q_net1_lr_decay = optim.lr_scheduler.StepLR(global_q_net1_optimizer,step_size=DECAY_STEP, gamma=0.96)
q_net2_lr_decay = optim.lr_scheduler.StepLR(global_q_net2_optimizer,step_size=DECAY_STEP, gamma=0.96)
log_alpha_lr_decay = optim.lr_scheduler.StepLR(log_alpha_optimizer, step_size=DECAY_STEP, gamma=0.96)
entropy_target = 0.01 * (-np.log(1 / K_SIZE))
curr_episode = 0
target_q_update_counter = 1
if LOAD_MODEL:
print('Loading Model...')
checkpoint = torch.load(model_path + '/checkpoint.pth')
global_policy_net.load_state_dict(checkpoint['policy_model'])
global_q_net1.load_state_dict(checkpoint['q_net1_model'])
global_q_net2.load_state_dict(checkpoint['q_net2_model'])
# log_alpha = checkpoint['log_alpha'] # not trainable when loaded from checkpoint, manually tune it for now
global_policy_optimizer.load_state_dict(checkpoint['policy_optimizer'])
global_q_net1_optimizer.load_state_dict(checkpoint['q_net1_optimizer'])
global_q_net2_optimizer.load_state_dict(checkpoint['q_net2_optimizer'])
log_alpha_optimizer.load_state_dict(checkpoint['log_alpha_optimizer'])
policy_lr_decay.load_state_dict(checkpoint['policy_lr_decay'])
q_net1_lr_decay.load_state_dict(checkpoint['q_net1_lr_decay'])
q_net2_lr_decay.load_state_dict(checkpoint['q_net2_lr_decay'])
log_alpha_lr_decay.load_state_dict(checkpoint['log_alpha_lr_decay'])
curr_episode = checkpoint['episode']
print("curr_episode set to ", curr_episode)
print(log_alpha)
print(global_policy_optimizer.state_dict()['param_groups'][0]['lr'])
global_target_q_net1.load_state_dict(global_q_net1.state_dict())
global_target_q_net2.load_state_dict(global_q_net2.state_dict())
global_target_q_net1.eval()
global_target_q_net2.eval()
# launch meta agents
meta_agents = [RLRunner.remote(i) for i in range(NUM_META_AGENT)]
# get global networks weights
weights_set = []
if device != local_device:
policy_weights = global_policy_net.to(local_device).state_dict()
q_net1_weights = global_q_net1.to(local_device).state_dict()
global_policy_net.to(device)
global_q_net1.to(device)
else:
policy_weights = global_policy_net.to(local_device).state_dict()
q_net1_weights = global_q_net1.to(local_device).state_dict()
weights_set.append(policy_weights)
weights_set.append(q_net1_weights)
# distributed training if multiple GPUs available
dp_policy = nn.DataParallel(global_policy_net)
dp_q_net1 = nn.DataParallel(global_q_net1)
dp_q_net2 = nn.DataParallel(global_q_net2)
dp_target_q_net1 = nn.DataParallel(global_target_q_net1)
dp_target_q_net2 = nn.DataParallel(global_target_q_net2)
# launch the first job on each runner
job_list = []
for i, meta_agent in enumerate(meta_agents):
curr_episode += 1
job_list.append(meta_agent.job.remote(weights_set, curr_episode))
# initialize metric collector
metric_name = ['travel_dist', 'success_rate', 'explored_rate']
training_data = []
perf_metrics = {}
for n in metric_name:
perf_metrics[n] = []
# initialize training replay buffer
experience_buffer = []
for i in range(15):
experience_buffer.append([])
# collect data from worker and do training
try:
while True:
# wait for any job to be completed
done_id, job_list = ray.wait(job_list)
# get the results
done_jobs = ray.get(done_id)
# save experience and metric
for job in done_jobs:
job_results, metrics, info = job
for i in range(len(experience_buffer)):
experience_buffer[i] += job_results[i]
for n in metric_name:
perf_metrics[n].append(metrics[n])
# launch new task
curr_episode += 1
job_list.append(meta_agents[info['id']].job.remote(weights_set, curr_episode))
# start training
if curr_episode % 1 == 0 and len(experience_buffer[0]) >= MINIMUM_BUFFER_SIZE:
print("training")
# keep the replay buffer size
if len(experience_buffer[0]) >= REPLAY_SIZE:
for i in range(len(experience_buffer)):
experience_buffer[i] = experience_buffer[i][-REPLAY_SIZE:]
indices = range(len(experience_buffer[0]))
# training for n times each step
for j in range(8):
# randomly sample a batch data
sample_indices = random.sample(indices, BATCH_SIZE)
rollouts = []
for i in range(len(experience_buffer)):
rollouts.append([experience_buffer[i][index] for index in sample_indices])
# stack batch data to tensors
node_inputs_batch = torch.stack(rollouts[0]).to(device)
edge_inputs_batch = torch.stack(rollouts[1]).to(device)
current_inputs_batch = torch.stack(rollouts[2]).to(device)
node_padding_mask_batch = torch.stack(rollouts[3]).to(device)
edge_padding_mask_batch = torch.stack(rollouts[4]).to(device)
edge_mask_batch = torch.stack(rollouts[5]).to(device)
action_batch = torch.stack(rollouts[6]).to(device)
reward_batch = torch.stack(rollouts[7]).to(device)
done_batch = torch.stack(rollouts[8]).to(device)
next_node_inputs_batch = torch.stack(rollouts[9]).to(device)
next_edge_inputs_batch = torch.stack(rollouts[10]).to(device)
next_current_inputs_batch = torch.stack(rollouts[11]).to(device)
next_node_padding_mask_batch = torch.stack(rollouts[12]).to(device)
next_edge_padding_mask_batch = torch.stack(rollouts[13]).to(device)
next_edge_mask_batch = torch.stack(rollouts[14]).to(device)
# SAC
with torch.no_grad():
q_values1, _ = dp_q_net1(node_inputs_batch, edge_inputs_batch, current_inputs_batch, node_padding_mask_batch, edge_padding_mask_batch, edge_mask_batch)
q_values2, _ = dp_q_net2(node_inputs_batch, edge_inputs_batch, current_inputs_batch, node_padding_mask_batch, edge_padding_mask_batch, edge_mask_batch)
q_values = torch.min(q_values1, q_values2)
logp = dp_policy(node_inputs_batch, edge_inputs_batch, current_inputs_batch, node_padding_mask_batch, edge_padding_mask_batch, edge_mask_batch)
policy_loss = torch.sum((logp.exp().unsqueeze(2) * (log_alpha.exp().detach() * logp.unsqueeze(2) - q_values.detach())), dim=1).mean()
with torch.no_grad():
next_logp = dp_policy(next_node_inputs_batch, next_edge_inputs_batch, next_current_inputs_batch, next_node_padding_mask_batch, next_edge_padding_mask_batch, next_edge_mask_batch)
next_q_values1, _ = dp_target_q_net1(next_node_inputs_batch, next_edge_inputs_batch, next_current_inputs_batch, next_node_padding_mask_batch, next_edge_padding_mask_batch, next_edge_mask_batch)
next_q_values2, _ = dp_target_q_net2(next_node_inputs_batch, next_edge_inputs_batch, next_current_inputs_batch, next_node_padding_mask_batch, next_edge_padding_mask_batch, next_edge_mask_batch)
next_q_values = torch.min(next_q_values1, next_q_values2)
value_prime_batch = torch.sum(next_logp.unsqueeze(2).exp() * (next_q_values - log_alpha.exp() * next_logp.unsqueeze(2)), dim=1).unsqueeze(1)
target_q_batch = reward_batch + GAMMA * (1 - done_batch) * value_prime_batch
q_values1, _ = dp_q_net1(node_inputs_batch, edge_inputs_batch, current_inputs_batch, node_padding_mask_batch, edge_padding_mask_batch, edge_mask_batch)
q_values2, _ = dp_q_net2(node_inputs_batch, edge_inputs_batch, current_inputs_batch, node_padding_mask_batch, edge_padding_mask_batch, edge_mask_batch)
q1 = torch.gather(q_values1, 1, action_batch)
q2 = torch.gather(q_values2, 1, action_batch)
mse_loss = nn.MSELoss()
q1_loss = mse_loss(q1, target_q_batch.detach()).mean()
q2_loss = mse_loss(q2, target_q_batch.detach()).mean()
global_policy_optimizer.zero_grad()
policy_loss.backward()
policy_grad_norm = torch.nn.utils.clip_grad_norm_(global_policy_net.parameters(), max_norm=100, norm_type=2)
global_policy_optimizer.step()
global_q_net1_optimizer.zero_grad()
q1_loss.backward()
q_grad_norm = torch.nn.utils.clip_grad_norm_(global_q_net1.parameters(), max_norm=20000, norm_type=2)
global_q_net1_optimizer.step()
global_q_net2_optimizer.zero_grad()
q2_loss.backward()
q_grad_norm = torch.nn.utils.clip_grad_norm_(global_q_net2.parameters(), max_norm=20000, norm_type=2)
global_q_net2_optimizer.step()
entropy = (logp * logp.exp()).sum(dim=-1)
alpha_loss = -(log_alpha * (entropy.detach() + entropy_target)).mean()
log_alpha_optimizer.zero_grad()
alpha_loss.backward()
log_alpha_optimizer.step()
target_q_update_counter += 1
#print("target q update counter", target_q_update_counter % 1024)
policy_lr_decay.step()
q_net1_lr_decay.step()
q_net2_lr_decay.step()
# data record to be written in tensorboard
perf_data = []
for n in metric_name:
perf_data.append(np.nanmean(perf_metrics[n]))
data = [reward_batch.mean().item(), value_prime_batch.mean().item(), policy_loss.item(), q1_loss.item(),
entropy.mean().item(), policy_grad_norm.item(), q_grad_norm.item(), log_alpha.item(), alpha_loss.item(), *perf_data]
training_data.append(data)
# write record to tensorboard
if len(training_data) >= SUMMARY_WINDOW:
writeToTensorBoard(writer, training_data, curr_episode)
training_data = []
perf_metrics = {}
for n in metric_name:
perf_metrics[n] = []
# get the updated global weights
weights_set = []
if device != local_device:
policy_weights = global_policy_net.to(local_device).state_dict()
q_net1_weights = global_q_net1.to(local_device).state_dict()
global_policy_net.to(device)
global_q_net1.to(device)
else:
policy_weights = global_policy_net.to(local_device).state_dict()
q_net1_weights = global_q_net1.to(local_device).state_dict()
weights_set.append(policy_weights)
weights_set.append(q_net1_weights)
# update the target q net
if target_q_update_counter > 64:
print("update target q net")
target_q_update_counter = 1
global_target_q_net1.load_state_dict(global_q_net1.state_dict())
global_target_q_net2.load_state_dict(global_q_net2.state_dict())
global_target_q_net1.eval()
global_target_q_net2.eval()
# save the model
if curr_episode % 32 == 0:
print('Saving model', end='\n')
checkpoint = {"policy_model": global_policy_net.state_dict(),
"q_net1_model": global_q_net1.state_dict(),
"q_net2_model": global_q_net2.state_dict(),
"log_alpha": log_alpha,
"policy_optimizer": global_policy_optimizer.state_dict(),
"q_net1_optimizer": global_q_net1_optimizer.state_dict(),
"q_net2_optimizer": global_q_net2_optimizer.state_dict(),
"log_alpha_optimizer": log_alpha_optimizer.state_dict(),
"episode": curr_episode,
"policy_lr_decay": policy_lr_decay.state_dict(),
"q_net1_lr_decay": q_net1_lr_decay.state_dict(),
"q_net2_lr_decay": q_net2_lr_decay.state_dict(),
"log_alpha_lr_decay": log_alpha_lr_decay.state_dict()
}
path_checkpoint = "./" + model_path + "/checkpoint.pth"
torch.save(checkpoint, path_checkpoint)
print('Saved model', end='\n')
except KeyboardInterrupt:
print("CTRL_C pressed. Killing remote workers")
for a in meta_agents:
ray.kill(a)
if __name__ == "__main__":
main()