-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.v1.py
201 lines (174 loc) · 7.37 KB
/
model.v1.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
import tensorflow as tf
import numpy as np
from tensorflow.contrib import rnn, layers
class DQL(object):
def __init__(self,
input_len,
nb_state_token,
nb_action_token):
self.input_len = input_len
self.nb_state_token = nb_state_token
self.nb_action_token = nb_action_token
self.built = False
def build(self,
cells=[64, 64],
basic_cell=rnn.LSTMCell,
state_emb_dim=50,
action_emb_dim=50,
batch_size=1,
learning_rate=1e-4,
clip_norm=1.,
gamma=1.0):
# state
single_state = tf.placeholder('int32', [1, self.input_len],
name='single_state')
batch_state = tf.placeholder('int32', [batch_size, self.input_len],
name='batch_state')
# state_embW = self.get_preset_state_emb(self.nb_state_token)
state_embW = tf.Variable(
tf.random_uniform([self.nb_state_token, state_emb_dim], -1.0, 1.0),
name='state_embW')
single_state_emb = tf.nn.embedding_lookup(state_embW, single_state)
batch_state_emb = tf.nn.embedding_lookup(state_embW, batch_state)
encoder = rnn.MultiRNNCell([basic_cell(c) for c in cells])
single_encoder_outputs, _ = \
tf.nn.dynamic_rnn(encoder, single_state_emb, dtype=tf.float32)
batch_encoder_outputs, _ = \
tf.nn.dynamic_rnn(encoder, batch_state_emb, dtype=tf.float32)
single_last_output = single_encoder_outputs[:, -1, :]
batch_last_output = batch_encoder_outputs[:, -1, :]
# action
action = tf.placeholder('int32', [batch_size], name='action')
action_embW = tf.Variable(
tf.random_uniform([self.nb_action_token,
action_emb_dim], -1.0, 1.0), name='action_embW')
# (bs, a_emb_dim)
action_emb = tf.nn.embedding_lookup(action_embW, action)
# joint
joint = tf.concat([batch_last_output, action_emb], axis=-1)
def decision_fnn(input, reuse):
with tf.variable_scope('decision', reuse=reuse):
x = layers.linear(input, 512, scope='d1')
x = tf.nn.relu(x)
x = layers.linear(x, 256, scope='d2')
x = tf.nn.relu(x)
x = layers.linear(x, 1, scope='qv')
return x
qvalue = decision_fnn(joint, reuse=False)
# expected_qv
expected_qv = tf.placeholder('float32', [batch_size],
name='expected_qv')
loss = expected_qv-qvalue[:, 0]
loss = tf.where(tf.abs(loss) < 1.0,
0.5 * tf.square(loss),
tf.abs(loss) - 0.5)
loss = tf.reduce_mean(loss)
optimizer = tf.train.AdamOptimizer(
learning_rate=learning_rate)
train_op = tf.contrib.slim.learning.create_train_op(
loss, optimizer, clip_gradient_norm=clip_norm)
# enumerate every actions
joints = tf.concat([tf.tile(single_last_output,
(self.nb_action_token, 1)),
action_embW], axis=-1)
qvalues = decision_fnn(joints, reuse=True)
max_qv = tf.reduce_max(qvalues)
max_qv_ind = tf.argmax(qvalues[:, 0])
# calculate Q(s, a)
self.single_state = single_state
self.batch_state = batch_state
self.qvalue = qvalue
self.action = action
# training
self.expected_qv = expected_qv
self.train_op = train_op
self.loss = loss
self.batch_size = batch_size
self.gamma = gamma
# finding max_{a'} Q(s', a')
self.qvalues = qvalues
self.max_qv = max_qv
self.max_qv_ind = max_qv_ind
self.built = True
def train(self,
env,
continued=False,
epsilon=0.1,
epsilon_decay=0.99,
epsilon_min=0.1,
iterations=1000,
rng=np.random,
max_exp=200):
assert (self.built)
with tf.Session() as sess:
R = tf.Variable(0.)
R_summary = tf.summary.scalar('R', R)
sess.run(tf.global_variables_initializer())
train_writer = tf.summary.FileWriter('tensorboard', sess.graph)
saver = tf.train.Saver()
if continued:
saver.restore(sess, 'session/sess.ckpt')
exp = []
global_step = tf.get_collection(tf.GraphKeys.GLOBAL_STEP)[0]
i = begin = global_step.eval()
for i in range(begin, begin+iterations):
env.reset()
sess.run(R.assign(0))
print '[{}, {}]'.format(i, epsilon)
while True:
s = env.state
if rng.rand() < epsilon:
a = rng.choice(self.nb_action_token)
else:
a = sess.run(self.max_qv_ind,
feed_dict={self.single_state: [s]})
r = env.play(a)
s_next = env.state
exp.append((s, a, r, s_next))
# print s
sess.run(R.assign_add(r))
if r <= 0:
break
# sample and update
batch_s = []
batch_a = []
batch_q = []
batch_ind = np.random.choice(len(exp),
size=(self.batch_size,))
for exp_ind in batch_ind:
s, a, r, s_next = exp[exp_ind]
if r > 0:
exp_pv = sess.run(self.max_qv,
feed_dict={self.single_state:
[s_next]})
exp_pv = r + self.gamma*exp_pv
else:
exp_pv = r
batch_s.append(s)
batch_a.append(a)
batch_q.append(exp_pv)
feed_dict = {self.batch_state: batch_s,
self.action: batch_a,
self.expected_qv: batch_q}
qv, loss, _ = sess.run([self.qvalue,
self.loss,
self.train_op],
feed_dict=feed_dict)
# print loss, zip(qv[:, 0], batch_q)
epsilon = max(epsilon*epsilon_decay, epsilon_min)
if len(exp) > max_exp:
np.random.shuffle(exp)
exp = exp[:int(max_exp*0.2)]
print 'shuffle'
train_writer.add_summary(R_summary.eval(), i)
if i % 100 == 0:
saver.save(sess, 'session/sess.ckpt')
def get_preset_state_emb(self, n):
emb = np.ones((n+1, n+1), dtype='float32') * (-1)
emb[0] = 0
for i in range(1, n+1):
for j in range(i):
emb[i, j] = +1
return tf.Variable(emb, name='state_embW', trainable=False)
if __name__ == '__main__':
pass