-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgan_rl_fitter.py
320 lines (227 loc) · 8.27 KB
/
gan_rl_fitter.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
313
314
315
316
317
318
319
320
import chainer
from chainer import Variable, optimizers, flag
from chainer import Link, Chain, ChainList
import chainer.functions as F
import chainer.links as L
import numpy as np
import cPickle as pc
import os
import json
# miscellaneous class
class EnvSpec():
def __init__(self, max_steps):
self.timestep_limit = max_steps
### ASSUMPTION: INITIAL CONDITION IS IMPORTANT TO GET RIGHT
w_init = 4.0 # wegiht of objective for initial output of RNN ~size of sequence
class DOBJ(Chain):
def __init__(self):
super(DOBJ, self).__init__()
def __call__(self, X, Yt, D, G):
D.reset_state()
r = 0.0
mg = w_init
for x, yt in zip(X, Yt):
t = D(x, yt)
r += F.mean_squared_error(t, t*0.0 + 1.0)*mg
mg = 1.0
D.reset_state()
G.reset_state()
mg = w_init
for x, yt in zip(X, Yt):
f = D(x, G(x))
r += F.mean_squared_error(f, f * 0.0)*mg
mg = 1.0
return r
class GOBJ(Chain):
def __init__(self):
super(GOBJ, self).__init__()
def __call__(self, X, D, G):
D.reset_state()
G.reset_state()
r = 0.0
mg = w_init
for x in X:
f = D(x, G(x))
r += F.mean_squared_error(f, f*0.0 + 1.0)*mg
mg = 1.0
return r
def tv(x, v = flag.OFF):
return Variable(np.array(x).astype('float32'), volatile=v)
from time import time
def FitStochastic(G, D, XY, learning_rate, momentum, iters, pr_caption=None):
X, Y = XY
X, Y = tv(X), tv(Y)
objD, objG = DOBJ(), GOBJ()
optG = optimizers.Adam(alpha=learning_rate, beta1=momentum)
optD = optimizers.Adam(alpha=learning_rate, beta1=momentum)
optG.setup(G)
optD.setup(D)
st = time()
for i in range(iters):
D.zerograds()
loss = objD(X, Y, D, G)
loss.backward()
optD.update()
G.zerograds()
loss = objG(X, D, G)
loss.backward()
optG.update()
if i % 10 == 0:
#for p in G.params():
# p.data = p.data + 0.5*np.random.randn(*p.data.shape).astype('float32')
print "iter:", i, "/", iters, "iter. time:", time()-st
if not pr_caption is None:
print pr_caption
st = time()
G.reset_state()
Yp = [G(x) for x in X]
for j in range(10):
Yi = [np.round(yp.data[j].astype('float64'), 1).tolist() for yp in Yp]
print Yi
""""""
# collect the data about env
def generate_data(agent, env, N):
O, A, R, E = [], [], [], []
for episode in range(N):
print "Real ep. ", episode
agent.reset_state()
observation = env.reset()
done = False
obvs, acts, rews, ends = [observation], [np.zeros(agent.action_size)], [0.0], [0.0]
# play
for i in range(env.spec.timestep_limit):
act, act_unformatted = agent.next([observation])
act = act[0]
act_unformatted = act_unformatted[0]
if done: # if episode is done - pad the episode to have max_steps length
reward = 0.0
else:
observation, reward, done, info = env.step(act)
rews.append(reward)
obvs.append(observation)
acts.append(act_unformatted)
ends.append(done*1.0)
O.append(obvs)
A.append(acts)
R.append(rews)
E.append(ends)
X, Y = [], []
# convert data to X, Y format
for i in range(env.spec.timestep_limit):
x, y = [], []
for o, a, r, e in zip(O, A, R, E):
x.append(a[i])
y.append(np.array(o[i].tolist() + [r[i], e[i]]))
X.append(np.array(x))
Y.append(np.array(y))
return X, Y, np.mean(np.array(R)[:,1:])
# warning: below function should be used during training only, eg agent(observ) returns Chainer tensor
def evaluate_on_diff_env(env, n_sample_traj, agent):
# this function is used to sample n traj using GAN version of environment
R = 0.0
# reset environment
env.reset_state()
agent.reset_state()
# get initial observation
observations = env(tv(np.zeros((n_sample_traj, env.act_size))))[:, :-2]
for i in range(env.spec.timestep_limit):
act = agent(observations)
obs_rew = env(act)
rewards = obs_rew[:, -2]
ends = obs_rew[:, -1]
observations = obs_rew[:, :-2]
R += F.sum(rewards * (1.0 - ends)) / (-len(rewards) * env.spec.timestep_limit)
return R
def train_gan_rl(CreateGenerator, CreateDiscriminator, CreateActor,
Environment,
project_folder,
noise_decay_schedule,
N_real_samples, N_GAN_batches, N_GAN_samples,
GAN_tr_lr, GAN_tr_mm, GAN_training_iter,
evaluation_only):
"""
This function performs reinforcement learning of an actor on environment using
combination of generative adversarial neural network model or real environment
and gradient descent.
:param CreateGenerator:
:param CreateDiscriminator:
:param CreateActor:
:param Environment:
:param project_folder:
:param noise_decay_schedule:
:param N_real_samples:
:param N_GAN_batches:
:param N_GAN_samples:
:param GAN_tr_lr:
:param GAN_tr_mm:
:param GAN_training_iter:
:param evaluation_only:
:return:
"""
idx = 0
files = ['train.bin', 'validate.bin']
agent_file = 'agent.bin'
perf_file = 'stats.json'
agent_file = os.path.join(project_folder, agent_file)
perf_file = os.path.join(project_folder, perf_file)
files = [os.path.join(project_folder, f) for f in files]
# load agent if possible
if os.path.exists(agent_file):
agent = pc.load(open(agent_file))
print "Loaded agent:", agent
else:
agent = CreateActor()
optA = optimizers.Adam(alpha=0.001, beta1=0.9)
optA.setup(agent)
# main training loop
for noise_p in noise_decay_schedule:
idx += 1
envs, perf = {}, {}
# load environments if already trained some
for fname in files:
if not os.path.exists(fname):
envs[fname] = {'G': CreateGenerator(), 'X': None, 'Y': None}
else:
envs[fname] = pc.load(open(fname))
# set temporarily noise probability != 0
agent.noise_probability = noise_p
# fit differentiable environment for training and testing
for fname in files:
X, Y, Rmean = generate_data(agent, Environment, N_real_samples)
perf[fname] = float(Rmean) # shows the performance of agent on real environment
if not evaluation_only:
# extend the data
if envs[fname]['X'] is None:
envs[fname]['X'] = X
envs[fname]['Y'] = Y
else:
for i in range(Environment.spec.timestep_limit):
for elm, v in [('X', X), ('Y', Y)]:
envs[fname][elm][i] = np.concatenate([envs[fname][elm][i], v[i]])
FitStochastic(
envs[fname]['G'],
CreateDiscriminator(),
(envs[fname]['X'], envs[fname]['Y']),
GAN_tr_lr,
GAN_tr_mm,
GAN_training_iter,
"Example generated [state_0, state_1, ..., reward, finished] sequences:")
pc.dump(envs[fname], open(fname, 'w'))
# set noise probability to zero for training
agent.noise_probability = 0
print "Performance:", perf
json.dump(perf, open(perf_file, 'w'))
if evaluation_only:
continue
# train the agent with SGD
for reps in range(N_GAN_batches):
# reset the agent
agent.cleargrads()
# train
R = evaluate_on_diff_env(envs[files[0]]['G'], N_GAN_samples, agent)
R.backward()
optA.update()
Rv = evaluate_on_diff_env(envs[files[1]]['G'], N_GAN_samples, agent)
print 'Avg. reward: training GAN = ', -R.data, 'testing GAN = ', -Rv.data
# save trained agent
pc.dump(agent, open(agent_file, 'w'))