-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
328 lines (257 loc) · 14.2 KB
/
model.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
import random
import numpy as np
import tensorflow as tf # Deep Learning library
from sklearn.preprocessing import OneHotEncoder
from prioritized_exp_replay import Memory
class DDDQNNet:
def __init__(self, state_size, action_size, learning_rate, sess, name):
self.state_size = state_size
self.action_size = action_size
self.learning_rate = learning_rate
self.name = name
# We use tf.variable_scope here to know which network we're using (DQN or target_net)
# it will be useful when we will update our w- parameters (by copy the DQN parameters)
with sess.graph.as_default():
with tf.variable_scope(self.name):
# We create the placeholders
# *state_size means that we take each elements of state_size in tuple hence is like if we wrote
# [None, 100, 120, 4]
self.inputs_ = tf.placeholder(tf.float32, [None, *state_size], name="inputs")
#
self.ISWeights_ = tf.placeholder(tf.float32, [None, 1], name='IS_weights')
self.actions_ = tf.placeholder(tf.float32, [None, action_size], name="actions_")
# Remember that target_Q is the R(s,a) + ymax Qhat(s', a')
self.target_Q = tf.placeholder(tf.float32, [None], name="target")
# Input
self.batchNorm1 = tf.keras.layers.BatchNormalization(name="batchNorm1")(self.inputs_)
self.dense1 = tf.layers.dense(inputs=self.batchNorm1,
units=512,
activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="dense1")
self.batchNorm2 = tf.keras.layers.BatchNormalization(name="batchNorm2")(self.inputs_)
self.dense2 = tf.layers.dense(inputs=self.batchNorm2,
units=256,
activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="dense2")
self.dropOut = tf.keras.layers.Dropout(rate=0.2)(self.dense2)
# Here we separate into two streams
# The one that calculate V(s)
self.value_fc = tf.layers.dense(inputs=self.dropOut,
units=128,
activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="value_fc")
self.value = tf.layers.dense(inputs=self.value_fc,
units=1,
activation=None,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="value")
# The one that calculate A(s,a)
self.advantage_fc = tf.layers.dense(inputs=self.dense2,
units=128,
activation=tf.nn.elu,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="advantage_fc")
self.advantage = tf.layers.dense(inputs=self.advantage_fc,
units=self.action_size,
activation=None,
kernel_initializer=tf.contrib.layers.xavier_initializer(),
name="advantages")
# Aggregating layer
# Q(s,a) = V(s) + (A(s,a) - 1/|A| * sum A(s,a'))
self.output = self.value + tf.subtract(self.advantage,
tf.reduce_mean(self.advantage, axis=1, keepdims=True))
# Q is our predicted Q value.
self.Q = tf.reduce_sum(tf.multiply(self.output, self.actions_), axis=1)
# The loss is modified because of PER
self.absolute_errors = tf.abs(self.target_Q - self.Q) # for updating Sumtree
self.loss = tf.reduce_mean(self.ISWeights_ * tf.losses.huber_loss(self.target_Q, self.Q))
self.optimizer = tf.train.RMSPropOptimizer(self.learning_rate).minimize(self.loss)
# summary
self.summary_loss = tf.summary.scalar("Loss", self.loss)
print("Created model with action " + str(self.action_size) + ", state " + str(self.state_size))
class Model:
# tensorflow backend
"""
Nh=Ns(α/(Ni+No))
Ni = number of input neurons.
No = number of output neurons.
Ns = number of samples in training data set.
α = an arbitrary scaling factor usually 2-10.
"""
memory_size = 10000
learning_rate = 0.00025 # Alpha (aka learning rate)
sess = tf.Session()
saver = None
writer = None
def __init__(self, n_actions, n_states, batch_size, name):
self.n_actions = n_actions
self.n_states = n_states
self.batch_size = batch_size
self.step = 0
self.name = name
self.write_op = None
self.memory = Memory(self.memory_size)
self.one_hot_encoder = OneHotEncoder(sparse=False, categories='auto')
self.one_hot_encoder.fit(np.array([i for i in range(self.n_actions)]).reshape(-1, 1))
@classmethod
def init(cls):
with cls.sess.graph.as_default():
cls.sess.run(tf.global_variables_initializer())
cls.saver = tf.train.Saver()
# Setup TensorBoard Writer
cls.writer = tf.summary.FileWriter("/home/laikasin93/python/tensorboard/dddqn/1")
cls.writer.add_graph(cls.sess.graph)
def fit(self, state, reward, action_value, next_state=None):
self.store_experience(state, reward, action_value, next_state)
if self.memory.is_full():
self.fit_model(self.batch_size)
self.step = self.step + 1
else:
if self.memory.tree.data_pointer % 1000 == 0:
print(self.name + " - Filled memory - " + str(self.memory.tree.data_pointer))
def store_experience(self, state, reward, action_value, next_state=None):
# prioritized replay
if next_state is None:
transition = state, action_value, reward
else:
transition = state, action_value, reward, next_state
self.memory.store(transition) # have high priority for newly arrived transition
def predict(self, state):
# state = np.expand_dims(state, axis=0)
# action_pos = self.model.predict(state).argmax()
with self.sess.graph.as_default():
Qs = self.sess.run(self.model.output, feed_dict={self.model.inputs_: state.reshape((1, *state.shape))})
# Take the biggest Q value (= the best action)
action_pos = np.argmax(Qs)
action_value = self.action_map_to_value(action_pos)
# print("Predicted action: " + str(action_value))
return action_value
def get_random_action(self):
action_value = random.choice(list(self.action_map.keys()))
# print("Random action: " + str(action_value))
return action_value
def action_map_to_value(self, search_action):
for value, action in self.action_map.items():
if (action == search_action).all():
return value
def value_map_to_action(self, value):
value = list(map(lambda x: self.action_map.get(x), value))
action = self.one_hot_encoder.transform([[x] for x in value])
return action
def fit_model(self, batch_size):
# LEARNING PART
# Obtain random mini-batch from memory
tree_idx, batch, ISWeights_mb = self.memory.sample(self.batch_size)
states_mb = np.array([each[0][0] for each in batch])
actions_mb = np.array([each[0][1] for each in batch])
rewards_mb = np.array([each[0][2] for each in batch])
target_Qs_batch = []
actions_mb = self.value_map_to_action(actions_mb)
# Set Q_target = r
for i in range(0, len(batch)):
# Reward as target
target_Qs_batch.append(rewards_mb[i])
targets_mb = np.array([each for each in target_Qs_batch])
with self.sess.graph.as_default():
_, loss, absolute_errors = self.sess.run(
[self.model.optimizer, self.model.loss, self.model.absolute_errors],
feed_dict={self.model.inputs_: states_mb,
self.model.target_Q: targets_mb,
self.model.actions_: actions_mb,
self.model.ISWeights_: ISWeights_mb})
# Update priority
self.memory.batch_update(tree_idx, absolute_errors)
# Write TF Summaries
summary = self.sess.run(self.write_op, feed_dict={self.model.inputs_: states_mb,
self.model.target_Q: targets_mb,
self.model.actions_: actions_mb,
self.model.ISWeights_: ISWeights_mb})
self.writer.add_summary(summary, self.step)
self.writer.flush()
class OrderModel(Model):
# action map the value (percentage of ma(5)) to the output form from the model
action_map = {-3: 0,
-2: 1,
-1: 2,
0: 3,
1: 4,
2: 5,
3: 6}
def __init__(self, n_actions, n_states, batch_size, name):
super().__init__(n_actions, n_states, batch_size, name)
# Instantiate the DQNetwork
self.model = DDDQNNet(n_states, n_actions, self.learning_rate, self.sess, name=(name + "DQNetwork"))
with self.sess.graph.as_default():
self.write_op = tf.summary.merge_all(scope=(self.name + "DQNetwork"))
class SignalModel(Model):
action_map = {False: 0,
True: 1}
def __init__(self, n_actions, n_states, batch_size, name):
super().__init__(n_actions, n_states, batch_size, name)
# Instantiate the DQNetwork
self.model = DDDQNNet(n_states, n_actions, self.learning_rate, self.sess, name=(name + "DQNetwork"))
with self.sess.graph.as_default():
self.write_op = tf.summary.merge_all(scope=(self.name + "DQNetwork"))
class SellSignalModel(SignalModel):
gamma = 0.99 # Discounting rate
def __init__(self, n_actions, n_states, batch_size, name):
super().__init__(n_actions, n_states, batch_size, name)
# Instantiate the target network
self.target_model = DDDQNNet(n_states, n_actions, self.learning_rate, self.sess, name=(name + "TargetNetwork"))
# Copy the parameters of DQN to Target_network
def update_target_graph(self):
# Get the parameters of our DQNNetwork
from_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name + "DQNetwork")
# Get the parameters of our Target_network
to_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name + "TargetNetwork")
op_holder = []
# Update our target_network parameters with DQNNetwork parameters
for from_var, to_var in zip(from_vars, to_vars):
op_holder.append(to_var.assign(from_var))
with self.sess.graph.as_default():
self.sess.run(op_holder)
print("Target Model updated")
def fit_model(self, batch_size):
# LEARNING PART
# Obtain random mini-batch from memory
tree_idx, batch, ISWeights_mb = self.memory.sample(self.batch_size)
states_mb = np.array([each[0][0] for each in batch])
actions_mb = np.array([each[0][1] for each in batch])
rewards_mb = np.array([each[0][2] for each in batch])
next_states_mb = np.array([each[0][3] for each in batch])
target_Qs_batch = []
actions_mb = self.value_map_to_action(actions_mb)
# DOUBLE DQN Logic
# Use DQNNetwork to select the action to take at next_state (a') (action with the highest Q-value)
# Use TargetNetwork to calculate the Q_val of Q(s',a')
with self.sess.graph.as_default():
# Get Q values for next_state
q_next_state = self.sess.run(self.model.output, feed_dict={self.model.inputs_: next_states_mb})
# Calculate Qtarget for all actions that state
q_target_next_state = self.sess.run(self.target_model.output,
feed_dict={self.target_model.inputs_: next_states_mb})
# Q_target = r + gamma * Qtarget(s',a')
for i in range(0, len(batch)):
# We got a'
action = np.argmax(q_next_state[i])
target = rewards_mb[i] + self.gamma * q_target_next_state[i][action]
target_Qs_batch.append(target)
targets_mb = np.array([each for each in target_Qs_batch])
_, loss, absolute_errors = self.sess.run(
[self.model.optimizer, self.model.loss, self.model.absolute_errors],
feed_dict={self.model.inputs_: states_mb,
self.model.target_Q: targets_mb,
self.model.actions_: actions_mb,
self.model.ISWeights_: ISWeights_mb})
# Update priority
self.memory.batch_update(tree_idx, absolute_errors)
# Write TF Summaries
summary = self.sess.run(self.write_op, feed_dict={self.model.inputs_: states_mb,
self.model.target_Q: targets_mb,
self.model.actions_: actions_mb,
self.model.ISWeights_: ISWeights_mb})
self.writer.add_summary(summary, self.step)
self.writer.flush()