-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathes_train.py
More file actions
205 lines (159 loc) · 6.41 KB
/
es_train.py
File metadata and controls
205 lines (159 loc) · 6.41 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
"""
"""
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import copy
import time
import glob
import cma
import os
from mpi4py import MPI
from torch.distributions import Categorical
from model import RNNModel, Controller, VAE
from common import Logger
from config import cfg
def load_init_z():
data = np.load('init_z.npz')
mus = data['mus']
logvars = data['logvars']
return mus, logvars
def sample_init_z(mus, logvars):
idx = np.random.randint(0, mus.shape[0])
mu, logvar = mus[idx], logvars[idx]
# mu /= 2.0
# logvar /= 2.0
z = mu + np.exp(logvar / 2.0) * np.random.randn(*mu.shape)
return z
def encode_action(y):
if y > 1 / 3.0:
action = torch.LongTensor([1])
elif y < -1 / 3.0:
action = torch.LongTensor([0])
else:
action = torch.LongTensor([2])
return action
def flatten_controller(controller):
param_array = []
for param in controller.parameters():
param_array.append(param.data.view(-1))
param_array = torch.cat(param_array, dim=0)
return param_array.numpy()
def deflatten_controller(param_array):
controller = Controller()
for param in controller.parameters():
size = param.data.view(-1).size(0)
param.data = torch.FloatTensor(param_array[:size]).view_as(param.data)
param_array = param_array[size:]
return controller
def rollout(model, controller, zs):
rewards = []
for epi in range(cfg.trials_per_pop):
model.reset()
z = zs[epi]
for step in range(cfg.max_steps):
z = torch.from_numpy(z).float().unsqueeze(0)
x = torch.cat((model.hx.detach(), model.cx.detach(), z), dim=1)
y = controller(x)
# m = Categorical(F.softmax(y, dim=1))
# action = m.sample()
y = y.item()
action = encode_action(y)
logmix, mu, logstd, done_p = model.step(z.unsqueeze(0), action.unsqueeze(0))
# logmix = logmix - reduce_logsumexp(logmix)
logmix_max = logmix.max(dim=1, keepdim=True)[0]
logmix_reduce_logsumexp = (logmix - logmix_max).exp().sum(dim=1, keepdim=True).log() + logmix_max
logmix = logmix - logmix_reduce_logsumexp
# Adjust temperature
logmix = logmix / cfg.temperature
logmix -= logmix.max(dim=1, keepdim=True)[0]
logmix = F.softmax(logmix, dim=1)
m = Categorical(logmix)
idx = m.sample()
new_mu = torch.FloatTensor([mu[i, j] for i, j in enumerate(idx)])
new_logstd = torch.FloatTensor([logstd[i, j] for i, j in enumerate(idx)])
z_next = new_mu + new_logstd.exp() * torch.randn_like(new_mu) * np.sqrt(cfg.temperature)
z = z_next.detach().numpy()
if done_p.squeeze().item() > 0:
break
rewards.append(step)
return np.array(rewards)
def l2_reg(x):
x = np.array(x)
return cfg.es_w_reg * np.mean(x * x, axis=1)
def slave(comm):
mus, logvars = load_init_z()
vae = VAE()
vae.load_state_dict(torch.load(cfg.vae_save_ckpt, map_location=lambda storage, loc: storage)['model'])
model = RNNModel()
model.load_state_dict(torch.load(cfg.rnn_save_ckpt, map_location=lambda storage, loc: storage)['model'])
count = 1
status = MPI.Status()
gpuid = comm.rank % 4
# device = torch.device('cuda:{}'.format(gpuid))
# vae.to(device)
# model.to(device)
print('Worker {} Started, model on GPU {}'.format(comm.rank, gpuid))
while True:
solution = comm.recv(source=0, tag=MPI.ANY_TAG, status=status)
tag = status.Get_tag()
if tag == 1:
print('Worker {} received solution {}'.format(comm.rank, count))
zs = [sample_init_z(mus, logvars) for _ in range(cfg.trials_per_pop)]
controller = deflatten_controller(solution)
reward = rollout(model, controller, zs)
print('Worker {} finished solution {}, reward: mean {} | max {} | min {} | std {}'.format(
comm.rank, count, reward.mean(), reward.max(), reward.min(), reward.std()))
comm.send(reward.mean(), dest=0, tag=2)
count += 1
elif tag == 3:
print('Worker {} evaluate current solution'.format(comm.rank))
controller = deflatten_controller(solution)
reward = evaluate(model, vae, controller)
comm.send(reward, dest=0, tag=2)
def master(comm):
logger = Logger("{}/es_train_{}.log".format(cfg.logger_save_dir, cfg.timestr))
logger.log(cfg.info)
controller = Controller()
es = cma.CMAEvolutionStrategy(flatten_controller(controller), cfg.es_sigma, {'popsize': cfg.population_size})
for step in range(cfg.es_steps):
solutions = es.ask()
for idx, solution in enumerate(solutions):
comm.send(solution, dest=idx+1, tag=1)
check = np.ones(cfg.num_workers)
rewards = []
for idx in range(cfg.num_workers):
reward = comm.recv(source=idx+1, tag=2)
rewards.append(reward)
check[idx] = 0
assert check.sum() == 0
assert len(rewards) == cfg.num_workers
r_cost = - np.array(rewards)
reg_cost = l2_reg(solutions)
cost = reg_cost + r_cost
es.tell(solutions, cost.tolist())
sigma = es.result[6]
rms_var = np.mean(sigma * sigma)
info = "Step {:d}\t Max_R {:4f}\t Mean_R {:4f}\t Min_R {:4f}\t RMS_Var {:4f}\t Reg_Cost {:4f}\t R_Cost {:4f}".format(
step, max(rewards), np.mean(rewards), min(rewards), rms_var, r_cost.mean(), reg_cost.mean())
logger.log(info)
if step % 25 == 0:
current_param = es.result[5]
current_controller = deflatten_controller(current_param)
save_path = "{}/controller_curr_{}_step_{:05d}.pth".format(cfg.model_save_dir, cfg.timestr, step)
torch.save({'model': current_controller.state_dict()}, save_path)
best_param = es.result[0]
best_controller = deflatten_controller(best_param)
save_path = "{}/controller_best_{}_step_{:05d}.pth".format(cfg.model_save_dir, cfg.timestr, step)
torch.save({'model': best_controller.state_dict()}, save_path)
def es_train():
comm = MPI.COMM_WORLD
rank = comm.rank
size = comm.size
if rank == 0:
master(comm)
else:
slave(comm)
if __name__ == '__main__':
es_train()