-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdq.py
454 lines (352 loc) · 14.5 KB
/
dq.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
from __future__ import print_function
import numpy as np
import gym
from gym.spaces import Discrete, Box
# DQN for Reinforcement Learning
# by Qin Yongliang
# 2017 01 11
def continuous_actions(env):
if isinstance(env.action_space,Box):
pass
split = 7
dims = env.action_space.shape[0]
action_count = split * dims
low = env.action_space.low
high = env.action_space.high
itvl = high - low
global cbuf
cbuf = np.zeros((dims),dtype='float32')
def d2c(index):
global cbuf
# cbuf = cbuf*.5
idx = index
chosen_dim = int(idx/split)
chosen_split = idx%split
cbuf[chosen_dim] = chosen_split/float(split-1)
cont = cbuf * itvl + low
# print(cont)
return cont
return action_count,d2c
# run episode with some policy of some agent, and collect the rewards
# well the usual gym stuff
def do_episode_collect_trajectory(agent, env, max_steps, render=True, feed=True, realtime=False, use_best=False):
observation = env.reset() # s1
last_observation = observation
agent.wakeup() # notify the agent episode starts
total_reward=0
for t in range(max_steps):
global cbuf
combined_observation = np.hstack([last_observation,observation,cbuf])
last_observation = observation
action = agent.act(combined_observation,use_best=use_best) # a1
if isinstance(env.action_space,Box):
# action_count,d2c = continuous_actions(env)
actual_action = d2c(action)
else:
actual_action = action
# s2, r1,
observation, reward, done, _info = env.step(actual_action)
# d1
isdone = 1 if done else 0
total_reward += reward
if feed:
agent.feed_immediate_data((combined_observation,action,reward,isdone))
if render and (t%15==0 or realtime==True): env.render()
if done :
break
print('episode done in',t,'steps, total reward',total_reward)
return
# keras boilerplate: the simplest way to neural networking
from keras.models import *
from keras.layers import *
from keras.optimizers import *
import keras
from math import *
import keras.backend as K
import time
# our neural network agent.
class nnagent(object):
def __init__(self, num_of_actions, num_of_observations, discount_factor, optimizer, epsilon=-1,):
# agent database
self.observations = np.zeros((0,num_of_observations))
self.actions = np.zeros((0,num_of_actions))
self.rewards = np.zeros((0,1))
self.isdone = np.zeros((0,1))
# agent property
self.num_of_actions = num_of_actions
self.num_of_observations = num_of_observations
self.discount_factor = discount_factor
self.big_C = 20
self.big_C_counter = 0
self.epsilon = epsilon # epsilon-greedy per David Silver's Lecture and DeepMind paper.
# -----------------------------
# Deep-Q-Network
from keras.regularizers import l2, activity_l2
def resdense(features):
def unit(i):
hfeatures = max(4,int(features/4))
ident = i
i = Dense(features,activation='tanh')(i)
ident = Dense(hfeatures)(ident)
ident = Dense(features)(ident)
return merge([ident,i],mode='sum')
return unit
input_shape = num_of_observations
inp = Input(shape=(input_shape,))
i = inp
# i1 = inp
# i2 = inp
# i = BatchNormalization()(i)
# i = Activation('linear')(i)
# i1 = arm(i1)
# i2 = arm(i2)
# i = Dense(8,activation='tanh')(i)
# i = Dense(64,activation='relu')(i)
# i = Dense(16,activation='tanh')(i)
# i = Dense(1024,activation='tanh')(i)
# i = Dense(128,activation='relu')(i)
i = resdense(64)(i)
i = resdense(32)(i)
i = resdense(128)(i)
# i = Dense(32,activation='tanh')(i)
# i = MaxoutDense(32)(i)
# i = MaxoutDense(32)(i)
# i = MaxoutDense(64)(i)
# i = MaxoutDense(8)(i)
# i = Activation('relu')(i)
# i = residual(i,64)
# i = residual(i,16)
# i = residual(i,32)
# i = residual(i,32)
# i = residual(i,64)
# i = residual(i,32)
# i = residual(i,6)
# i = BatchNormalization()(i)
# i = Activation('elu')(i)
# i1 = Dense(1)(i1)
# i2 = Dense(1)(i2)
# i = merge([i1,i2],mode='concat')
# i = Dense(16,activation='tanh')(i)
# i = Dense(16,activation='tanh')(i)
# i = Dense(16,activation='tanh')(i)
# i = Dense(16,activation='tanh')(i)
# i = Dense(16,activation='tanh')(i)
# i = arm(i,128)
# i = arm(i,16)
# i = arm(i,16)
# i = arm(i,16)
# i = arm(i,32)
# i = arm(i,num_of_actions)
# abuf = []
# for k in range(num_of_actions):
# r = Dense(5,activation='tanh')(i)
# r = Dense(1)(r)
# abuf.append(r)
# out = merge(abuf,mode='concat')
i = Dense(num_of_actions)(i)
# out = Activation('softmax')(i)
out = i
qfunc = Model(input=inp,output=out)
self.qfunc = qfunc
# ------------------------------
# ------------------------------
# DQN trainer
s1 = Input(shape=(input_shape,))
a1 = Input(shape=(num_of_actions,))
r1 = Input(shape=(1,))
isdone = Input(shape=(1,))
# s2 = Input(shape=(input_shape,))
qs2 = Input(shape=(num_of_actions,)) # qs2 is precalc-ed
q_prediction = qfunc(s1)
# the q values we predicted for the given state.
q_s1_a1 = merge([q_prediction,a1],
mode=(lambda x:K.sum(x[0] * x[1],axis=-1,keepdims=True)),
output_shape=(1,))
def calc_target(x):
qs2 = x[0] # q value of next state
r1 = x[1]
isdone = x[2]
return (K.max(qs2,axis=-1,keepdims=True) * discount_factor * (1-isdone) + r1)
q_target = merge([qs2,r1,isdone],
mode=calc_target,output_shape=(1,))
# target = sum of [immediate reward after action a] and [q values predicted for next state, discounted]. target is a better approximation of q function for current state, so we use it as the training target.
def mse(x):
return K.mean((x[0] - x[1])**2, axis=-1, keepdims=True)
q_loss = merge([q_target,q_s1_a1],
mode=mse,output_shape=(1,),name='q_loss')
# what we meant: q_loss = (q_target - q_prediction)**2
qtrain = Model(input=[s1,a1,r1,isdone,qs2],output=q_loss)
def pass_thru(y_true,y_pred):
return K.mean(y_pred,axis=-1)
qtrain.compile(loss=pass_thru,optimizer=optimizer)
# -----------------------------
# -----------------------------
# mirrored DQN(for 'target' calculation)
qfunc2 = model_from_json(qfunc.to_json())
# -------------------------
self.qfunc = qfunc
self.qfunc2 = qfunc2
self.qtrain = qtrain
print('agent Initialized with',num_of_observations,'dim input and',num_of_actions,'dim output.')
print('discount_factor',discount_factor)
print('model architechture:')
qfunc.summary()
print('trainer architechture:')
qtrain.summary()
# act one step base on observation
def act(self, observation, use_best=False):
qfunc = self.qfunc
epsilon = self.epsilon # greedy factor
observation = observation.reshape((1,len(observation)))
# observation is a vector
qvalues = qfunc.predict([observation])[0]
# for qfunc:
# with probability epsilon we act randomly:
if (self.epsilon > np.random.rand(1)) and use_best==False:
action_index = np.random.choice(len(qvalues))
else:
# with probability 1-epsilon we act greedy:
action_index = qvalues.argmax()
# print(action_index)
from winfrey import showbar
showbar(np.hstack([qvalues,np.max(qvalues,keepdims=True)]),action_index) #visualization
agent.epsilon -= 1./10000
agent.epsilon = max(0.1,agent.epsilon)
return action_index
def wakeup(self):
# clear states
pass
# after playing for one(or whatever) episode, we could feed the agent with data.
def feed_episodic_data(self,episodic_data):
observations,actions,rewards,isdone = episodic_data
actions = np.array(actions)
rewards = np.array(rewards).reshape((-1,1))
isdone = np.array(isdone).reshape((-1,1))
# IMPORTANT: convert actions to their one-hot representations
def one_hot(tensor,classes):
heat = np.zeros(tensor.shape+(classes,))
for i in range(classes):
heat[...,i] = tensor[...] == i
return heat
onehot_actions = one_hot(actions,self.num_of_actions)
# add to agent's database
self.observations = np.vstack((self.observations,observations))
self.actions = np.vstack((self.actions,onehot_actions))
self.rewards = np.vstack((self.rewards,rewards))
self.isdone = np.vstack((self.isdone,isdone))
def feed_immediate_data(self,immediate_data):
observation,action,rewards,isdone = immediate_data
action = np.array(action)
reward = np.array(rewards).reshape((-1,1))
isdone = np.array(isdone).reshape((-1,1))
# IMPORTANT: convert actions to their one-hot representations
def one_hot(tensor,classes):
heat = np.zeros(tensor.shape+(classes,))
for i in range(classes):
heat[...,i] = tensor[...] == i
return heat
onehot_action = one_hot(action,self.num_of_actions)
# add to agent's database
self.observations = np.vstack((self.observations,observation))
self.actions = np.vstack((self.actions,onehot_action))
self.rewards = np.vstack((self.rewards,reward))
self.isdone = np.vstack((self.isdone,isdone))
# self_train
if len(self.observations)>100:
self.train(epochs=1)
pass
def eat(self):
# throw away excessively long history
length = len(self.observations)
if length>50000:
eat = 50000-length
print('eating',eat,'kids..')
self.observations = self.observations[eat:length]
self.actions = self.actions[eat:length]
self.rewards = self.rewards[eat:length]
self.isdone = self.isdone[eat:length]
# train agent with some of its collected data from its database
def train(self,epochs=10):
qtrain = self.qtrain
qfunc = self.qfunc
qfunc2 = self.qfunc2
observations,actions,rewards,isdone = self.observations, self.actions,self.rewards,self.isdone
length = len(observations)
# print('----trainning for',epochs,'epochs')
for i in range(epochs):
# train 1 epoch on a randomly selected subset of the whole database.
if epochs-1 == i and epochs>1:
verbose = 2
else:
verbose = 0
# muted since bad for performance.
# before training, we may read the weights of the policy network, and load em into the 'target' network
# do this every C steps (How DeepMind this is.)
self.big_C_counter+=1
self.big_C_counter%=self.big_C
if self.big_C_counter == 0:
thetas = qfunc.get_weights()
qfunc2.set_weights(thetas)
subset_size = min(length-1,128)
indices = np.random.choice(length-1,subset_size,replace=False)
subset_observations = np.take(observations,indices,axis=0).astype('float32')
subset_actions = np.take(actions,indices,axis=0).astype('float32')
subset_rewards = np.take(rewards,indices,axis=0).astype('float32')
subset_isdone = np.take(isdone,indices,axis=0).astype('float32')
subset_next_observations = np.take(observations,indices+1,axis=0).astype('float32')
qs2 = self.qfunc2.predict(subset_next_observations)
# 'target' Q-func weights are not updated every training call, to prevent potential divergence problems
qtrain.fit([
subset_observations,
subset_actions,
subset_rewards,
subset_isdone,
qs2
], np.random.rand(subset_size),
batch_size=subset_size,
nb_epoch=1,
verbose=verbose,
shuffle=False)
# print('----done')
from gym import wrappers
# give it a try
# env = gym.make('Acrobot-v1')
# env = gym.make('Pendulum-v0')
env = gym.make('BipedalWalker-v2')
# env = gym.make('LunarLander-v2')BipedalWalker-v2
# env = gym.make('CartPole-v1')
# env = gym.make('MountainCar-v0')
# env = wrappers.Monitor(env,'./experiment-3',force=True)
if isinstance(env.action_space,Box):
action_count,d2c = continuous_actions(env)
num_of_actions = action_count
else:
num_of_actions = env.action_space.n
num_of_observations = env.observation_space.shape[0]
print('environment:',num_of_actions,'actions,',num_of_observations,'observations')
agent = nnagent(
num_of_actions=num_of_actions,
num_of_observations=num_of_observations*2 + len(cbuf),
discount_factor=.99,
epsilon=1.,
optimizer = RMSprop()
# optimizer = SGD(lr=0.0005, clipnorm=10.,momentum=0.0,nesterov=False) # momentum must = 0; use plain SGD
)
# main training loop
def r(times=3):
for k in range(times):
print('training loop',k,'/',times)
for i in range(1): # do 1 episode
print('play episode:',i)
do_episode_collect_trajectory(agent,env,max_steps=100000,render=True,feed=True)
# after play, the episodic data will be feeded to the agent AUTOMATICALLY, so no feeding here
# wait until collected data became diverse enough
if len(agent.observations)> 100:
# ask agent to train itself, with previously collected data
# agent.train(epochs=min(100,len(agent.observations)/4))
# decrease epsilon to make agent choose less and less random actions.
# agent.epsilon -= .02
# agent.epsilon = max(0.05,agent.epsilon)
print('agent epsilon:', agent.epsilon)
def check():
do_episode_collect_trajectory(agent,env,max_steps=1000,render=True,feed=False,realtime=True,use_best=True)