-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtrain.py
308 lines (274 loc) · 11.8 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
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
from __future__ import absolute_import, division, print_function
import os
import math
import numpy as np
import torch
import torch.legacy.optim as legacyOptim
import torch.nn.functional as F
import torch.multiprocessing as mp
from torch.autograd import Variable
from envs import create_atari_env
from model import ES
import matplotlib.pyplot as plt
def do_rollouts(args, models, random_seeds, return_queue, env, are_negative):
"""
For each model, do a rollout. Supports multiple models per thread but
don't do it -- it's inefficient (it's mostly a relic of when I would run
both a perturbation and its antithesis on the same thread).
"""
all_returns = []
all_num_frames = []
for model in models:
if not args.small_net:
cx = Variable(torch.zeros(1, 256))
hx = Variable(torch.zeros(1, 256))
state = env.reset()
state = torch.from_numpy(state)
this_model_return = 0
this_model_num_frames = 0
# Rollout
for step in range(args.max_episode_length):
if args.small_net:
state = state.float()
state = state.view(1, env.observation_space.shape[0])
logit = model(Variable(state, volatile=True))
else:
logit, (hx, cx) = model(
(Variable(state.unsqueeze(0), volatile=True),
(hx, cx)))
prob = F.softmax(logit)
action = prob.max(1)[1].data.numpy()
state, reward, done, _ = env.step(action[0])
this_model_return += reward
this_model_num_frames += 1
if done:
break
state = torch.from_numpy(state)
all_returns.append(this_model_return)
all_num_frames.append(this_model_num_frames)
return_queue.put((random_seeds, all_returns, all_num_frames, are_negative))
def perturb_model(args, model, random_seed, env):
"""
Modifies the given model with a pertubation of its parameters,
as well as the negative perturbation, and returns both perturbed
models.
"""
new_model = ES(env.observation_space.shape[0],
env.action_space, args.small_net)
anti_model = ES(env.observation_space.shape[0],
env.action_space, args.small_net)
new_model.load_state_dict(model.state_dict())
anti_model.load_state_dict(model.state_dict())
np.random.seed(random_seed)
for (k, v), (anti_k, anti_v) in zip(new_model.es_params(),
anti_model.es_params()):
eps = np.random.normal(0, 1, v.size())
v += torch.from_numpy(args.sigma*eps).float()
anti_v += torch.from_numpy(args.sigma*-eps).float()
return [new_model, anti_model]
optimConfig = []
averageReward = []
maxReward = []
minReward = []
episodeCounter = []
def gradient_update(args, synced_model, returns, random_seeds, neg_list,
num_eps, num_frames, chkpt_dir, unperturbed_results):
def fitness_shaping(returns):
"""
A rank transformation on the rewards, which reduces the chances
of falling into local optima early in training.
"""
sorted_returns_backwards = sorted(returns)[::-1]
lamb = len(returns)
shaped_returns = []
denom = sum([max(0, math.log(lamb/2 + 1, 2) -
math.log(sorted_returns_backwards.index(r) + 1, 2))
for r in returns])
for r in returns:
num = max(0, math.log(lamb/2 + 1, 2) -
math.log(sorted_returns_backwards.index(r) + 1, 2))
shaped_returns.append(num/denom + 1/lamb)
return shaped_returns
def unperturbed_rank(returns, unperturbed_results):
nth_place = 1
for r in returns:
if r > unperturbed_results:
nth_place += 1
rank_diag = ('%d out of %d (1 means gradient '
'is uninformative)' % (nth_place,
len(returns) + 1))
return rank_diag, nth_place
batch_size = len(returns)
assert batch_size == args.n
assert len(random_seeds) == batch_size
shaped_returns = fitness_shaping(returns)
rank_diag, rank = unperturbed_rank(returns, unperturbed_results)
if not args.silent:
print('Episode num: %d\n'
'Average reward: %f\n'
'Variance in rewards: %f\n'
'Max reward: %f\n'
'Min reward: %f\n'
'Batch size: %d\n'
'Max episode length: %d\n'
'Sigma: %f\n'
'Learning rate: %f\n'
'Total num frames seen: %d\n'
'Unperturbed reward: %f\n'
'Unperturbed rank: %s\n'
'Using Adam: %r\n\n' %
(num_eps, np.mean(returns), np.var(returns), max(returns),
min(returns), batch_size,
args.max_episode_length, args.sigma, args.lr, num_frames,
unperturbed_results, rank_diag, args.useAdam))
averageReward.append(np.mean(returns))
episodeCounter.append(num_eps)
maxReward.append(max(returns))
minReward.append(min(returns))
pltAvg, = plt.plot(episodeCounter, averageReward, label='average')
pltMax, = plt.plot(episodeCounter, maxReward, label='max')
pltMin, = plt.plot(episodeCounter, minReward, label='min')
plt.ylabel('rewards')
plt.xlabel('episode num')
plt.legend(handles=[pltAvg, pltMax,pltMin])
fig1 = plt.gcf()
plt.draw()
fig1.savefig('graph.png', dpi=100)
# For each model, generate the same random numbers as we did
# before, and update parameters. We apply weight decay once.
if args.useAdam:
globalGrads = None
for i in range(args.n):
np.random.seed(random_seeds[i])
multiplier = -1 if neg_list[i] else 1
reward = shaped_returns[i]
localGrads = []
idx = 0
for k, v in synced_model.es_params():
eps = np.random.normal(0, 1, v.size())
grad = torch.from_numpy((args.n*args.sigma) * (reward*multiplier*eps)).float()
localGrads.append(grad)
if len(optimConfig) == idx:
optimConfig.append({ 'learningRate' : args.lr })
idx = idx + 1
if globalGrads == None:
globalGrads = localGrads
else:
for i in range(len(globalGrads)):
globalGrads[i] = torch.add(globalGrads[i], localGrads[i])
idx = 0
for k, v in synced_model.es_params():
r, _ = legacyOptim.adam( lambda x: (1, -globalGrads[idx]), v , optimConfig[idx])
v.copy_(r)
idx = idx + 1
else:
# For each model, generate the same random numbers as we did
# before, and update parameters. We apply weight decay once.
for i in range(args.n):
np.random.seed(random_seeds[i])
multiplier = -1 if neg_list[i] else 1
reward = shaped_returns[i]
for k, v in synced_model.es_params():
eps = np.random.normal(0, 1, v.size())
v += torch.from_numpy(args.lr/(args.n*args.sigma) *
(reward*multiplier*eps)).float()
args.lr *= args.lr_decay
torch.save(synced_model.state_dict(),
os.path.join(chkpt_dir, 'latest.pth'))
return synced_model
def render_env(args, model, env):
while True:
state = env.reset()
state = torch.from_numpy(state)
this_model_return = 0
if not args.small_net:
cx = Variable(torch.zeros(1, 256))
hx = Variable(torch.zeros(1, 256))
done = False
while not done:
if args.small_net:
state = state.float()
state = state.view(1, env.observation_space.shape[0])
logit = model(Variable(state, volatile=True))
else:
logit, (hx, cx) = model(
(Variable(state.unsqueeze(0), volatile=True),
(hx, cx)))
prob = F.softmax(logit)
action = prob.max(1)[1].data.numpy()
state, reward, done, _ = env.step(action[0, 0])
env.render()
this_model_return += reward
state = torch.from_numpy(state)
print('Reward: %f' % this_model_return)
def generate_seeds_and_models(args, synced_model, env):
"""
Returns a seed and 2 perturbed models
"""
np.random.seed()
random_seed = np.random.randint(2**30)
two_models = perturb_model(args, synced_model, random_seed, env)
return random_seed, two_models
def train_loop(args, synced_model, env, chkpt_dir):
def flatten(raw_results, index):
notflat_results = [result[index] for result in raw_results]
return [item for sublist in notflat_results for item in sublist]
print("Num params in network %d" % synced_model.count_parameters())
num_eps = 0
total_num_frames = 0
for _ in range(args.max_gradient_updates):
processes = []
return_queue = mp.Queue()
all_seeds, all_models = [], []
# Generate a perturbation and its antithesis
for j in range(int(args.n/2)):
random_seed, two_models = generate_seeds_and_models(args,
synced_model,
env)
# Add twice because we get two models with the same seed
all_seeds.append(random_seed)
all_seeds.append(random_seed)
all_models += two_models
assert len(all_seeds) == len(all_models)
# Keep track of which perturbations were positive and negative
# Start with negative true because pop() makes us go backwards
is_negative = True
# Add all peturbed models to the queue
while all_models:
perturbed_model = all_models.pop()
seed = all_seeds.pop()
p = mp.Process(target=do_rollouts, args=(args,
[perturbed_model],
[seed],
return_queue,
env,
[is_negative]))
p.start()
processes.append(p)
is_negative = not is_negative
assert len(all_seeds) == 0
# Evaluate the unperturbed model as well
p = mp.Process(target=do_rollouts, args=(args, [synced_model],
['dummy_seed'],
return_queue, env,
['dummy_neg']))
p.start()
processes.append(p)
for p in processes:
p.join()
raw_results = [return_queue.get() for p in processes]
seeds, results, num_frames, neg_list = [flatten(raw_results, index)
for index in [0, 1, 2, 3]]
# Separate the unperturbed results from the perturbed results
_ = unperturbed_index = seeds.index('dummy_seed')
seeds.pop(unperturbed_index)
unperturbed_results = results.pop(unperturbed_index)
_ = num_frames.pop(unperturbed_index)
_ = neg_list.pop(unperturbed_index)
total_num_frames += sum(num_frames)
num_eps += len(results)
synced_model = gradient_update(args, synced_model, results, seeds,
neg_list, num_eps, total_num_frames,
chkpt_dir, unperturbed_results)
if args.variable_ep_len:
args.max_episode_length = int(2*sum(num_frames)/len(num_frames))